您的足迹:首页 > JAVA >sitemesh简单例子

sitemesh简单例子

Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filter,类似于 ASP.NET 中的‘母版页’技术,相比于jsp 的include语法,在需要修改时,只需要修改模板的内容就可以,提供了相对方便的操作

首先要引入sitemesh包

<dependency>
<groupId>opensymphony</groupId>
<artifactId>sitemesh</artifactId>
<version>${sitemesh.version}</version>
<scope>runtime</scope>
</dependency>



配置web.xml,让sitemeshfilter过滤需要的链接

<filter>
<filter-name>sitemeshFilter</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemeshFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


配置decorators.xml,和web.xml在同一个目录

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/WEB-INF/sitemesh/">
<excludes>
<pattern>/static/*</pattern>
<pattern>/api/*</pattern>
</excludes>

<decorator name="default" page="default.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>

其中excludes代表不包括下面链接的意思


默认的模板页面为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"%>

<c:set var="ctx" value="${pageContext.request.contextPath}" />

<!DOCTYPE html>
<html>
<head>
<title>QuickStart示例:<sitemesh:title/></title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="Cache-Control" content="no-store" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

<link type="image/x-icon" href="${ctx}/static/images/favicon.ico" rel="shortcut icon">
<link href="${ctx}/static/bootstrap/2.3.2/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<link href="${ctx}/static/jquery-validation/1.11.1/validate.css" type="text/css" rel="stylesheet" />
<link href="${ctx}/static/styles/default.css" type="text/css" rel="stylesheet" />
<script src="${ctx}/static/jquery/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="${ctx}/static/jquery-validation/1.11.1/jquery.validate.min.js" type="text/javascript"></script>
<script src="${ctx}/static/jquery-validation/1.11.1/messages_bs_zh.js" type="text/javascript"></script>


<sitemesh:head/>
</head>

<body>
<div class="container">
<%@ include file="/WEB-INF/sitemesh/header.jsp"%>
<div id="content">
<sitemesh:body/>
</div>
<%@ include file="/WEB-INF/sitemesh/footer.jsp"%>
</div>
<script src="${ctx}/static/bootstrap/2.3.2/js/bootstrap.min.js" type="text/javascript"></script>
</body>
</html>


head.jsp

<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<div id="header">
<div id="title">
   <h1><a href="${ctx}">QuickStart示例</a><small>--TodoList应用演示</small>
  
</h1>
</div>
</div>



footer.jsp


<%@ page language="java" pageEncoding="UTF-8" %>
<div id="footer">
Copyright &copy; 2005-2012 <a href="http://www.springside.org.cn">springside.org.cn</a>
</div>


java代码


package com.ljtxh.daike.web.sitemesh;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/sitemesh")
public class SitemeshController {
@RequestMapping
public String list(){
return "test/list";
}
}



list.jsp


<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="reqUrl" value="http://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}" />

<html>
<head>
<title>sitemesh测试</title>
</head>

<body>

<h3>sitemesh测试</h3>
<h4>sitemesh API</h4>
<ul>
<li>获取任务列表 </li>
<li>获取任务(id=1)</li>
</ul>

<h4>修改API</h4>
<ul>
<li>创建任务 :</li>
<li>修改任务(id=1) </li>
</ul>
</body>
</html>

QQ图片20151205234700.png


本博客所有文章如无特别注明均为原创。作者:小天复制或转载请以超链接形式注明转自 钟天才的博客
原文地址《sitemesh简单例子

相关推荐

网友评论(0)