标签:
SiteMesh是一个Java WEB项目的网页布局和修饰框架。
WEB-INF/web.xml文件
<web-app ......> ...... <filter> <filter-name>sitemeshFilter</filter-name> <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class> </filter> <filter-mapping> <filter-name>sitemeshFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
如果Web.xml中配置了taglib,jsp页面中引入taglib的方式
<web-app ...> ... <taglib> <taglib-uri>sitemesh-decorator</taglib-uri> <taglib-location>/WEB-INF/sitemesh-decorator.tld</taglib-location> </taglib> <taglib> <taglib-uri>sitemesh-page</taglib-uri> <taglib-location>/WEB-INF/sitemesh-page.tld</taglib-location> </taglib> ... </web-app>
<%@taglib uri="sitemesh-decorator"?prefix="decorator" %>
指定装饰模板与URL的对应关系,也可以配置那些URL不需要模板控制。
WEB-INF/decorators.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!-- Any urls that are excluded will never be decorated by Sitemesh --> <excludes> <pattern>/Login*</pattern> <patterm>/index.jsp*<pattern> </excludes> <decorators defaultdir="/WEB-INF/views"> <!--defaultdir属性为模板文件的存放路径--> <!-- 默认装饰模板配置, 在需要装饰的页面增加<meta name="decorator" content="default"/> --> <decorator name="main" page="layouts/main.jsp" > <pattern>/api/certs/*</pattern> <pattern>/api/provs/*</pattern> <pattern>/api/macs/*</pattern> </decorator> <decorator name="panel" page="layouts/panle.jsp"> </decorator> <!-- 下面可以写多个 --> </decorators>
decorator标签属性
使用SiteMesh最主要的工作就是创建装饰模板,然后在decorators.xml配置装饰模板应用于哪些页面URL。一般项目可以抽象出主模板,二级页面模板,三级页面模板,弹出窗口模板等,但数量往往不会超过8个。
WEB-INF/views/layouts/main.jsp
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %> <html> <head> <title> <decorator:title default="default title"/> </title> <decorator:head/> </head> <body <decorator:getProperty property=“body.onload" writeEntireProperty=“1"/> > <jsp:include page="/header.jsp"></jsp:include> ...... <decorator:body/> ...... 从meta中获取变量company的名称: <decorator:getProperty property=“meta.company”/> ...... <decorator:usePage id=“myPage" /> <%=myPage.getRequest().getAttribute(“username”)%> ...... <jsp:include page="/footer.jsp"></jsp:include> </body> </html>
Sitemesh标签
<decorator:head />
填充被装饰页面的head标签内容
<decorator:body />
填充被装饰页面的body标签内容
<decorator:title default="default title" />
填充被装饰页面的title标签内容,
<decorator:getProperty property="" default="" writeEntireProperty="{true|false|1|0}"/>
读取被装饰页面中的相关标签的属性值,writeEntrieProperty表示只显示"value",还是显示"prop=value"
<decorator:usePage id="" />
<%=myPage.getRequest().getAttribute(“username”)%>
将被装饰页面构造为一个对象,可以在装饰页面的JSP中直接引用。
<html lang=“en”> <head> <title>我的sitemesh</title> <meta name="decorator" content="default"/> <meta name=“company” content=“smartdot”/> <meta name=“Author” content=“zhangsan”/> <script> function count(){return 10;} </script> </head> <body onload=“count()”> <p>这是一个被修饰页面</p> </body> </html>
在装饰模板中和被包装页面中都可以主动应用装饰器。使用的标签为applyDecorator或apply-decorator,可以内嵌param标签提供参数,装饰模板中用getProperty标签可以读取param提供的参数值。
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%> ...... <body> <page:applyDecorator page="/common/top.jsp" name="panel"> <page:param name="paramName"> ...... </page:param> </page:applyDecorator> ...... </body>
panel装饰模板中配置:
<div class="...."> <decorator:body/> top.jsp的body <decorator:getProperty property="paramName"/> </div>
applyDecorator属性
http://my.oschina.net/georgele/blog/49137?p=1
http://my.oschina.net/s2jh/blog/361044
http://m.blog.csdn.net/blog/kpchen_0508/41281749
http://blog.csdn.net/jzh440/article/details/7770013
标签:
原文地址:http://www.cnblogs.com/pixy/p/4868282.html