`

sitemesh 项目中的使用

 
阅读更多

sitemesh 项目中的使用

1. 加入lib/sitemesh-2.4.2.jar

2.在web.xml 文件中加过滤器

<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>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
</filter-mapping>

3.从sitemesh-2.4.2.jar 中拷出sitemesh-default.xml 复制到工程的WEB-INF目录下,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>

注意:sitemesh主要用于装饰处理信息,对页面的解析和装饰器配置等等,一般采用默认的即可

配置文件中page-parser 请采用sitemesh.parser.HTMLPageParser 可以解决页面中文乱码问题

4.添加装饰模板decorators.xml配置 文件到WEB-INF  该文件主要用于sitemesh的配置规则。

主要有两个节点:exclude:指定了节点请求不适用任何模板

    decorator:指定了模板的位置和文件名,通过pattern指定那些url使用那些模板

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/WEB-INF/jsp/decorators">     //指定了模板文件存放的目录
    <excludes>

<pattern>/scripts/*</pattern>   //排除的连接

    </excludes>

    <decorator name="store" page="store_main.jsp">  //指定使用的模板
        <pattern>/omsStore/*</pattern>       //使用装饰的连接
        <pattern>/omsCustomer/*</pattern>
    </decorator>

5.模板页面的定义

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="sec"
    uri="http://www.springframework.org/security/tags"%>
<%@taglib uri="http://www.springframework.org/security/tags"
    prefix="security"%>
<%@taglib uri="http://www.opensymphony.com/sitemesh/decorator"
    prefix="decorator"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title><decorator:title default="********" /> </title>
        
        <link href="<c:url value="/styles/jquery-ui/jquery-ui.css" />"rel="stylesheet" type="text/css" />
        <link href="<c:url value="/styles/tools.css" />"rel="stylesheet" type="text/css" />
        <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/styles/list.css"></link>
                <link rel="stylesheet" type="text/css"
            href="${pageContext.request.contextPath}/styles/jquery.lightTreeview.css"></link>

        <script type="text/javascript"
            src="${pageContext.request.contextPath}/scripts/jquery.lightTreeview.pack.js">
</script>
        <script type="text/javascript"
            src="${pageContext.request.contextPath}/scripts/jquery.js"></script>
        <script type="text/javascript"
            src="${pageContext.request.contextPath}/scripts/pglist.js"></script>
        <script type="text/javascript"
            src="${pageContext.request.contextPath}/scripts/selectOperate.js"></script>
        <script type="text/javascript"
            src="${pageContext.request.contextPath}/scripts/jquery.tools.min.js"></script>
            <decorator:head />
    </head>

    <body>
                <div class="tableBox">
                    <decorator:body />
                </div>
    </body>
</html>

6.总结:1.模板页得作用主要是将功能页中通用的、与业务功能不相关的代码进行分离,统一管理,从而达到代码的重用,如页面布局,css,js等等。

  2.凡是能经过过滤器请求的页面,都可以使用上面的js,css等,页面自身的内容放到<decorator:body />

  3.<decorator:head /> 读取装饰器页面head的内容

  4.<decorator:title/> 读取装饰器页面title的内容

7.常见的问题:

1.在运行过程中无法显示模板页,查看配置的路径(pattern),查看web.xml文件Filter是否采用sitemesh.filter.pageFilter

2.无法显示功能页,只能显示模板页,查看<decorator:body /> 的位置

3.在web logic 中出现了中文乱码,查看sitemesh.xml文件 <page-parsers> 是否采用sitemesh.parser.HTMLPageParser

8.标签:

被用于建立装饰器页面.

<decorator:head />
<decorator:body />
<decorator:title />
<decorator:getProperty />
<decorator:usePage />

被用于从原始内容页面访问装饰器.

<page:applyDecorator />

<page:param />

9.性能问题:

使用sitemesh和不使用sitemesh对于虚拟机堆内存的使用差异不太大,但 是在GC次数上的差异很大。从GC日志的详细信息可以看出,在使用sitemesh时,发生次要GC(Minor GC)的频率非常的高,可以推断在运行时期产生了大量的短生命周期的对象,然后又迅速的被释放,GC在新生代就已经完成了。主要GC(Major GC, Full GC)在使用sitemesh和不使用的情况下,均发生了1次,而且这1次主要GC也是在resin启动中发生的,不是应用在进行压力测试时发生的。由于 使用sitemesh时的GC次数远远大于不使用sitemesh,所以在整个测试过程中,GC上消耗的时间也是差异非常大的。

不使用sitemesh时完成的请求数是使用sitemesh时的144.3%。同时页面响应时间也仅为使用sitemesh时的69.0%。

从上述测试数据来分析,使用sitemesh对于系统性能是有较大的影响的,主要表现在GC的次数会显著增多。建议在大压力、页面内容大的系统中,慎重选择sitemesh,并且使用之前对其带来的性能影响进行一个较为合理、全面的评估。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics