Struts2整合SiteMesh

本文详细介绍了如何在Struts2框架中集成并使用SiteMesh进行页面装饰,包括配置、装饰器的创建与应用,以及相关实体类、Action、JSP模板的实现与整合,最终展示了一个完整的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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拦截器 注意有一定的顺序

Xml代码 复制代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appversion="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <!--用来消除sitemesh 拦截器 Struts2拦截器的冲突 使之兼容-->
  8. <filter>
  9. <filter-name>struts-cleanup</filter-name>
  10. <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
  11. </filter>
  12. <filter>
  13. <filter-name>sitemesh</filter-name>
  14. <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
  15. </filter>
  16. <filter>
  17. <filter-name>struts2</filter-name>
  18. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  19. </filter>
  20. <filter-mapping>
  21. <filter-name>struts-cleanup</filter-name>
  22. <url-pattern>/*</url-pattern>
  23. </filter-mapping>
  24. <filter-mapping>
  25. <filter-name>sitemesh</filter-name>
  26. <url-pattern>/*</url-pattern>
  27. </filter-mapping>
  28. <filter-mapping>
  29. <filter-name>struts2</filter-name>
  30. <url-pattern>/*</url-pattern>
  31. </filter-mapping>
  32. <welcome-file-list>
  33. <welcome-file>index.jsp</welcome-file>
  34. </welcome-file-list>
  35. </web-app>
<?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 装饰器页面

Html代码 复制代码 收藏代码
  1. <%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="decorator"uri="http://www.opensymphony.com/sitemesh/decorator" %>
  3. <%@ taglib prefix="page"uri="http://www.opensymphony.com/sitemesh/page" %>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. %>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <basehref="<%=basePath%>">
  12. <title><decorator:titledefault="装饰器页面"/></title>
  13. <decorator:head/>
  14. </head>
  15. <body>
  16. <div>top</div>
  17. <hr>
  18. <div>
  19. <decorator:body/>
  20. </div>
  21. <hr>
  22. <div>bottom</div>
  23. </body>
  24. </html>
<%@ 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代码 复制代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <decoratosdefaultdir="/decorators">
  3. <decoratorname="myDecorator"page="myDecorator.jsp">
  4. <pattern>/songs/*</pattern>
  5. </decorator>
  6. </decoratos>
<?xml version="1.0" encoding="UTF-8"?>
<decoratos defaultdir="/decorators">
	<decorator name="myDecorator" page="myDecorator.jsp">
		<pattern>/songs/*</pattern>
	</decorator>
</decoratos>



5.建立 实体类 Song.java

Java代码 复制代码 收藏代码
  1. private String songName;
  2. private String singer;
  3. private String songTime;
  4. //get set
private String songName;
	private String singer;
	private String songTime;
//get set



6.action

Java代码 复制代码 收藏代码
  1. package com.sh.action;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.opensymphony.xwork2.ActionSupport;
  5. import com.sh.entity.Song;
  6. publicclass AllSongs extends ActionSupport {
  7. private List<Song> allSongs=new ArrayList<Song>();
  8. public List<Song> getAllSongs() {
  9. return allSongs;
  10. }
  11. publicvoid setAllSongs(List<Song> allSongs) {
  12. this.allSongs = allSongs;
  13. }
  14. @Override
  15. public String execute() throws Exception {
  16. // TODO Auto-generated method stub
  17. Song song1=new Song();
  18. song1.setSongName("浪子心声");
  19. song1.setSinger("刘德华");
  20. song1.setSongTime("3:41");
  21. Song song2=new Song();
  22. song2.setSongName("中国话");
  23. song2.setSinger("SHE");
  24. song2.setSongTime("4:18");
  25. Song song3=new Song();
  26. song3.setSongName("丁香花");
  27. song3.setSinger("唐磊");
  28. song3.setSongTime("4:05");
  29. allSongs.add(song1);
  30. allSongs.add(song2);
  31. allSongs.add(song3);
  32. return SUCCESS;
  33. }
  34. }
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代码 复制代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <constantname="struts.i18n.encoding"value="utf-8"/>
  7. <packagename="default"extends="struts-default"namespace="/songs">
  8. <actionname="allSongs"class="com.sh.action.AllSongs">
  9. <result>/allSongs.jsp</result>
  10. </action>
  11. </package>
  12. </struts>
<?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

Html代码 复制代码 收藏代码
  1. <%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <%@ taglib uri="/struts-tags"prefix="s" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <basehref="<%=basePath%>">
  11. <title>Struts2AndStieMesh</title>
  12. </head>
  13. <body>
  14. <center>
  15. <br/>
  16. <div>
  17. <ulstyle="list-style: none">
  18. <listyle="float: left; width: 100px;">歌名</li>
  19. <listyle="float: left; width: 100px;">歌手</li>
  20. <listyle="float: left; width: 100px;">时长</li>
  21. <divstyle="clear: both;"></div>
  22. </ul>
  23. <s:iteratorvalue="allSongs">
  24. <ulstyle="list-style:none">
  25. <listyle="float: left; width: 100px;"><s:propertyvalue="songName"/></li>
  26. <listyle="float: left; width: 100px;"><s:propertyvalue="singer"/></li>
  27. <listyle="float: left; width: 100px;"><s:propertyvalue="songTime"/></li>
  28. <divstyle="clear: both;"></div>
  29. </ul>
  30. </s:iterator>
  31. </div>
  32. <br/>
  33. </center>
  34. </body>
  35. </html>
<%@ 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>
那上面的访问的页面就没有经过装饰器页面 修饰

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值