I've tried every example i could find and I can't get struts2 + sitemesh + freemarker to work on a simple jsp.
I have a very simple web.xml, a single action that just goes to index.jsp, and a simple .ftl decorator that just adds some text to the result.
When I hit index.action
, the page "seems" to be decorated, but I get the literal ${body}
instead of the actual contents.
Here's my setup:
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>struts2 test</description>
<display-name>struts 2 test</display-name>
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.apache.struts2.sitemesh.FreeMarkerPageFilter</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
开发者_开发百科 <filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.action</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<struts>
<constant name="struts.devMode" value="true"/>
<package name="basicstruts2" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
</package>
</struts>
sitemesh.xml
<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.DefaultPageParser"/>
<parser content-type="text/html" 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>
decorators.xml
<decorators defaultdir="/decorators">
<decorator name="main" page="main.ftl">
<pattern>/*</pattern>
</decorator>
</decorators>
main.ftl
<html>
<head>
<title>${title}</title>
${head}
</head>
<body>
I'm Fancy!<br>
${body}<br />
</body>
</html>
index.jsp
<html>
<head>
<title>my title</title>
</head>
<body>
my body
</body>
</html>
Any ideas???
See http://struts.apache.org/2.x/docs/sitemesh-plugin.html for working docs. You probably need to add:
<servlet-mapping>
<servlet-name>sitemesh-freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
to your web.xml
.
And rename index.jsp to index.ftl. Incidentally if you're going to go with sitemesh + freemarker I usually prefer the convention of *.dec
for the decorators and *.ftl
for the freemarker templates.
精彩评论