当前位置:编程学习 > JSP >>

使用sitemesh建立复合视图 - 3.其他讨论

前面的文章已经足以应用sitemesh来改善您的应用,但我发现还有一些其他的东东可能也会对大家有所帮助
可打印的界面装饰
前面说过有1种可打印的装饰器,可以允许你当用http://localhost/aaa/a.html?printable=true方式访问时,应用其他的装饰器(自己指定),给出原始页面以供打印(免得把header,footer等的花哨的图片也搭上)。
让我们来看一看怎样实现他:
1.首先在WEB-INFO/sitemesh.xml中设置:
  <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>
这样就可以通过?printable=true来使用名为printable的装饰器,而不是用原来的装饰器。
2.在WEB-INFO/decorators.xml中定义相应的printable装饰器
  <decorator name="printable" page="printable.jsp"/>
3.最后编写printable装饰器/decorators/printable.jsp
<%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
<html>
<head>
  <title><decorator:title /></title>
  <decorator:head />
</head>
<body>
  <h1><decorator:title /></h1>
  <p align="right"><i>(printable version)</i></p>
  <decorator:body />
</body>
</html>
这样就可以让一个原始页面通过?printable=true开关来切换不同的装饰器页面。
 
中文问题
由于sitemesh内部所使用的缺省字符集为iso-8859-1,直接使用会产生乱码,我们可以通过以下方法纠正之:
方法1:可以在您所用的application server的配置文件中找一找,有没有设置encoding或charset的项目,然后设成gbk或gb2312即可
方法2:这也是我们一直使用的方法。
1.在每一个jsp页里设置: <%@ page contentType="text/html; charset=gbk"%> 来告诉server你所要求的字符集。
2.在每个jsp页的head中定义:<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=gbk"> 来告诉浏览器你所用的字符集。
总结:使用sitemesh最通常的途径:
1.配置好环境,
2.在WEB-INFO/decroators.xml中描述你将建立的包装器。
3.开发在decroators.xml中描述的包装器,最好存放在/_decorators目录下
4.ok ,可以看看辛勤的成果了 :)
补充:Jsp教程,J2EE/EJB/服务器 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,