Composite pages, is always a development of the Web application must face the problem, the struts of the titles have tired, heard Sitemesh good, try, originally thought complex, who knows to use it is so simple, too amazing.
Write down the trial tutorial:
1. Prepare a Web project to download the Sitemesh package and TLD files to the Sitemesh official website
Http://wiki.sitemesh.org/wiki/display/sitemesh/Home
2. Add the Sitemesh-2.3.jar to the classpath and place the TLD under webcontent/web-inf/tld/
3. Change the Web. xml file and add it where appropriate:
(1) Filter configuration:
<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>
(2) Tag Library configuration:
<taglib>
<taglib-uri>sitemesh-decorator</taglib-uri>
<taglib-location>
/web-inf/tld/sitemesh-decorator.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>sitemesh-page</taglib-uri>
<taglib-location>
/web-inf/tld/sitemesh-page.tld
</taglib-location>
</taglib>
4. Build the Sitemesh.xml file under the Webcontent/web-inf directory, as follows:
<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"/>
<parser content-type= "Text/html;charset=iso-8859-1"
class= "Com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
</page-parsers>
<decorator-mappers>
<mapper class= "Com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper" >
<param name= "config" value= "${decorators-file}"/>
</mapper>
</decorator-mappers>
</sitemesh>
5. Build Decorators.xml file with the following contents:
<decorators defaultdir= "/pages/_decorators" >
<decorator name= "main" page= "main.jsp" >
<pattern>/user*</pattern>
</decorator>
</decorators>
6. Under the catalogue webcontent/pages/_decorators layout.jsp, the contents are as follows:
<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ taglib uri= "Sitemesh-decorator" prefix= "decorator"%>
<title> <decorator:title/> </title>
<decorator:head/>
<body>
Hello world!<decorator:body/>
</body>
7. Under the catalogue webcontent/pages index.jsp, the contents are as follows:
<title>C2OneMenu</title>
<body>main</body>
Complete.
Related instructions:
A. As you can see through file Decorators.xml, I've configured only to decorate URL requests that can match the/user*, because there is a URL rewrite, so the decorations are for URLs regardless of the original path of the decorated file. For example, my Access URL is: http://localhost:9080/C2oneWeb/userAction.htm, the original useraction.htm is corresponding to pages/ Index.jsp, the results of this page will be decorated layout.jsp decoration, the combination of the page is layout.jsp layout.
B.<decorator:title/> This tab will find the title of the decorated page (in the <title></title> tag) to fill in, <decorator:head/> Find the content of the head (The DefaultDir property under the Decorators tab in C.decorators.xml indicates the directory where the default decorative page (in this case, layout.jsp) is located.