Use Sitemesh to build a composite view-2. Adorner decorator

Source: Internet
Author: User
Tags date copy header wrapper tld
View adorner decorator The concept of creating reusable Web applications, a common approach is to build a layered system, as in the following common Web application:
    • Front, front-end:jsp and Servlets, or Jakarta Velocity
    • Control Layer Frame Controller: (struts/webwork)
    • Business logic Business: Primary business logic
    • Persistence Framework: HIBERNATE/JDO

What's bad is that the front page logic is hard to reuse, and when you're using countless include-and-stylesheet in every page, scripts,footer, a problem arises-duplicate code, Each page must use copy to use the page structure,

And when you need creative changes to the page structure, disaster falls in love with you.

Sitemesh through the filter to intercept request and response, and add a certain decoration to the original page (possibly Header,footer ...), and then return the results to the client, and the decorated original page does not know the Sitemesh decoration, This also achieves the goal of decoupling.

It is said that the upcoming Portlet specification will help us to achieve the standard of more cool ideas than these, but poor I still don't understand what it really is, interested people can study
Jetspeed, or JSR (Java specification Request) 168, but I think Sitemesh is so simple, we might as well use it first.

 

Let's see how to configure the environment in addition to copy to the Sitemesh.jar in Web-inf/lib, copy to Sitemesh-decorator.tld in Web-inf, SITEMESH-PAGE.TLD files, there are 2 more files to be set up to web-inf/:
    • Sitemesh.xml (optional)
    • Decorators.xml

Sitemesh.xml can set up 2 kinds of information:

Page parsers: is responsible for reading the stream's data into a Page object to be Sitemesh parsed and manipulated. (less often, default)

Decorator Mappers: Different types of adorners, I found 2 kinds of more useful are listed below. A generic mapper that specifies the profile name of the adorner, and another printable adorner,

Allows you to give the original page for printing (lest you hook up the fancy pictures of header,footer, etc.) when you access it in Http://localhost/aaa/a.html?printable=true mode.

(but generally do not have to establish it, the default settings are sufficient: Com/opensymphony/module/sitemesh/factory/sitemesh-default.xml):

Example:

<sitemesh>
<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=iso-8859-1" class= " Com.opensymphony.module.sitemesh.parser.FastPageParser "/>
</page-parsers>

<decorator-mappers>
<mapper class= "Com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper" >
<param name= "config" value= "/web-inf/decorators.xml"/>
</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>
</decorator-mappers>
</sitemesh>

Decorators.xml: Defines the description of all the page artifacts that make up the composite view (the main structure page, Header,footer ...), as the following example:

<decorators defaultdir= "/_decorators" >
<decorator name= "main" page= "main.jsp" >
<pattern>*</pattern>
</decorator>
<decorator name= "printable" page= "printable.jsp" role= "Customer" webapp= "AAA"/>
</decorators>
    • DefaultDir: Table of contents containing adorner pages
    • Page: Paging file name
    • Name: Alias
    • Role: Roles, for security
    • WebApp: You can specify this file to store the directory separately
    • Patterns: Matching path, can be used *, those visited pages need to be decorated.

 

The most important thing is to write the adorner itself (that is, those that want to reuse pages, and structure pages). In fact, the important job is to make the adorner page itself (that is, the pages that contain the structure and the rules) and then describe them to the decorators.xml.

Let's take a look at the simplest usage: in fact, the most common and simplest use is our hello example, in the face of so many technologies, I think that as long as the function to reach the point, there is no need to study too deep (unless you have deeper requirements).

<%@ page contenttype= "text/html; CHARSET=GBK "%> <%@ taglib uri=" Sitemesh-decorator "prefix=" decorator "%>"  
<decorator:head/>
examples of <body> Sitemesh <decorator:body/>

We used only 2 tabs on the adorner page:

<decorator:title default= "Adorner page ..."/>: Inserts the title of the original page of the request into the <title></title> center.

<decorator:body/>: Inserts the entire contents of the original page of the request into the appropriate position.

Then we add the following description in the Decorator.xml:

<decorator name= "main" page= "main.jsp" >
<pattern>*</pattern>
</decorator>

In this way, all of the requested pages will be processed and main.jsp in front of you in the form of a new one.

 

Let's take a look at more usage. (plagiarism Sitemesh document) with all the following tags: decorator tagspage tags are used to create an adorner page. is used to access adorners from the original content page. <decorator:head/>
<decorator:body/>
<decorator:title/>
<decorator:getproperty/>
<decorator:usepage/>
<page:applydecorator/>
<page:param

<decorator:head/>

Inserts the contents of the Head tab of the original page (the wrapped page) (excluding the head tag itself).

<decorator:body/>

Inserts the contents of the Body tab of the original page (the wrapped page).

<decorator:title [default=]/>

You can also add a default value by inserting the contents of the title tag of the original page (the wrapped page).

Cases:

/_decorator/main.jsp (Adorner page): <title><decorator:title default= "Title-hello"/>-Additional title </title>

/aaa.jsp (Original page): <TITLE>AAA page </title>

Results of Access/aaa.jsp: <TITLE>AAA page-Additional title </title>

<decorator:getproperty property= "..." [default= "..."] [writeentireproperty= "..."] />

You can also add a default value by inserting the contents of the original label property of the Raw page (the wrapped page) at the label.

The examples in the Sitemesh document are well understood:
The decorator: <body bgcolor= "White" <decorator:getproperty property= "Body.onload" writeentireproperty= "true"/ >>
The undecorated page: <body >
The decorated page: <body bgcolor= "White" >

Note that writeentireproperty= "true" adds a space before the content is inserted.

<decorator:usepage id= "..."/>
Like the <jsp:useBean> tags in a JSP page, you can use a page that is wrapped as a Page object. (Lazy to use)

Example: Available <decorator:usepage id= "page"/>: <%=page.gettitle ()%> to achieve <decorator:title/> access results.

 

<page:applydecorator name= "..." [page=] ... "title=" ... " >
<page:param name= "..." > ... </page:param>
<page:param name= "..." > ... </page:param>
</page:applyDecorator>

Application wrapper to the specified page, generally used in the wrapped page to actively apply the wrapper. This label is a bit difficult to understand, let's look at an example:

Wrapper page/_decorators/panel.jsp:<p><decorator:title/></p> ... <p><decorator:body/>< /p>
And in the Decorators.xml, there are <decorator name= "Panel" page= "panel.jsp"/>

A public page that is about to be packaged by a panel:/_public/date.jsp:
... <%=new java.util.Date ()%> <decorator:getproperty property= "Myemail"/>

Wrapped page/page.jsp:
Application </title> of <title>page
.....
<page:applydecorator name= "Panel" page= "/_public/date.jsp" >
<page:param name= "Myemail" > Chen_p@neusoft.com </page:param>
</page:applyDecorator>

What will be the end result? In addition to/page.jsp will be wrapped on the default packaging page Header,footer outside, Page.jsp page is also embedded in the date.jsp page, and this date.jsp page will be panel.jsp packaged as a title plus the body has 2 paragraphs of the page, the 1th paragraph is date.jsp title, the 2nd paragraph is the date.jsp body content.

In addition, the value of the property declared by the Page:param tag contained in Page:applydecorator can also be accessed in the wrapper page using the Decorator:getproperty tag.



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.