Combining Struts and Spring MVC

If you are migrating an existing Struts application to Spring MVC, you may find yourself in a situation in which you have a Struts action that you need to forward to a Spring MVC controller implementation. This is quite easy to implement: all you need to do is create a Spring MVC controller and a Struts action that forwards to that controller's URL. If you create these, you will be able to pass data from your Struts actions to Spring controllers, which means that you will be able to use Spring views to display the output of Struts actions.

Note 

This is a very clumsy way to use Spring views from Struts actions. Use this approach only if your Struts actions perform large amount of processing that are too difficult or too time-consuming to implement in Spring MVC. This is just a temporary solution.

To demonstrate how to pass data from Struts actions to the Spring MVC infrastructure, let's create the IndexController class as a subclass of the MultiActionController and define the mapping for its handleIndex() method in the Spring application context file. The handleIndex() method returns a ModelAndView instance with the view name set to .index, and the model contains a list of all Products. Listing 19-14 shows the Spring bean configuration for the IndexController.

Listing 19-14: IndexController Beans

image from book
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ¿ "http://www.springframework.org/dtd/spring-beans.dtd">      <beans>     <bean bold">tilesConfigurer"          >         <property name="definitions">             <list>                 <value>/WEB-INF/tiles-layout.xml</value>             </list>         </property>     </bean>              <bean bold">viewResolver"          >         <property name="basename"><value>views</value></property>         </bean>          <bean bold">publicUrlMapping"          >         <property name="mappings">             <props>                 <prop key="/index.html">indexController</prop>             </props>         </property>     </bean>          <!-- Index -->     <bean bold">indexController"          >         <property name="productManager"><ref bean="productManager"/></property>         <property name="methodNameResolver">             <ref local="indexMethodNameResolver"/></property>     </bean>          <bean bold">indexMethodNameResolver"          symbol">¿             PropertiesMethodNameResolver">         <property name="mappings">             <props>                 <prop key="/index.html">handleIndex</prop>             </props>         </property>     </bean>      </beans> 
image from book

The definitions in Listing 19-14 are nothing out of the ordinary; however, it is important to notice that we had to define the tilesConfigurer in Spring, even though we already configured it in Struts. Next, we declare the usual viewResolver, publicUrlMapping, indexController, and indexMethodNameResolver beans. To test that the IndexController is working, you need to make a request to /index.html; you should see a page that looks exactly like the page generated by the /sindex.do action, which is shown in Listing 19-3.

Next, we create a Struts ForwardAction that simply forwards to the success forward, which passes the control to the IndexController, which, in turn, displays the .index tile. Listing 19-15 shows the implementation of the ForwardAction.

Listing 19-15: ForwardAction Implementation

image from book
package com.apress.prospring.ch19.web.actions; // imports omitted public class ForwardAction extends Action {          public final ActionForward execute(ActionMapping mapping, ActionForm form,          HttpServletRequest request, HttpServletResponse response)          throws Exception {         request.setAttribute("fromStruts", "Hello");         return mapping.findForward("success");     }      }
image from book

We now need to add the action definition to the Struts configuration file, as shown in Listing 19-16.

Listing 19-16: Struts Configuration File

image from book
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE struts-config PUBLIC     "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"     "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">      <struts-config>     <action-mappings>         <!-- other actions as usual -->         <action path="/fwd"             type="com.apress.prospring.ch19.web.actions.ForwardAction"              validate="false">             <forward name="success" path="/index.html"/>         </action>     </action-mappings>     <!-- the rest of the file omitted --> </struts-config> 
image from book

When we make a request to /fwd.do, the ForwardAction.execute() method runs and forwards to the /index.html, which is handled by the IndexController. As a result, you see the same output as is generated by the IndexController.handleIndex() method, whose implementation is shown in Listing 19-17.

Listing 19-17: IndexController.handleIndex Implementation

image from book
package com.apress.prospring.ch19.web;      public class IndexController extends MultiActionController {          private ProductManager productManager;          public ModelAndView handleIndex(HttpServletRequest request,          HttpServletResponse response) throws Exception {         Map<String, Object> model = new HashMap<String, Object>();         model.put("products", productManager.findAll());         model.put("fromStruts", request.getAttribute("fromStruts"));                  return new ModelAndView("index", model);     }          public final void setProductManager(ProductManager productManager) {         this.productManager = productManager;     } }
image from book

As you can see, in the handleIndex() method, we added the fromStruts request parameter to the model Map, thus enabling any Spring view to access it. This is a quick and dirty solution if you need to display the data generated by a Struts action in a Spring view.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net