日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Struts2整合SiteMesh

發(fā)布時(shí)間:2024/4/17 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2整合SiteMesh 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

1.導(dǎo)入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代碼??
  • <?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">??
  • ??
  • <!--用來(lái)消除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 裝飾器頁(yè)面?
    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="裝飾器頁(yè)面"/></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代碼??
  • <?xml?version="1.0"?encoding="UTF-8"?>??
  • <decoratos?defaultdir="/decorators">??
  • ????<decorator?name="myDecorator"?page="myDecorator.jsp">??
  • ????????<pattern>/songs/*</pattern>??
  • ????</decorator>??
  • </decoratos>??


  • 5.建立 實(shí)體類(lèi) Song.java?
    Java代碼??
  • private?String?songName;??
  • ????private?String?singer;??
  • ????private?String?songTime;??
  • //get?set??


  • 6.action?
    Java代碼??
  • 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("中國(guó)話");??
  • ????????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代碼??
  • <?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代碼??
  • <%@?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;">時(shí)長(zhǎng)</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.訪問(wèn)localhost:8080/Struts2AndSiteMesh/songs/allSongs.action 可以看到 頁(yè)面中加入?
    ? top 和bottom?
    ? 如果將 decorations.xml中的pattern 改成 <pattern>/singer/*</pattern>?
    ? 那上面的訪問(wèn)的頁(yè)面就沒(méi)有經(jīng)過(guò)裝飾器頁(yè)面 修飾?

    與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖

    總結(jié)

    以上是生活随笔為你收集整理的Struts2整合SiteMesh的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。