Results 1 to 9 of 9

Thread: Struts ForwardAction counterpart in Spring MVC?

  1. #1
    Join Date
    May 2005
    Location
    BEEK, The Netherlands
    Posts
    230

    Default Struts ForwardAction counterpart in Spring MVC?

    Hi,

    I'm currenly doing a pilot with the Spring framework and rebuilding a my Struts controller to Spring MVC.
    I have a lot of simple url requests (actions) which do nothing with the model but just go to a .jsp page (view). Struts has a convienient ForwarAction I use for this.

    <action
    path="/login"
    type="org.apache.struts.actions.ForwardAction"
    parameter="/WEB-INF/jsp/login/display_login.jsp"
    />

    I can use the Spring MVC UrlFilenameViewController and SimpleUrlHandlerMapping but then the view name has to have the same name as the path.

    I was wondering if there is some UrlHandlerMapping in which I can wire the url mapping path directly to a view name?

    Regards,
    Marcel

  2. #2
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    I think you want to look at the view resolvers; e.g., org.springframework.web.servlet.view.InternalResou rceViewResolver.

    Look at the api docs for org.springframework.web.servlet.ViewResolver and then look at the classes that implement that interface.

  3. #3
    Join Date
    May 2005
    Location
    BEEK, The Netherlands
    Posts
    230

    Default

    Thanks for your reply lumpynose,

    I think the view resolving is one step later.

    What I'm trying to avoid is to create and define a Controllers (actions) for simple forward actions.

  4. #4
    Join Date
    Aug 2004
    Location
    Atlanta, GA
    Posts
    129

    Default

    The InternalResourceViewResolver iteself can work with proper naming conventions, but I think what you're looking for is the ParameterizableViewController. You'll need to map the incoming url to the controller, and the controller forwards to a (specified) view name, which must then be mapped via a ViewResolver to the desired jsp.

    The samples have examples of its usage.

    HTH
    Randy

  5. #5
    Join Date
    May 2005
    Location
    BEEK, The Netherlands
    Posts
    230

    Default

    Thanks for your reply HTH,

    With the ParameterizableViewController I could do the following:

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="/home.htm">homeForwardController</prop>
    <prop key="/faq.htm">faqForwardController</prop>
    </props>
    </property>
    </bean>

    <bean name="homeForwardController" class="org.springframework.web.servlet.mvc.Paramet erizableViewController">
    <property name="viewName"><value>indexView</value></property>
    </bean>

    <bean name="faqForwardController" class="org.springframework.web.servlet.mvc.Paramet erizableViewController">
    <property name="viewName"><value>faqView</value></property>
    </bean>

    This would work, but for each simple forward action (from a request directly to a view) I have to define a controller! This is time consuming and will explode my *-servlet.xml.

    I was hoping to define 1 controller and specify the view name as a property in the HandlerMapping.

    E.g. some kind of SimpleUrlToViewHandlerMapping class:
    <bean id="urlMapping" class="xyz.SimpleUrlToViewHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="/home.htm">indexView</prop>
    <prop key="/faq.htm">faqView</prop>
    </props>
    </property>
    </bean>

    Before creating some class like this, I want to make sure I cannot define this with the already existing classes.

    Regards,
    Marcel

  6. #6
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    ("HTH" is one of those acronyms like LOL. HTH means "happy to help.")

  7. #7
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    What about the ResourceBundleViewResolver? With it I have a views.properties file that specifies 3 views handled by the same class:
    Code:
    signInView.class=org.springframework.web.servlet.view.JstlView
    signInView.url=/WEB-INF/jsp/signInView.jsp
    
    adminView.class=org.springframework.web.servlet.view.JstlView
    adminView.url=/WEB-INF/jsp/adminView.jsp
    
    waitlistView.class=org.springframework.web.servlet.view.JstlView
    waitlistView.url=/WEB-INF/jsp/waitlistView.jsp
    (I'm a rank newbie at this so I may not know what I'm talking about.)

  8. #8
    Join Date
    May 2005
    Location
    BEEK, The Netherlands
    Posts
    230

    Default

    Hi, - thanks for your help again -

    I'm using the XmlViewResolver to do what you described.

    You wrote you are using one class (1 controller I assume) and the have the ability to forward to 1 of 3 views. This means you had the code the controller class.
    That's what I don't want to do as my controller type as I described is a very very simple controller which returns always only 1 and the same view.

    The ParameterizableViewController is the which comes close but it could be better....

    Regards,
    Marcel

  9. #9
    Join Date
    May 2005
    Location
    BEEK, The Netherlands
    Posts
    230

    Default

    Hi,
    I solved it by using the standard BeanNameUrlHandlerMapping and ParameterizableViewController classes.
    See the example:

    Code:
    <!-- Simple handler mapping which maps url requests directly to the specified views. -->
    <bean id="urlViewMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 
    
    <bean name="/index.htm" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> 
        <property name="viewName"><value>indexView</value></property> 
    </bean> 
    <bean name="/faq.htm" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> 
        <property name="viewName"><value>faqView</value></property> 
    </bean> 	
    <bean name="/questions.htm" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> 
        <property name="viewName"><value>faqView2</value></property> 
    </bean>

Similar Threads

  1. Spring MVC Web Framework versus Struts
    By biguniverse in forum Web Flow
    Replies: 27
    Last Post: Aug 29th, 2012, 03:57 AM
  2. Spring managed Struts Actions
    By drc in forum Web
    Replies: 2
    Last Post: Jul 14th, 2005, 10:46 AM
  3. migrating Struts to Spring (long-winded)
    By ocampesato in forum Web
    Replies: 2
    Last Post: May 6th, 2005, 11:17 AM
  4. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  5. Testing troubles with Struts / Spring
    By anthonyb in forum Web
    Replies: 4
    Last Post: Feb 10th, 2005, 03:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •