Using the decorators feature of Sitemesh

Source: Internet
Author: User
Tags tld

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.