1. 介绍
sitemesh的作用是制作一个模板页面,然后再把需要访问的页面的指定内容加载到这个模板页面然后显示出来,所以本质上来说你的url访问的不是你指定的页面,而是这个模板页面。
这么做的好处是一些资源文件的加载,还有<include>这种东西只用写一遍就可以了,减少了重复性的操作。
还有就是sitemesh3.0的配置和sitemesh2.4的配置是完全不同的!!!反正我是吃过亏了。
2. 配置
2.1 maven配置
<dependency> <groupId>org.sitemesh</groupId> <artifactId>sitemesh</artifactId> <version>3.0.0</version> </dependency>
2.2 web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>SpringMVC</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>sitemeshFilter</filter-name>
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemeshFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这里有些人会遇到问题,因为web.xml加载配置的时候因为先后问题会把之前的配置冲掉,我没有具体研究这个原因,反正就把我自己的配置贴出来了。这个url-pattern会拦截所有的请求,就是说所有请求都会使用sitemesh的配置去操作。
2.3 sitemesh3.xml配置
<?xml version="1.0" encoding="utf-8" ?>
<sitemesh>
<mapping path="/*" decorator="/template/template.jsp"></mapping>
</sitemesh>
你可以有多个mapping 的 path ,这样不同的请求可有不同的模板效果,同时使用下面这个代码的可以排除模板使用:
<sitemesh>
<mapping path="*/login*" exclue="true" />
</sitemesh>
2.4 模板页面
<head>
<meta charset="utf-8" />
<title><sitemesh:write property='body'/></title>
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
<meta content="heinqi" name="author" />
<link rel="shortcut icon" href="../../res/media/image/favicon.ico" />
<sitemesh:write property='head'/>
</head>
<body class="page-header-fixed">
sfsdfsd
<div class="page-container">
<div class="page-content">
<div class="container-fluid">
<sitemesh:write property='body'/>
</div>
</div>
</div>
<script type="text/javascript">
</script>
</body>
</html>
<sitemesh:write property='title'/>:指定页面的title会加载到模板页面,
<sitemesh:write property='head'/> :指定页面的head会加载到模板页面,
<sitemesh:write property='body'/>:指定页面的body会加载到模板页面,
接下来以此类推吧。
祝你好运