Sitemesh Page Layout

Source: Internet
Author: User

Look at the project found that the corresponding page can not find the sidebar part of the code, carefully observed after the introduction of the page Sitemesh tags, check the next data is the page with the Sitemesh framework solution! Coupling! The

Before more than one module contains the same module always include JSP file, no feeling how troublesome, but looked at the Sitemesh, feel can be very simple!

Sitemesh by intercepting the request and response based on Servletfilter, and the original page involved in a certain decoration, and then return the results to the client, the decorated page does not know Sitemesh decoration.

The steps to use are as follows: (Sitemesh running environment: servlet, JDK)

1, introducing Maven dependency

<dependency>
<groupId>opensymphony</groupId>
<artifactId>sitemesh</artifactId>
<version>2.4.2</version>
</dependency>

Add a filter to the 2,web.xml:

<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<servlet-name>springmvcServlet</servlet-name>
</filter-mapping>

<servlet>
<servlet-name>springmvcServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>***</param-name>
<param-value>***springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvcServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

3. Add the Sitemesh configuration file under the Web-inf directory Decorators.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<decorators defaultdir= "/web-inf/layout" >
<!--here to define pages that don't need filtering--
<excludes>
<pattern>/login</pattern>

<pattern>/static/*</pattern>
<!--......-->
</excludes>

<!--define pages to decorate--
<decorator name= "Default" page= "yanan7890.jsp" >
<pattern>/*</pattern>
</decorator>
</decorators>

4. Define the yanan7890.jsp page , which is placed in the/web-inf/layout/directory according to the decorators DefaultDir configuration

<% @page language= "java" contenttype= "text/html; Charset=utf-8 "%>
<%@ taglib prefix= "Sitemesh" uri= "Http://www.opensymphony.com/sitemesh/decorator"%>
<! DOCTYPE html>

<title>sitemesh Example-<sitemesh:title/></title> <!--is automatically replaced with the title.sitemesh:title of the filtered page, optional.

<!--can also introduce CSS and js--> that need to be reused

<link href= "/***/.css" rel= "stylesheet" type= "Text/css" >
<script src= "/***/.js" ></script>
<sitemesh:head/><!--put the contents of the filtered page head (except the title) in this place--
<body>

<%@ include file="/common/head.jsp"%>

<div>
<sitemesh:body/><!--the contents of the filtered page body are placed here. -
</div>
<%@ include file= "/common/foot.jsp"%>
</body>

such as Step 3 configuration, such as access to the page under/login and/static will not be decorated, access to other pages will be in accordance with yanan7890.jsp interception decoration

At this point, it's done!


Reference article:

1.http://cuisuqiang.iteye.com/blog/2066166

2.http://www.cnblogs.com/shanshouchen/archive/2012/08/10/2631819.html

3.http://baike.baidu.com/item/sitemesh

Sitemesh Page Layout

Related Article

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.