Struts2整合SiteMesh

http://takeme.iteye.com/blog/1716488

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>  


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>  


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>  


5.建立 实体类 Song.java 
Java代码   收藏代码
  1. private String songName;  
  2.     private String singer;  
  3.     private String songTime;  
  4. //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. }  


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>  


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>  



9.访问localhost:8080/Struts2AndSiteMesh/songs/allSongs.action 可以看到 页面中加入 
  top 和bottom 
  如果将 decorations.xml中的pattern 改成 <pattern>/singer/*</pattern> 
  那上面的访问的页面就没有经过装饰器页面 修饰 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值