Struts2整合SiteMesh

Struts2整合SiteMesh          

博客分类:

    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. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="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.   
  8. <!--用来消除sitemesh 拦截器 Struts2拦截器的冲突 使之兼容-->      
  9. <filter>  
  10.     <filter-name>struts-cleanup</filter-name>  
  11.     <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>  
  12. </filter>  
  13.   
  14. <filter>  
  15.     <filter-name>sitemesh</filter-name>  
  16.     <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>  
  17. </filter>  
  18.       
  19. <filter>  
  20.     <filter-name>struts2</filter-name>  
  21.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  22. </filter>  
  23.   
  24. <filter-mapping>  
  25.     <filter-name>struts-cleanup</filter-name>  
  26.     <url-pattern>/*</url-pattern>  
  27. </filter-mapping>  
  28.   
  29. <filter-mapping>  
  30.     <filter-name>sitemesh</filter-name>  
  31.     <url-pattern>/*</url-pattern>  
  32. </filter-mapping>  
  33.   
  34. <filter-mapping>  
  35.     <filter-name>struts2</filter-name>  
  36.     <url-pattern>/*</url-pattern>  
  37. </filter-mapping>  
  38.   
  39.   
  40.   <welcome-file-list>  
  41.     <welcome-file>index.jsp</welcome-file>  
  42.   </welcome-file-list>  
  43.     
  44. </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.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.   <head>  
  12.     <base href="<%=basePath%>">  
  13.       
  14.     <title><decorator:title default="装饰器页面"/></title>  
  15.       
  16.     <decorator:head/>  
  17.   
  18.   </head>  
  19.     
  20.   <body>  
  21.     <div>top</div>  
  22.     <hr>  
  23.     <div>  
  24.         <decorator:body/>  
  25.     </div>  
  26.     <hr>  
  27.     <div>bottom</div>  
  28.   </body>  
  29. </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. <?xml version="1.0" encoding="UTF-8"?>  
  2. <decoratos defaultdir="/decorators">  
  3.     <decorator name="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.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.opensymphony.xwork2.ActionSupport;  
  7. import com.sh.entity.Song;  
  8.   
  9. public class AllSongs extends ActionSupport {  
  10.     private List<Song> allSongs=new ArrayList<Song>();  
  11.   
  12.     public List<Song> getAllSongs() {  
  13.         return allSongs;  
  14.     }  
  15.   
  16.     public void setAllSongs(List<Song> allSongs) {  
  17.         this.allSongs = allSongs;  
  18.     }  
  19.   
  20.     @Override  
  21.     public String execute() throws Exception {  
  22.         // TODO Auto-generated method stub  
  23.         Song song1=new Song();  
  24.         song1.setSongName("浪子心声");  
  25.         song1.setSinger("刘德华");  
  26.         song1.setSongTime("3:41");  
  27.           
  28.         Song song2=new Song();  
  29.         song2.setSongName("中国话");  
  30.         song2.setSinger("SHE");  
  31.         song2.setSongTime("4:18");  
  32.           
  33.   
  34.         Song song3=new Song();  
  35.         song3.setSongName("丁香花");  
  36.         song3.setSinger("唐磊");  
  37.         song3.setSongTime("4:05");  
  38.           
  39.         allSongs.add(song1);  
  40.         allSongs.add(song2);  
  41.         allSongs.add(song3);  
  42.           
  43.         return SUCCESS;  
  44.     }     
  45. }  
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. <?xml version="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.   
  6. <struts>   
  7.    <constant name="struts.i18n.encoding" value="utf-8"/>  
  8.    <package name="default" extends="struts-default" namespace="/songs">  
  9.         <action name="allSongs" class="com.sh.action.AllSongs">  
  10.             <result>/allSongs.jsp</result>  
  11.         </action>  
  12.    </package>  
  13. </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.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>Struts2AndStieMesh</title>  
  14.   
  15.   
  16.   </head>  
  17.     
  18.   <body>  
  19.         <center>  
  20.             <br/>  
  21.             <div>  
  22.                 <ul style="list-style: none">  
  23.                     <li style="float: left; width: 100px;">歌名</li>  
  24.                     <li style="float: left; width: 100px;">歌手</li>  
  25.                     <li style="float: left; width: 100px;">时长</li>  
  26.                     <div style="clear: both;"></div>  
  27.                 </ul>  
  28.                 <s:iterator value="allSongs">  
  29.                     <ul style="list-style:none">  
  30.                         <li style="float: left; width: 100px;"><s:property value="songName"/></li>  
  31.                         <li style="float: left; width: 100px;"><s:property value="singer"/></li>  
  32.                         <li style="float: left; width: 100px;"><s:property value="songTime"/></li>  
  33.                         <div style="clear: both;"></div>  
  34.                     </ul>  
  35.                 </s:iterator>  
  36.             </div>  
  37.             <br/>  
  38.         </center>  
  39.   </body>  
  40. </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>   那上面的访问的页面就没有经过装饰器页面 修饰

 

posted @ 2017-07-13 10:34  sky20080101  阅读(158)  评论(0)    收藏  举报