繼續使用上一章http://www.cnblogs.com/EasonJim/p/7086916.html的例子,改形成使用ParameterDecoratorMapper映射器的方法,這個映射器不須要經過匹配URL和在頁面上寫模板路徑去實現,只須要經過URL上傳入指定參數便可。html
修改配置以下:java
一、sitemesh.xml引入ParameterDecoratorMapper映射器git
<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" /> <!-- 注意這一行指定<meta/>標籤的 name 屬性做爲PageDecorator的識別符號 --> <param name="property.3" value="meta.theme" /> </mapper> <mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper"/> <mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper"/> <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.FileDecoratorMapper"/> <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper"> <param name="config" value="${decorators-file}" /> </mapper> </decorator-mappers> </sitemesh>
二、新建test2.jsp頁面,這個頁面無需寫任何模板github
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title></title> </head> <body> <h1>Test2</h1> </body> </html>
三、訪問URL變成http://localhost:8080/test1/test2.jsp?decorator=basic-themeweb
而若是URL沒有參數,那麼會變成原始頁面api
四、爲URL後面的參數指定自定義的參數名,修改sitemesh.xml中映射器的寫法app
<mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper"> <param name="decorator.parameter" value="theme"/> </mapper>
訪問URL變成:http://localhost:8080/test1/test2.jsp?theme=basic-themejsp
<mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper"> <param name="decorator.parameter" value="theme"/> <param name="parameter.name" value="confirm"/> <param name="parameter.value" value="true"/> </mapper>
增長了兩個參數,那麼訪問將變成訪問URI時:test2.jsp?theme=basic-theme&confirm=true將映射裝飾器basic-theme。那裏的URI是test2.jsp?theme=basic-theme和test2.jsp?theme=basic-theme&confirm=false不會返回任何裝飾器。測試
也就是說在訪問時增長確認參數。ui
測試工程:https://github.com/easonjim/5_java_example/tree/master/sitemesh/test3
參考: