Using Sitemesh to implement similar ASP. NET Master effects in JSPs

Source: Internet
Author: User

1. Introduction

Sitemesh is a lightweight, Flexible Java Web application framework that is applied decorator mode to allow a complete separation from the presentation content.

Sitemesh Framework is a very good page decorator framework developed by the Opensymphony team, which filters the user requests, filters the server to the client, and then adds some decorations (header,footer, etc.) to the original page. The results are then returned to the client. Through the Sitemesh page decoration, can provide better code reuse, all the page decoration effect coupling in the target page, no need to use the include directive to contain the decoration effect, the target page and the decorative page completely separated, if all the pages use the same adorner, Can be a unified style for the entire Web application.

Step 1

Introducing Jar Package Sitemesh-3.0.1.jar

Step 2

Create Sitemesh3.xml in the Web-inf directory

<?xml version= "1.0" encoding= "UTF-8"?><sitemesh><!--indicate a page that satisfies "/*" and will be decorated with "/master.jsp"-->< Mapping path= "/*" decorator= "/master.jsp"/><!--indicate a page that satisfies "/exclude.jsp*" and will be excluded from being decorated--><mapping path= "/ el.jsp* "exclue=" true "/></sitemesh>  

Step 3

Add Sitemesh to the Web-inf directory under Web. xml

<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>  

Step 4

Creating a master Page master.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 "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title><Sitemesh:write Property= ' title '/></title>   <Sitemesh:write Property= ' head '/>  </Head><Body> <H1class= ' title '>Sitemesh Example site:<Sitemesh:write Property= ' title '/></H1>   <HR> <Sitemesh:write Property= ' body '/>  </Body></HTML>

Step 5

Build test Page test.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 "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>This is a test.</title></Head><Body> <P>Sitemesh applies decorator mode, intercepts request and response with filter, and combines the page component Head,content,banner into a full view. Usually we are using the include tag in each JSP page to constantly contain a variety of headers, stylesheet,scripts and footer, now, with the help of Sitemesh, we can happily delete them. For example, you want to easily reach the composite view mode, then read this article.</P></Body></HTML>

Step 6 Complete, the effect is as follows

Sitemesh3.xml Configuration Detailed

<sitemesh> <!--By default, Sitemesh only intercepts and trims the types Content-type to text/html in the HTTP response header, and we can add more MIME Type-<mime-type>text/html</mime-type> <mime-type>application/vnd.wap.xhtml+xml</ Mime-type> <mime-type>application/xhtml+xml</mime-type> ... <!--the default adorner, enabling the adorner to decorate when none of the following paths match--&  Gt <mapping decorator= "/default-decorator.html"/> <!--to different paths, enable different adorners--<mapping path= "/admin/*" decor  Ator= "/another-decorator.html"/> <mapping path= "/*.special.jsp" decorator= "/special-decorator.html"/> <!--to the same path, enable multiple adorners-<mapping> <path>/articles/*</path> <decorator>/decorators/arti Cle.html</decorator> <decorator>/decorators/two-page-layout.html</decorator> <decorator> /decorators/common.html</decorator> </mapping> <!--excluded, no decorating path--<mapping path= "/javadoc/*" Exclue= "true"/> <mapping path= "/brochures/*" EXclue= "true"/> <!--custom tag rules-<content-processor> <tag-rule-bundle class= "Com.something.CssC Ompressingbundle "/> <tag-rule-bundle class=" Com.something.LinkRewritingBundle "/> </content-processor > ...</sitemesh>

  

Using Sitemesh to implement similar ASP. NET Master effects in JSPs

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.