1.导入Struts2的jar 和 sitemesh.jar 和 Struts2-sitemesh-plugin.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
sitemesh-2.4.2.jar
struts2-core-2.3.4.jar
struts2-sitemesh-plugin-2.3.4.1.jar
xwork-core-2.3.4.jar
2.在web.xml中配置 Struts sitemesh拦截器 注意有一定的顺序
3.在webroot下的descorators目录下建立 myDecorator.jsp 装饰器页面
4.在web-inf 下建立 装饰器描述文件 decorators.xml
5.建立 实体类 Song.java
6.action
7.配置 struts.xml
8.allSongs.jsp
9.访问localhost:8080/Struts2AndSiteMesh/songs/allSongs.action 可以看到 页面中加入
top 和bottom
如果将 decorations.xml中的pattern 改成 <pattern>/singer/*</pattern>
那上面的访问的页面就没有经过装饰器页面 修饰
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
sitemesh-2.4.2.jar
struts2-core-2.3.4.jar
struts2-sitemesh-plugin-2.3.4.1.jar
xwork-core-2.3.4.jar
2.在web.xml中配置 Struts sitemesh拦截器 注意有一定的顺序
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!--用来消除sitemesh 拦截器 Struts2拦截器的冲突 使之兼容--> <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class> </filter> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</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>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3.在webroot下的descorators目录下建立 myDecorator.jsp 装饰器页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>
<%@ taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title><decorator:title default="装饰器页面"/></title>
<decorator:head/>
</head>
<body>
<div>top</div>
<hr>
<div>
<decorator:body/>
</div>
<hr>
<div>bottom</div>
</body>
</html>
4.在web-inf 下建立 装饰器描述文件 decorators.xml
<?xml version="1.0" encoding="UTF-8"?> <decoratos defaultdir="/decorators"> <decorator name="myDecorator" page="myDecorator.jsp"> <pattern>/songs/*</pattern> </decorator> </decoratos>
5.建立 实体类 Song.java
private String songName;
private String singer;
private String songTime;
//get set
6.action
package com.sh.action;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.sh.entity.Song;
public class AllSongs extends ActionSupport {
private List<Song> allSongs=new ArrayList<Song>();
public List<Song> getAllSongs() {
return allSongs;
}
public void setAllSongs(List<Song> allSongs) {
this.allSongs = allSongs;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
Song song1=new Song();
song1.setSongName("浪子心声");
song1.setSinger("刘德华");
song1.setSongTime("3:41");
Song song2=new Song();
song2.setSongName("中国话");
song2.setSinger("SHE");
song2.setSongTime("4:18");
Song song3=new Song();
song3.setSongName("丁香花");
song3.setSinger("唐磊");
song3.setSongTime("4:05");
allSongs.add(song1);
allSongs.add(song2);
allSongs.add(song3);
return SUCCESS;
}
}
7.配置 struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.i18n.encoding" value="utf-8"/> <package name="default" extends="struts-default" namespace="/songs"> <action name="allSongs" class="com.sh.action.AllSongs"> <result>/allSongs.jsp</result> </action> </package> </struts>
8.allSongs.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Struts2AndStieMesh</title>
</head>
<body>
<center>
<br/>
<div>
<ul style="list-style: none">
<li style="float: left; width: 100px;">歌名</li>
<li style="float: left; width: 100px;">歌手</li>
<li style="float: left; width: 100px;">时长</li>
<div style="clear: both;"></div>
</ul>
<s:iterator value="allSongs">
<ul style="list-style:none">
<li style="float: left; width: 100px;"><s:property value="songName"/></li>
<li style="float: left; width: 100px;"><s:property value="singer"/></li>
<li style="float: left; width: 100px;"><s:property value="songTime"/></li>
<div style="clear: both;"></div>
</ul>
</s:iterator>
</div>
<br/>
</center>
</body>
</html>
9.访问localhost:8080/Struts2AndSiteMesh/songs/allSongs.action 可以看到 页面中加入
top 和bottom
如果将 decorations.xml中的pattern 改成 <pattern>/singer/*</pattern>
那上面的访问的页面就没有经过装饰器页面 修饰