Official website :http://wiki.sitemesh.org/wiki/display/sitemesh/Home
You can also download official examples Demo Reference and learning , Here I will only do a simple example , Demonstrate the most basic use
The first is to add Jar package , I use it sitemesh-2.4.2.jar, And then in web.xml Add filter in the :
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>
increase SiteMesh The configuration file decorators.xml, The document is placed in WEB-INF Next :
<?xml version="1.0" encoding="UTF-8"?><decorators defaultdir="/layouts/"> <!-- Requests that do not require filtering --> <excludes> <pattern>/static/*</pattern> <pattern>/remote/*</pattern> </excludes> <!-- Define the pages to be filtered by the decorator --> <decorator name="default" page="default.jsp"> <pattern>/*</pattern> </decorator></decorators>
Create a new folder in the root directory layouts, And then create three JSP, A default is a default , An output head , An output tail , The default page references the other two .
Default page default.jsp:
<%@ page contentType="text/html;charset=UTF-8"%><%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><html><head><title>SiteMesh Example -<sitemesh:title/></title><sitemesh:head/></head><body> <%@ include file="/layouts/header.jsp"%> <div id="content"> <sitemesh:body/> </div> <%@ include file="/layouts/footer.jsp"%></body></html>
Briefly explain :
Introduced SiteMesh label .
<sitemesh:title/> It will be automatically replaced by the filtered page title.
<sitemesh:head/> Will filter the page head What's inside ( except title) Put it in this place .
<sitemesh:body/> Filtered pages body Put the contents here .
Head introduction js and css, Can be reused in other .
Header page header.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> Menu information
Tail page footer.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> Copyright information
Create a new folder under the root static, Used to test whether to intercept , Create a new JSP:
<%@ 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> <head> <base href="<%=basePath%>"> <title> Did someone intercept me ?</title> </head> <body> Did someone intercept me ? </body></html>
visit :http://127.0.0.1:8080/sitemesh/index.jsp This will intercept
visit :http://127.0.0.1:8080/sitemesh/static/index.jsp Will not intercept processing
See the actual effect according to the page
Click download attachment
I recommend you to read more about “ sitemesh frame The page layout Open source introduction ” The article