Using Other Views

Unfortunately, you cannot use Spring views in your Struts actions simply because the Struts Action subclasses are not Spring-managed beans and because the execute() methods must return an instance of ActionForward rather than ModelAndView. If you want to take advantage of Tiles, for example, you must configure the Tiles support in the Struts configuration files. Declaring the tilesConfigurer bean in Spring application context has no effect on the Struts actions. And if you attempt to set the forward to a tile definition, most likely you end up in an IllegalArgumentException with a message saying that Path .index does not start with a slash (/) character being thrown.

If you wish to use Tiles, remove the tilesConfigurer bean from the Spring application context file and replace it with a Struts plugin definition, as shown in Listing 19-9.

Listing 19-9: Struts Plugin Definition

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>         <action path="/index"              type="com.apress.prospring.ch19.web.actions.IndexAction"              validate="false">             <forward name="success" path=".index"/>         </action>     </action-mappings>          <plug-in className="org.apache.struts.tiles.TilesPlugin" >         <set-property property="definitions-config"              value="/WEB-INF/tiles-layout.xml" />         <set-property property="moduleAware" value="true" />         <set-property property="definitions-parser-validate" value="true" />         <set-property property="definitions-debug" value="2" />     </plug-in> </struts-config> 
image from book

The tiles-layout.xml and the JSP pages for the individual tiles are a copy of the files discussed in Figure 19-2.

image from book
Figure 19-2: .index tile displayed from a Struts action

Note that you need to keep the tilesConfigurer bean definition in place if you are slowly migrating the Struts actions to Spring controllers. In this case, you need to fully configure the Spring web framework as well as the Struts framework. You should only use this approach in situations in which you need to support the legacy Struts code. Do not mix Struts and Spring MVC in a new application. The reasons are obvious: first, if you use both the Spring MVC and Struts, you are wasting the server resources, and then by having to maintain two sets of configuration files, you are unnecessarily increasing the complexity of your application.

The same rules we discussed here apply to any other view technologies that are used in a Struts application.



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

Similar book on Amazon

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