springspring-mvcstruts2action-mapping

Spring MVC equivalent of Action Mapping in Struts 2


Is there an equivalient/similar action mapping xml configuration available for SpringMVC just as there is for Struts 2?

e.g. in a Struts2 app I have this:

<action name="index" class="homeAction">
    <interceptor-ref name="myAppVarsBasic" />
    <interceptor-ref name="defaultStack" />
    <result name="input">/WEB-INF/jsp16/home.jsp</result>
    <result name="userDisplay">/WEB-INF/jsp16/userDisplay.jsp</result>
    <result name="changePassword" type="redirectAction" >passwordReset!changePass.do</result>
</action>

Is there a similar way to configure action mappings, redirects and to where with Spring MVC using xml config?

I'm teaching myself (with help of SO and Google) S2 to SpringMVC but find most resources seem to handle redirect directly from the action (or is that Controller in MVC?) e.g.:

return "redirect:index";

Solution

  • Is there an equivalient/similar action mapping xml configuration available for SpringMVC just as there is for Struts 2?

    Spring MVC has xml based configuration, annotations, and JavaConfig. It has several namespaces in their xml config files. The most usable is beans namespace. There you can define your beans configuration. You can define a path in the bean's name attribute or use @RequestMapping annotation.

    When Struts2 is integrated with Spring you can configure actions in struts.xml and define beans for action classes in the applicationContext.xml. Instead of class in the action config you just place a bean's id.

    In Spring MVC you can map request path to a method with @RequestMapping annotation. It's similar like Struts1 mapped their actions. In the Spring MVC 4 It has also support for wildcards and regex to match the request path.

    Spring has also interceptors but they are just pointcuts in the AOP namespace. Of course you can use them in Spring config.

    Results in Struts2 are just another actions that executes when the initial action ends. Spring MVC returns a View object with the Model. It can return a string, or another object, or nothing. The result in Spring MVC is what actually requested method returns. The result is handled by the view resolver. Spring MVC has many view resolves that you can configure as beans that are capable to handle a servlet dispatcher's request.


    Is there a similar way to configure action mappings, redirects and to where with Spring MVC using xml config?

    One of them is XmlViewResolver that is similar to the Struts 2 action config. You can read more abour these beans on the Spring site. Using this resolver you define views via views.xml that you feed to this resolver as property and use the view names mapped to each JSP.