Simplifying web site development: Sitemesh Plugins

Source: Internet
Author: User

When you write a Web site, almost all of the pages will have the same part. For example, at the top of the navigation bar, each page is the same, the bottom of the copyright notice, each page is the same.

So when writing the top navigation bar, the first method is to directly copy the navigation bar all the code, this method of early development is very simple but late maintenance is simply a nightmare, if the navigation bar to add a button, all the pages to be modified, very troublesome.

The second method is to write the top navigation bar specifically into a file, and all pages include this file. This allows each page to write an include statement. Later modification as long as you change a file. But this method writes out the navigation bar file which is not a full web page. And each file must be written with an include statement, or more cumbersome.

In order to make this page writing more simple, Sitemesh came into being.

Sitemesh solves this problem through the adorner mode. The use of adorners is described below.

Installation method

The first step is to put Sitemesh.xxx.jar in the/web-inf/lib.

The second step is to add the Sitemesh filter to Web. Xml.

<filter>  <filter-name>sitemesh</filter-name>  <filter-class> Com.opensymphony.sitemesh.webapp.sitemeshfilter</filter-class></filter> <filter-mapping>  <filter-name>sitemesh</filter-name>  <url-pattern>/*</url-pattern></ Filter-mapping>

It is important to note that if you are working with struts, the filter-mapping will be behind struts so that it can be decorated according to the filename, otherwise it is decorated according to the action name. In addition, the following three lines are added to the filter-mapping to allow struts to decorate the file before accessing the JSP. If you don't add it, you can't see the decorative effect.

<dispatcher>REQUEST</dispatcher><dispatcher>FORWARD</dispatcher><dispatcher> Include</dispatcher>

The third step is to create a new adorner definition file. The location of the file is/web-inf/decorators.xml. The contents are as follows:

<?xml version= "1.0" encoding= "UTF-8"?><decorators></decorators>

So the Sitemesh plug-in is installed.

Use of plugins

Start by writing a very simple adorner file, which is to add the header and footer words to the top and tail of the page. The role of \texttt{decorator:body} is to introduce the real content into the file.

<%@ taglib uri= "Http://www.opensymphony.com/sitemesh/decorator" prefix= "decorator"%>

The second step is to declare the adorner in Decorators.xml.

<?xml version= "1.0" encoding= "UTF-8"? ><decorators defaultdir= "/web-inf/decorators" >    <decorator Name= "Basic-theme" page= "basic-theme.jsp" >        <pattern>/test.jsp</pattern>    </decorator ></decorators>

Since the Web. XML was modified, you need to restart the server to see the effect. You'll see test.jsp's head and tail are added with headers and footer.

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.