现在的位置: 首页 > 综合 > 正文

SiteMesh实战

2019年06月20日 ⁄ 综合 ⁄ 共 3629字 ⁄ 字号 评论关闭

更多详细使用方法,可以查看demo
需要到http://www.opensymphony.com/sitemesh/下载Jar及demo(取其中的两个tld以及sitemesh.xml)
web.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">
    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <jsp-config>
        <taglib>
            <taglib-uri>sitemesh-page</taglib-uri>
            <taglib-location>/WEB-INF/sitemesh-page.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>sitemesh-decorator</taglib-uri>
            <taglib-location>/WEB-INF/sitemesh-decorator.tld</taglib-location>
        </taglib>
    </jsp-config>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

sitemesh.xml:

<sitemesh>
    <property name="decorators-file" value="/WEB-INF/decorators.xml"/>
    <excludes file="${decorators-file}"/>

    <page-parsers>
        <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
    </page-parsers>

    <decorator-mappers>

        <mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
            <param name="property.1" value="meta.decorator" />
            <param name="property.2" value="decorator" />
        </mapper>

        <mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper">
        </mapper>

        <mapper class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper">
            <param name="match.MSIE" value="ie" />
            <param name="match.Mozilla [" value="ns" />
            <param name="match.Opera" value="opera" />
            <param name="match.Lynx" value="lynx" />
        </mapper>

        <mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
            <param name="decorator" value="printable" />
            <param name="parameter.name" value="printable" />
            <param name="parameter.value" value="true" />
        </mapper>

        <mapper class="com.opensymphony.module.sitemesh.mapper.RobotDecoratorMapper">
            <param name="decorator" value="robot" />
        </mapper>

        <mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
            <param name="decorator.parameter" value="decorator" />
            <param name="parameter.name" value="confirm" />
            <param name="parameter.value" value="true" />
        </mapper>

        <mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper">
        </mapper>

        <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
            <param name="config" value="${decorators-file}" />
        </mapper>

    </decorator-mappers>

</sitemesh>

decorators.xml:

<?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/decorators">
    <!-- Any urls that are excluded will never be decorated by Sitemesh -->
    <excludes>
        <pattern>/index.jsp*</pattern>
          <pattern>/login/*</pattern>
    </excludes>
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>

main.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="sitemesh-decorator" prefix="decorator"%>
<%
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>
    
    <title><decorator:title default="title Of main.jsp"></decorator:title></title>
    <decorator:head></decorator:head>
  </head>
 
  <body
      <decorator:getProperty property="body.onload" writeEntireProperty="true"></decorator:getProperty>
      <decorator:getProperty property="body.style" writeEntireProperty="true"></decorator:getProperty>
      <decorator:getProperty property="body.id" writeEntireProperty="true"></decorator:getProperty>
  >
    This is my main JSP page. <br>
    <decorator:body></decorator:body>
  </body>
</html>

抱歉!评论已关闭.