[SSI Development Summary. 5] sitemesh decorators

Source: Internet
Author: User

Sitemesh Project Overview

The sitemesh of OS (opensymphony) is a layout and decoration interface used to implement page layout and Decoration in JSP)
The framework component helps website developers easily separate dynamic content from static decoration.
Sitemesh is a framework based on web page layout, decoration, and integration with existing web applications. It can help us
Create consistent page layout and appearance in a project composed of multiple pages, such as consistent navigation bar, consistent banner, consistent copyright, and so on.
It can not only process dynamic content, such as JSP, PHP, ASP, but also static content, such as HTM content,
So that its content meets the requirements of your page structure. It can even use an HTML file as an include file as a panel.
To other files. All of these are the most vivid implementations of the gof decorator mode. Although it is implemented by the Java language, it can be well integrated with other web applications.
After a request is sent to the server, if the request requires sitemesh decoration, the server first interprets the requested resource and obtains the decorator used for the request according to the configuration file, finally, decorate the requested resource with the decorator and return the result together to the client browser.

1. Configure web. xml

/******/WEB-INF/Web. xml ******/

<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app id = "webapp_id" version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee
"Xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance
"Xsi: schemalocation =" http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
">
<Display-Name> game_proj </display-Name>
<! -- Filter supported by Chinese Language -->
<Filter>
<Filter-Name> set character encoding </filter-Name>
<Filter-class> com. popoann. setcharacterencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> GBK </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> set character encoding </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
<Filter>
<! -- Acegi filter -->
<Filter-Name> acegifilterchain </filter-Name>
<Filter-class> org. acegisecurity. util. filtertobeanproxy </filter-class>
<Init-param>
<Param-Name> targetclass </param-Name>
<Param-value> org. acegisecurity. util. filterchainproxy </param-value>
</Init-param>
</Filter>
<! -- Acegi filter URL ing -->
<Filter-mapping>
<Filter-Name> acegifilterchain </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
<! -- Actioncontextcleanup filter -->
<Filter>
<Filter-Name> Struts-cleanup </filter-Name>
<Filter-class> org. Apache. struts2.dispatcher. actioncontextcleanup </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> Struts-cleanup </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
<! -- Core filter of sitemesh -->
<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>
<! -- Struts core filter -->
<Filter>
<Filter-Name> struts2 </filter-Name>
<Filter-class> org. Apache. struts2.dispatcher. filterdispatcher </filter-class>
<Init-param>
<Param-Name> struts. Action. Extension </param-Name>
<Param-value> HTM </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> struts2 </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

<! -- Load the springioc container when the application starts -->
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>
/WEB-INF/beans-*. xml
</Param-value>
</Context-param>
<Listener>
<Listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class>
</Listener>
<! -- Welcome page -->
<Welcome-file-List>
<Welcome-File> index.htm </welcome-File>
</Welcome-file-List>

</Web-app>

 

Pay attention to the filter sequence. Choose actioncontextcleanup filter> sitemesh core filter> struts core filter.

2. Configure sitemesh. xml

/******/WEB-INF/sitemesh. xml ******/

<Sitemesh>

<! -- Specify the address of the page decorator configuration file -->


<Property name = "decorators-file" value = "/WEB-INF/decorators. xml"/>

<! -- Specify the exception definition address in the page decorator configuration file -->

<Excludes file = "$ {decorators-file}"/>

<Page-parsers>
<Parser default = "true"
Class = "com. opensymphony. Module. sitemesh. parser. defaultpageparser"/>
<Parser Content-Type = "text/html"
Class = "com. opensymphony. Module. sitemesh. parser. fastpageparser"/>
<Parser Content-Type = "text/html; charset = gb2312"
Class = "com. opensymphony. Module. sitemesh. parser. fastpageparser"/>
</Page-parsers>
<! --
<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>

 

This configuration file is directly copied from the downloaded sitemesh Development Kit and basically only needs to be modified.

<Property name = "decorators-file" value = "/WEB-INF/decorators. xml"/>

This label is used to specify the path of the configuration file for page assembly

3. Configure decorators. xml

/*****/WEB-INF/decorators. xml ******/

<? XML version = "1.0" encoding = "ISO-8859-1"?>

<Decorators defaultdir = "/decorators">
<! -- Any URLs that are excluded will never be decorated by sitemesh -->

<Excludes>
<Pattern>/about.htm </pattern>
</Excludes>

<Decorator name = "main1" page = "headandfoot1.jsp">
<Pattern>/index.htm </pattern>
</Decorator>

<Decorator name = "main2" page = "headandfoot2.jsp">
<Pattern>/MySpace/* </pattern>
</Decorator>
</Decorators>

 

The defaultdir attribute of the decorators label specifies the location of the modification page relative to the application root directory. Here it is "/decorators"

Excludes label: Specifies which pages are not modified

Decorator specifies which matching pages are modified. Wildcards are supported. The page attribute indicates the modified pages, that is, what clothes are used for the pages.

4. Design Modification page

/*****/Decorators/headandfoot1.jsp ******/


<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" errorpage = "" %>
<% @ Taglib prefix = "decorator" uri = "http://www.opensymphony.com/sitemesh/decorator" %>

<% @ Taglib prefix = "page" uri = "http://www.opensymphony.com/sitemesh/page" %>

<! 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 = gb2312"/>
<Title> <decorator: Title default = "kangxm test"/>
</Title>
<Decorator: Head/>

</Head>

<Body>
<Decorator: body/>

</Body>
</Html>

 

Red indicates the tag to be configured:

<% @ Taglib prefix = "decorator" uri = "http://www.opensymphony.com/sitemesh/decorator" %>
<% @ Taglib prefix = "page" uri = "http://www.opensymphony.com/sitemesh/page" %>

Make sure this page is included in the sitemesh System

<Decorator: Title default = "kangxm test"/>

Extract the title of the decorated page. If not specified, use "kangxm test"

<Decorator: Head/>

Extract content from the head label of the decorated page

<Decorator: body/>

Extract content from the body tag of the decorated page

/*************************************** **********************

By now, the sitemesh configuration is complete. In general, the configuration is easier, making page control more flexible, and avoiding repetitive code.

[Note]: To enable sitemesh integration with struts2, you must also download the sitemesh plug-in of struts2.

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.