I used to learn sitemesh through some materials on the Internet. Later I found that all the materials are old. Now I have been using sitemesh3, and I have been reading sitemesh2.3, today I went back to some sitemesh3 documents and found that it is a little different from 2.3. I will not talk about how to use it. I will first attach a picture to my friends.
Does this figure clearly show the workflow of sitemesh? I think it is very clear. I can understand it after reading what I said yesterday.
Briefly describe the working principle:
Sitemesh is based on servlet filters, that is, filter streams. It intercepts the reponse and decorations it before it is delivered to the customer.
Two terms are involved: decorator page and content page. sitemesh uses content page decoration, the page layout and appearance are always displayed,
And return to the customer
The running environment of sitemesh3.0 requires: servlet2.5, jdk1.5, and later.
Next let's start to do a simple demo, first go to the next sitemesh-3.0-alpha-2.jar package to an address: https://github.com/sitemesh/sitemesh3/downloads
Next Full version, and then in the sitemesh-3.0-alpha-2 \ sitemesh-3.0-alpha-2 \ DIST has this jar package, see clearly is Dist under the sitemesh-3.0-alpha-2.jar, not the SRC below the jar package, the name is very like, at the beginning, I got it wrong.
Open myeclipse, create a new project sitemesh1, drag the sitemesh-3.0-alpha-2.jar below Lib, create a new Web. xml under the WEB-INF, used to configure a filter,
<filter> <filter-name>sitemesh</filter-name> <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Compared with 2.3, this configuration has different class package names, and the others are similar. Configured with a filter device, then configure the decoration page: Build a sitemesh3.xml under the WEB-INF
The configuration here is exquisite. Let me explain in detail
<Sitemesh> <! -- Decorator.html is decorated for all pages in the root directory --> <mapping Path = "/*" decorator = "/decorator. jsp"/> </sitemesh>
If we do not need to decorate a page, we can define it as follows:
<mapping path="/javadoc/page.html" exclue="true"/>
Also, the page.html file under the javadocfolder will not be decorated.
If you want the pages in the entire folder not to be decorated, use * instead:
<mapping path="/javadoc/*" exclue="true"/>
If you want the files in the javadoc folder to be decorated by multiple decorative pages, you can configure them as follows:
<mapping> <path>/javadoc/*</path> <decorator>/decorators/article.html</decorator> <decorator>/decorators/two-page-layout.html</decorator> <decorator>/decorators/common.html</decorator></mapping>
All pages under javadoc are decorated by three decorative pages. My demo made the first configuration, so I created a decorator folder under webroot and a decorator. jsp folder under the folder as the decoration page:
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>
The body of the decorated page is placed in the middle of the beginning and end.
In myeclipse, an index. jsp is automatically created for you. The body content in it is this is my JSP page by default. Let's modify it a bit:
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>
So our demo is ready. Open the browser.
It's easy to merge the title and body, an artifact.