Struts2 + Sitemesh + Freemarker не работает - PullRequest
0 голосов
/ 11 марта 2010

Я перепробовал каждый пример, который смог найти, и я не могу заставить struts2 + sitemesh + freemarker работать с простым jsp.

У меня есть очень простой web.xml, одно действие, которое просто идет к index.jsp, и простой декоратор .ftl, который просто добавляет текст к результату.

Когда я нажимаю index.action, страница «кажется» оформленной, но я получаю буквальное значение ${body} вместо фактического содержимого.

Вот мои настройки:

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>

Есть идеи ???

1 Ответ

1 голос
/ 22 января 2011

См. http://struts.apache.org/2.x/docs/sitemesh-plugin.html для рабочих документов. Вам, вероятно, нужно добавить:

<servlet-mapping>
     <servlet-name>sitemesh-freemarker</servlet-name>
     <url-pattern>*.ftl</url-pattern>
</servlet-mapping>

к вашему web.xml.

И переименуйте index.jsp в index.ftl. Кстати, если вы собираетесь использовать sitemesh + freemarker, я обычно предпочитаю соглашение *.dec для декораторов и *.ftl для шаблонов freemarker.

...