Decorating JSP pages with sitemesh/decorator adorners (principle and detailed configuration)

Source: Internet
Author: User

I. Introduction of Sitemesh

Sitemesh is a Web page layout and cosmetic framework for a Java Web project. With Sitemesh, it is no longer necessary to use <jsp:include> tags in each page to introduce other public pages, such as headers, footers, and navigation.

    • The content of the Web page and the structure of the page can be separated to achieve the purpose of page structure sharing .

    • The page decoration effect is coupled to the target page without using the include directive to display a decorative effect, and the target page and the decorating page are completely detached .

    • The entire Web application can use the same decorative page, style unity, the overall effect is better

    • Sitemesh through the filter interception request and response, to the original page to add decoration, and then return the results of the decoration to the client.

    • Find the Right decorating template page based on page URL matching rules

    • Extracts the contents of the page being accessed and places it in the appropriate place in the decorating template.

Second, the use of business scenarios

For example, the common is the CRM system, the left side of the tree menu is consistent, changing the right side of the main part (that is, the page is decorated).

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/847059/201707/847059-20170702213000102-507814613. PNG "style=" margin:0px;padding:0px;border:none; "/>

Three, Sitemesh working principle

Sitemesh applies decorator mode, intercepts request and response with filter, and combines the page component Head,content,banner and bottom into a complete view. Usually we use the include tag in each JSP page to constantly contain various headers, stylesheet, scripts and footer. See

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/847059/201707/847059-20170702210734664-1555213865. PNG "style=" margin:0px;padding:0px;border:none; "/>

When the user requests home.jsp and the server is ready to return the data, it is intercepted by the Sitemesh filter and wraps the data into a Page object, page page = parsepage (Request, response , chain) is called, and then it will go to query the Decorators.xml file to see if the page needs to be decorated [if (decorator! = null && decorator.getpage ()! = null)]? Yes, apply the adorner [Applydecorator (page, decorator, request, response)], otherwise, send the original not decorated page [writeoriginal (response, page);].

Iv. Sitemesh application Configuration

First we'll go to http://www.opensymphony.com/sitemesh/to download the jar package we need: Sitemesh-2.4.jar

Then take three steps, the first step: Web. config; step two: Decorate.xml configuration; Third step: Decorate the page

4.1 Web. XML configuration

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

<filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.sitemesh . WebApp. sitemeshfilter</filter-class> </filter> <filter-mapping> <filter-name>sitemesh</fi Lter-name> <url-pattern>/*</url-pattern> </filter-mapping>

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

4.2 Decorate.xml Configuration

Create a new Decorators.xml file in the Web-inf directory (/decorator is your wrapper JSP root path here main.jsp and panel.jsp are packaged jsp,a.jsp,b,jsp is packaged JSP)

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

<?xml version= "1.0"  encoding= "UTF-8"? ><decorators>    <excludes >        <pattern>/resources/**</pattern>         <pattern>/system/login_index.do</pattern>         <pattern>/system/login.do</pattern>         <pattern>/system/close_window.do</pattern>         <pattern>/system/login_force.jsp</pattern>         <pattern>/system/info.jsp</pattern>        < pattern>/index.jsp</pattern>        <pattern>/ Usermemcached/**</pattern>    </excludes>    <decorator  name= "main"  page= "/system/mAin.do ">        <pattern>/**</pattern>     </decorator></decorators>

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Use Decrator to specify the corresponding relationship between the decoration template and the URL, or you can configure those URLs with excludes without template control.

4.3 decorating page

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

<% @page  contenttype= "Text/html; charset=utf-8"  pageencoding= "UTF-8"%><% @taglib   Prefix= "C"  uri= "Http://java.sun.com/jsp/jstl/core"%><% @taglib  prefix= "decorator"  uri= " Http://www.opensymphony.com/sitemesh/decorator " %>

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Parameter description:

<decorator:head/>

Fill the head tag content of the decorated page

<decorator:body/>

Fill the body label content of the decorated page


Decorating JSP pages with sitemesh/decorator adorners (principle and detailed configuration)

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.