- 2024-02-27 17:19:24
- 7597 热度
- 0 评论
官网:http://wiki.sitemesh.org/wiki/display/sitemesh/Home
也可以下载官方的示例Demo参考和学习,这里我只做一个简单示例,演示最基本的使用
首先就是加Jar包,我用的是sitemesh-2.4.2.jar,然后在web.xml中增加过滤器:
<?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>
增加SiteMesh配置文件decorators.xml,该文件放在WEB-INF下:
<?xml version="1.0" encoding="UTF-8"?> <decorators defaultdir="/layouts/"> <!-- 不需要过滤的请求 --> <excludes> <pattern>/static/*</pattern> <pattern>/remote/*</pattern> </excludes> <!-- 定义装饰器要过滤的页面 --> <decorator name="default" page="default.jsp"> <pattern>/*</pattern> </decorator> </decorators>
在根目录下新建文件夹layouts,然后新建三个JSP,一个是默认,一个输出头,一个输出尾,默认页面引用其他两个。
默认页面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示例-<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>
简单说明:
引入了SiteMesh标签。
<sitemesh:title/> 会自动替换为被过滤页面的title。
<sitemesh:head/> 会把被过滤页面head里面的东西(除了title)放在这个地方。
<sitemesh:body/> 被过滤的页面body里面的内容放在这里。
头部引入js和css,都可以在其他重用。
头页面header.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 菜单信息
尾页面footer.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 版权信息
在根下新建一个文件夹static,用于实验是否拦截,在该文件夹下新建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>有人拦截我吗?</title> </head> <body> 有人拦截我吗? </body> </html>
访问:http://127.0.0.1:8080/sitemesh/index.jsp这个会拦截
访问:http://127.0.0.1:8080/sitemesh/static/index.jsp则不会拦截处理
根据页面看实际效果
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- JDBC(22)
- IDEA(22)
- mongoDB(22)
- MVC(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- SpringBoot(17)
- Docker(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- ORA(15)
- JPA(15)
- Apache(15)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- Data(12)
- JSON(12)
- OAuth2(12)
- stream(11)
- int(11)
- Myeclipse(11)
- not(10)
- Bug(10)
- maven(9)
- Map(9)
- Hystrix(9)
- ast(9)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- Swagger(8)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- windows(7)
- too(7)
- HTML(7)
- RabbitMQ(6)
- star(6)
- and(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- Freemarker(6)
- read(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- Server(6)
- nginx(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- script(5)
- Syntaxhighlighter(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)
- Servlet(5)