sitemesh struts2
时间:2010-09-01 来源:jiangchao
一.需要包(两个),我用的是struts-2.1.8.1,注意版本
struts2-sitemesh-plugin-2.1.8.1.jar
sitemesh-2.4.2.jar
二.创建decorators.xml
Xml代码- <?xml version="1.0" encoding="utf-8"?>
- <decorators defaultdir="/decorators">
- <!--excludes结点则指定了哪些路径的请求不使用任何模板-->
- <!--/index.jsp和凡是以/login/开头的请求路径一律不使用模板-->
- <excludes>
- <pattern>/index.jsp*</pattern>
- <pattern>/login/*</pattern>
- </excludes>
- <!--decorator结点指定了模板的位置和文件名,通过pattern来指定哪些路径引用哪个模板-->
- <decorator name="main" page="mode.jsp">
- <pattern>/*</pattern>
- </decorator>
- </decorators>
<?xml version="1.0" encoding="utf-8"?> <decorators defaultdir="/decorators"> <!--excludes结点则指定了哪些路径的请求不使用任何模板--> <!--/index.jsp和凡是以/login/开头的请求路径一律不使用模板--> <excludes> <pattern>/index.jsp*</pattern> <pattern>/login/*</pattern> </excludes> <!--decorator结点指定了模板的位置和文件名,通过pattern来指定哪些路径引用哪个模板--> <decorator name="main" page="mode.jsp"> <pattern>/*</pattern> </decorator> </decorators>
三.在WebRoot下创建decorators 文件夹,其下创建layout.jsp
Html代码- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@taglib prefix="decorator"
- uri="http://www.opensymphony.com/sitemesh/decorator"%>
- <%@taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <title><decorator:title default="嘻嘻哈哈" />
- </title>
- <decorator:head />
- </head>
- <body id="page-home">
- <div style="background-color: red;width:100%;100px;">
- 头
- </div>
- <div>
- <!-- 这里的内容由引用模板的子页面来替换 -->
- <decorator:body />
- </div>
- </body>
- </html>
<%@ page language="java" pageEncoding="UTF-8"%> <%@taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator"%> <%@taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title><decorator:title default="嘻嘻哈哈" /> </title> <decorator:head /> </head> <body id="page-home"> <div style="background-color: red;width:100%;height: 100px;"> 头 </div> <div> <!-- 这里的内容由引用模板的子页面来替换 --> <decorator:body /> </div> </body> </html>
四.测试,创建index.jsp,根据配置文件,他不使用模板
Html代码- <%@ page language="java" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>index.jsp</title>
- </head>
- <body>
- <form action="login.jsp">
- <input type="submit" />
- </form>
- </body>
- </html>
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>index.jsp</title> </head> <body> <form action="login.jsp"> <input type="submit" /> </form> </body> </html>
创建login.jsp,根据配置文件,他使用模板
Java代码- <%@ page language="java" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>行么</title>
- </head>
- <body>
- <form action="login.jsp">
- <input type="text" />
- <input type="submit" />
- </form>
- </body>
- </html>
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>行么</title> </head> <body> <form action="login.jsp"> <input type="text" /> <input type="submit" /> </form> </body> </html>
测试可以看到效果
我的开发环境
相关阅读 更多 +