整合Struts2.1.6 + Sitemesh2.4.1 + Freemarker 2.3.16

1. 首先 这些jar包都去官方 下载好,包括源代码.
另外还需要struts2-sitemesh-plugin-2.1.6.jar

 

2. WEB-INF/web.xml 主要设置

评述:这个filter的顺序是有关系的. StrutsPrepareFilter -> FreeMarkerPageFilter -> StrutsExecuteFilter

如果不配置sitemesh-freemarker, freemarker文件就不会被解析.

 

 

	<!-- SiteMesh FreeMarker -->
	<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>struts-execute</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>
	</filter-mapping>

	<filter-mapping>
		<filter-name>sitemesh</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<filter-mapping>
		<filter-name>struts-execute</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<servlet>
		<servlet-name>sitemesh-freemarker</servlet-name>
		<servlet-class>
			com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet</servlet-class>
		<init-param>
			<param-name>TemplatePath</param-name>
			<param-value>/</param-value>
		</init-param>
		<init-param>
			<param-name>default_encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

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

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

 

3.  WEB-INF/decorators.xml

<?xml version="1.0" encoding="UTF-8"?>

<decorators defaultdir="/decorators">
    <!-- Any urls that are excluded will never be decorated by Sitemesh -->
    <excludes>
        <pattern>/exclude.jsp</pattern>
    </excludes>

	<decorator name="index" page="index.ftl">
		<pattern>/*</pattern>
	</decorator>

</decorators>

4. WEB-INF/classes/struts.xml 主要配置

    <action name="test_*" method="{1}" class="com.starcite.action.TestAction">
      <result name="input" type="freemarker">/freemarker/register.ftl</result>
      <result name="success" type="freemarker">/freemarker/success.ftl</result>
      <result name="error" type="freemarker">/freemarker/error.ftl</result>
    </action>

 5. com.starcite.action.TestAction.java

package com.starcite.action;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class TestAction extends ActionSupport {

    public String input() {
        return INPUT;
    }
    
    public String error() {
        return ERROR;
    }
    
    public String success() {
        return SUCCESS;
    }
}

  6. decorators/index.ftl

<html>
	<head>
		<title>Test - ${title}</title>
		${head}
	</head>
	<body>
		<#include "/decorators/header.ftl">
		<hr noshade="noshade" size="1"/>
		${body}
		<hr noshade="noshade" size="1"/>
		<#include "/decorators/footer.ftl">
	</body>
</html>

 7. decorators/header.ftl
自己随便写点东西.

 8. decorators/footer.ftl
自己随便写点东西.

9. freemarker/success.ftl

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title><@s.text name="success"/></title>
  </head>
  <body>
    <@s.text name="success.info"/>
  </body>
</html>

  freemarker/error.ftl, freemarker/register.ftl 略

 

 

 

你可能感兴趣的:(apache,freemarker,Web,servlet,struts)