Adorner component for "Sitemesh" JSP Sitemesh

Source: Internet
Author: User

Regardless of whether this thing is useless, after all, JSP page programming can take advantage of JSP's include command, like traditional web programming, first write several header and footer header.html, footer.html, banner.html, and so on each page to use <jsp:include page= "xxx.html"/> introduced these several pages. This is good maintenance, and the code is clear no trouble, Sunline to ASP. NET, VBScript, PHP and other Server Programming page we do the same. If the HTML does not have an include command, it will not even use the include command in the server backend language. However, as a very common JSP decorator plug-in Sitemesh, you do not have to be one thing, you understand the matter, in the team development, if asked to use the Sitemesh, you even use the JSP include command, but also know how to ask some pages do not decorate the device. Below, for an example, the plugin is fully explained.


First, the download and configuration of Sitemesh

First of all, this thing is still updated now, but its old version of 2.x has become a permanent stable version of Sitemesh-2.4.2.jar in 2009 and is no longer updated. This is better, lest go out push the new version to code. However, now 3.x is as proud as spring, its jar to use Maven to download, and sitemesh3.x is not too stable, the major JSP project or use sitemesh2.x.

Directly on Sitemesh's official website http://wiki.sitemesh.org/wiki/display/sitemesh/Download, the following download, this page may sometimes open the comparison card.


After downloading, you get a sitemesh-2.4.2.jar that you can use to create a new dynamic Web Project named Sitemeshtest in Eclipse for Java EE. Put the Sitemesh-2.4.2.jar directly inside the Sitemeshtest Web-inf. In Web. XML, write the following code:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "><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></web-app>
Since Sitemesh does not provide its tag library on the official website, it requires us to use its tag library in the JSP project by referencing its URL. We can't do that. Not do not want to cite your website, the main your website in foreign countries, I want to ensure that their Web project open speed. It's like you're never going to introduce Google-hosted jquery. After all, these things will be blown off one day, not safe. Therefore, we must create a new sitemesh-decorator.tld in Web-inf and write the following code:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE taglib Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.1//en" "http://java.sun.com/j2ee/dtds/ Web-jsptaglibrary_1_1.dtd "><taglib><tlibversion>1.0</tlibversion><jspversion>1.1 </jspversion><shortname>sitemesh Decorator tags</shortname><uri>sitemesh-decorator</ Uri><tag><name>head</name><tagclass> com.opensymphony.module.sitemesh.taglib.decorator.headtag</tagclass><bodycontent>jsp</ Bodycontent></tag><tag><name>body</name><tagclass> com.opensymphony.module.sitemesh.taglib.decorator.bodytag</tagclass><bodycontent>jsp</ Bodycontent></tag><tag><name>title</name><tagclass> com.opensymphony.module.sitemesh.taglib.decorator.titletag</tagclass><bodycontent>jsp</ bodycontent><attribute><name>default</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute></tag><tag><name> getproperty</name><tagclass>com.opensymphony.module.sitemesh.taglib.decorator.propertytag</ tagclass><bodycontent>jsp</bodycontent><attribute><name>property</name>< Required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute ><name>default</name><required>false</required><rtexprvalue>true</ Rtexprvalue></attribute><attribute><name>writeentireproperty</name><required> false</required><rtexprvalue>true</rtexprvalue></attribute></tag><tag>< name>usepage</name><tagclass>com.opensymphony.module.sitemesh.taglib.decorator.usepagetag</ tagclass><teiclass>com.opensymphony.module.sitemesh.taglib.decorator.usepagetei</teiclass>< Bodycontent>jsp</bodyconTent><attribute><name>id</name><required>true</required><rtexprvalue> false</rtexprvalue></attribute></tag><tag><name>usehtmlpage</name>< Tagclass>com.opensymphony.module.sitemesh.taglib.decorator.usepagetag</tagclass><teiclass> com.opensymphony.module.sitemesh.taglib.decorator.usehtmlpagetei</teiclass><bodycontent>jsp</ bodycontent><attribute><name>id</name><required>true</required>< Rtexprvalue>false</rtexprvalue></attribute></tag></taglib>
In this way, the Sitemesh configuration is complete.


Second, the use of Sitemesh

1, in the Web-inf directory to open a new decorators.xml, this file is used to configure the use of the adorner in the JSP, indicating which uses the adorner, which pages are not used. The code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><decorators defaultdir= "/decorators" ><!--here to define a page that does not need an adorner-- ><excludes><!--root directory index.jsp will not be decorated--><!--next, if necessary, you can add the pattern under this label, but also to use * Also explain the problem such as <pattern >/a/*<pattern> means that all pages of the A folder under WebContent do not use adorners such as <pattern>/a/*.jsp<pattern> It means that all JSP pages under the A folder under WebContent do not use adorners such as <pattern>/a/b*<pattern>, which means that all B in the A folder under WebContent does not use adorners-- <pattern>/index.jsp</pattern></excludes><!--used to define the page to be filtered using the adorner--><decorator name= "main "Page=" ><pattern>/*</pattern></decorator></decorators> "decoratorstest.jsp"
It should be noted that if this project is developed using struts at the same time, what decorators.xml indicates should be the action corresponding to this page in Struts.xml, the return page corresponding to this action, not the actual path of this page.

2. Then we will create a new decorators folder in the root directory of the Webcontent,web project, because the beginning of the decorators.xml indicates that all the adorner's pages are hidden in the root directory of decorators. Then decorators a new decoratorstest.jsp below. The code is as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><%--This is the adorner page, notice at the beginning to declare the tag library to use Sitemesh--%><%@ taglib uri=" Sitemesh-decorator "prefix=" decorator "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

At this point, the page will appear in addition to the page specified in Decorators.xml exclude, so in Decorator.xml, the page that declares to use the adorner is all the pages of the site.

3. Finally, we build two test pages under the root directory webcontent,

One is in Decorators.xml already agreed not to use the adorner page index.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

One is to use the adorner's page fordecorator.jsp:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

third, the operation effect

Finally, the entire Web project directory structure is as follows, no one in SRC built any. java file, because the adorner Sitemesh is the content of the view layer.


Note that both index.jsp and fordecorator.jsp are just pages with a single text, but they run, because the use of adorners or not, the project to run in Tomcat, get two different results:


In the browser, look at the source code of INDEX.JSP and fordecorator.jsp, and notice that index.jsp is what we wrote in Eclipse.

The source code of FORDECORATOR.JSP is replaced by the function of the adorner:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
This JSP decorator component Sitemesh is not easy to use a matter of opinion, anyway, I think it is not good, but I will be used.

Adorner component for "Sitemesh" JSP Sitemesh

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.