Sitemesh study [1]

Source: Internet
Author: User
Tags date copy header wrapper tld access

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

Using Sitemesh to build a composite view-1.hello

Use Sitemesh to build a composite view-2. Adorner <-now

Using Sitemesh to build a composite view-3. Other discussions
Adorners decorator The concept of building reusable Web applications, a common approach is to build a layered system, as in the following common Web application: front-end, front-end:jsp and Servlets, or Jakarta Velocity control layer Framework Controller: (struts/webwork) business logic Business: main 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 to change the structure of the page creatively, the 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 implement the standard more cool ideas than these, but poor I still don't understand what it is, and 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 file, there are 2 files to be established to web-inf/: sitemesh.xml (optional) Decorators.xml Sitemesh.xml can set 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 that allows you to give the original page for printing when accessed in Http://localhost/aaa/a.html?printable=true mode ( Avoid the fancy pictures of header,footer and so on) (but generally do not build it, the default setting is 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 page with adorner page: page filename name: alias role: Roles, for Security WebApp: You can specify this file to store directory 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"%>
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): &LT;TITLE&GT;AAA page </title>

Results of Access/aaa.jsp: &LT;TITLE&GT;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 onload= "Document.someform.somefield.focus ();" >the decorated page: <body bgcolor= "white" onload= "Document.someform.somefield.focus" (); >

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

<decorator:usepage id= "..."/> like a <jsp:useBean> tag in a JSP page, you can use a page that is packaged 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 Decorators.xml has <decorator name= "Panel" page= "panel.jsp"/> A public page, will be the panel packaging:/_public/date.jsp: ... <%=new java.util.Date ()%> <decorator:getproperty property= "myemail"/> Wrapped page/page.jsp: <title> Page Application </title> ... <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.