Announcement Announcement Module
Collapse
No announcement yet.
How to pass a param to viewClass - RedirectView Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to pass a param to viewClass - RedirectView

    Forgive me if this is foolish, there is so much to digest when starting with Spring, I'd like to do a simple redirect to JSPs, its sort of working, except I want to tell my viewClass RedirectView to set contextRelative to "true". Can anyone show me how to do that? I've tried a number of different things and googled my eyes out, so I'm not hastily posting this request.
    Code:
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">
          	<property name="viewClass" value="org.springframework.web.servlet.view.RedirectView"/>
    	    <property name="prefix" value="/WEB-INF/jsp/"/>
    	    <property name="suffix" value=".jsp"/>
    </bean>
    Thanks!

  • #2
    More info...I'm still stuck...please help!

    80 views and no ideas ya'll? I'm still very much stuck, so if anyone has ANY ideas, I'll take them. Is it just not possible to configure viewClass parameters with XML?

    Here is more detail on the configs that I have, and some info on what I've tried. Assume I got the XML at the top correct (can't post URLs yet).

    web.xml
    Code:
    <web-app>
    
    	<servlet>
    		<servlet-name>urlfilename</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>urlfilename</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    
        <welcome-file-list>
    		<welcome-file>index.html</welcome-file>
    	</welcome-file-list>
    
    </web-app>
    urlfilename-servlet.xml
    Code:
    <beans>
    	<bean id="handlerMapping"
    		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="mappings">
    			<props>
    				<prop key="*">someViewController</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="someViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController">
    		 <property name="cacheSeconds" value="120"/>
    	</bean>
    	
    	<bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">
          	<property name="viewClass" value="org.springframework.web.servlet.view.RedirectView"/>
    	    <property name="prefix" value="/WEB-INF/jsp/"/>
    	    <property name="suffix" value=".jsp"/>
    	</bean>
    </beans>
    Above is the basic configuration, when I point my browser to:
    --my site context --/news/index.do
    it redirects to /WEB-INF/jsp/news/index.jsp, dropping the context. The "org.springframework.web.servlet.view.RedirectView " class accepts a parameter "contextRelative"...which according to the api docs says:
    Note that while the default value for the "contextRelative" flag is off, you will probably want to almost always set it to true. With the flag off, URLs starting with "/" are considered relative to the web server root, while with the flag on, they are considered relative to the web application root. Since most web applications will never know or care what their context path actually is, they are much better off setting this flag to true, and submitting paths which are to be considered relative to the web application root.
    So...give this, I assumed I could pass a parameter somehow. I tried the following in the urlfilename-servlet.xml file:
    Code:
    <bean id="redirectViewClass" class="org.springframework.web.servlet.view.RedirectView">
    	<property name="url" value="/test/"/>
    	<property name="contextRelative" value="true"/>
    </bean>
    
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">
       <property name="viewClass" ref="redirectViewClass"/>
       <property name="prefix" value="/WEB-INF/jsp/"/>
       <property name="suffix" value=".jsp"/>
    </bean>
    I only added the url property because it said it was required, though I was hoping/thinking that part could be passed via the framework.
    org.springframework.beans.factory.BeanCreationExce ption:
    Error creating bean with name 'viewResolver' defined in ServletContext resource [/WEB-INF/urlfilename-servlet.xml]:
    Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException:
    Failed to convert property value of type [org.springframework.web.servlet.view.RedirectView] to required type [java.lang.Class] for property 'viewClass'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.web.servlet.view.RedirectView] to required type [java.lang.Class] for property 'viewClass': no matching editors or conversion strategy found
    Basically tells me I can't refer to another bean definition for this value. So, I may just not understand how to do XML properly? Maybe this type of thing can only be done creating new Java classes - I was trying to do minimal Java maximum 'configuration'.

    Thanks again to anyone who responds with some ideas!

    Comment

    Working...
    X