0

見つけたすべての例を試しましたが、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>

何か案は???

4

1 に答える 1

1

作業ドキュメントについては、 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、デコレータとfreemarker*.ftlテンプレートの規則を好みます。

于 2011-01-22T17:35:49.863 に答える