Announcement Announcement Module
Collapse
No announcement yet.
Struts ForwardAction counterpart in Spring MVC? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        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

        Comment


        • #5
          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

          Comment


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

            Comment


            • #7
              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.)

              Comment


              • #8
                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

                Comment


                • #9
                  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>

                  Comment

                  Working...
                  X