In this blog I explain how I’ve migrated from Struts Tiles to SiteMesh in a AppFuse 1.5 project. I wanted to do that because I was fedup of dealing with Tiles aliases and also because I’ve used SiteMesh in other projects and it’s really easy to use.
Metadata
filter-mappings.xml
In %ROOT%metadatawebfilter-mappings.xml add the folowing
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
<!-- These are needed by Tomcat 5 for forwards -->
<!--dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher-->
</filter-mapping>
filters.xml
And in the filters.xml file add.
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
struts-actions.xml
Change all the actions parameter from Tiles to path to JSP pages. For example, change from
<action path="/login" type="org.apache.struts.actions.ForwardAction"
parameter=".login"/>
to
<action path="/login" type="org.apache.struts.actions.ForwardAction"
parameter="/WEB-INF/pages/login.jsp"/>
struts-plugins.xml
In the %ROOT%metadatawebstruts-plugins.xml delete the reference to Tiles plugin
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config"
value="/WEB-INF/tiles-config.xml" />
<set-property property="moduleAware" value="true" />
<set-property property="definitions-parser-validate" value="true" />
</plug-in>
Action source code
The most painful job is to change all your xDoclet tags so that actions forward to a page and not a tile. So you have to change all action classes that are in %ROOT%srcwebmyappwebappaction for example, refering to your tiles-config.xml, change
@struts.action-forward name="architectures" path=".architectures"
to
@struts.action-forward name="architectures" path="/WEB-INF/pages/architectures.jsp"
The other problem that you may find is if you have used tiles.Controller. If yes, just change them to struts actions.
JSPs
First, change your %ROOT%webcommontaglibs.jsp file. Delete
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
and add
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
Layout to decorator
You have to copy and rename the Tiles layout from %ROOT%weblayoutsbaseLayout.jsp to the SiteMesh decorator %ROOT%webdecoratorsdefault.jsp. Once it’s done, change
<tiles:insert attribute="footer"/>
to
<c:import url="/common/footer.jsp"/>
If your baseLayout.jsp uses struts actions to display headers or footers, import the action. For example change
<tiles:insert attribute="leftMenu"/>
to
<c:import url="/loadLeftColumn.html"/>
WEB-INF
Change the viewResolver in %ROOT%webWEB-INFaction-servlet.xml from
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="requestContextAttribute">
<value>rc</value>
</property>
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles.TilesJstlView</value>
</property>
</bean>
to
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="requestContextAttribute" value="rc"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
Delete %ROOT%webWEB-INFapplicationContext-tiles.xml and tiles-config.xml. And instead add the new decorator.xml file:
<decorators defaultdir="/decorators">
<excludes>
<pattern>/demos/*</pattern>
<pattern>/resources/*</pattern>
</excludes>
<decorator name="default" page="default.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
As well as the sitemesh.xml file
<sitemesh>
<property name="decorators-file" value="/WEB-INF/decorators.xml"/>
<excludes file="${decorators-file}"/>
<page-parsers>
<parser default="true" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
<parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
<parser content-type="text/html;charset=ISO-8859-1" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
</page-parsers>
<decorator-mappers>
<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}"/>
</mapper>
</decorator-mappers>
</sitemesh>
The two error pages 403.jsp and 404.jsp should be replaced by the new AppFuse ones.
Last but not least, in the %ROOT%build.xml file add the sitemesh.jar into the package-web task. And of course add the sitemesh.jar into the %ROOT%lib directory and update lib.properties file.
Should be ok now. Good luck.