SiteMesh, SpringMVC, Shiro 配置
1. 首先在在web.xml文件中,加入SiteMesh和shiro的过滤器,保证SiteMesh的过滤器配置放在shiro的过滤器后面,不然的话,shiro的标签不能正确处理。
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
- <display-name>ems</display-name>
- <servlet>
- <servlet-name>dispatcherServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:config/spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatcherServlet</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:config/applicationContext.xml</param-value>
- </context-param>
- <filter>
- <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- shiro 安全过滤器 -->
- <filter>
- <filter-name>shiroFilter</filter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- <async-supported>true</async-supported>
- <init-param>
- <param-name>targetFilterLifecycle</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>shiroFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter>
- <filter-name>sitemesh</filter-name>
- <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>sitemesh</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <session-config>
- <session-timeout>30</session-timeout> <!-- 单位:分钟 -->
- </session-config>
- </web-app>
2. pom.xml中引入sitemesh的依赖
- <dependency>
- <groupId>org.sitemesh</groupId>
- <artifactId>sitemesh</artifactId>
- <version>3.0.1</version>
- </dependency>
3. 在WEB-INF下加入sitemesh的配置文件(sitemesh3.xml):
- <?xml version="1.0" encoding="UTF-8"?>
- <sitemesh>
- <!-- Map default decorator. This shall be applied to all paths if no other paths match. -->
- <mapping path="/*" decorator="/main.jsp" exclude="false" />
- <!-- Exclude path from decoration. -->
- <mapping path="/js/*" exclude="true" />
- <mapping path="/img/*" exclude="true" />
- <mapping path="/images/*" exclude="true" />
- <mapping path="/css/*" exclude="true" />
- <mapping path="/font-awesome/*" exclude="true" />
- <mapping path="/login" exclude="true" />
- <mapping path="/login.jsp" exclude="true" />
- </sitemesh>
main.jsp 为模板页面,<mapping exlude /> 表示不经过sitemesh处理的资源和页面。
4. 模板页面的书写
在模板页面中可以使用下面的标签作为占位符:
<sitemesh:write property='title' />
<sitemesh:write property='head' />
<sitemesh:write property='body' />
被装饰的页面,可以使用自己页面中的对应部分(title, head, body),填充在这些标签的位置。
下面看一个模板页面的例子:
- <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
- <%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
- <!DOCTYPE html>
- <html>
- <head>
- <title><sitemesh:write property='title' /></title>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css" />
- <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap-responsive.min.css" />
- <link rel="stylesheet" href="${pageContext.request.contextPath}/css/fullcalendar.css" />
- <link rel="stylesheet" href="${pageContext.request.contextPath}/css/matrix-login.css" />
- <link rel="stylesheet" href="${pageContext.request.contextPath}/css/matrix-style.css" />
- <link rel="stylesheet" href="${pageContext.request.contextPath}/css/matrix-media.css" />
- <link rel="stylesheet" href="${pageContext.request.contextPath}/font-awesome/css/font-awesome.css" />
- <link rel="stylesheet" href="${pageContext.request.contextPath}/css/jquery.gritter.css" />
- <link rel='stylesheet' href='http://fonts.googleapis.com/css?family=Open+Sans:400,700,800' />
- <style type="text/css">
- #top_header{
- margin-left:80px;
- margin-right:80px;
- padding-top:9px;
- font-size:30px;
- display:block;
- width:500px;
- }
- #top_logout{
- margin-right:2px;
- }
- #cur_user{
- margin-top:9px;
- display:block;
- }
- .tc{ background-color:#e6e6e6; border:#016574 2px solid; display:none;
- position:fixed; top:39%; left:40%; -moz-border-radius: 5px; -khtml-border-radius: 5px;
- -webkit-border-radius: 5px; border-radius: 5px; padding:19px 35px; font-size:14px;
- }
- </style>
- <sitemesh:write property='head' />
- </head>
- <body>
- <!--Header-part-->
- <div id="header">
- <h1><a href="javascript:;">Admin</a></h1>
- </div>
- <!--close-Header-part-->
- <!--top-Header-menu-->
- <div id="user-nav" class="navbar navbar-inverse">
- <ul class="nav">
- <li class=""><span id="top_header">XXX系统</span></li>
- <li><span id="cur_user">当前登陆用户:${user.username}</span></li>
- <li id="top_logout" style="float:right;"><a title="" href="/ems/logout"><i class="icon icon-share-alt"></i> <span class="text">Logout</span></a></li>
- </ul>
- </div>
- <!--close-top-Header-menu-->
- <!--sidebar-menu-->
- <div id="sidebar"><a href="javascript:;" class="visible-phone"><i class="icon icon-home"></i> Dashboard</a>
- <ul>
- <shiro:hasAnyRoles name="student,teacher">
- <li id="li_queryScore"><a href="/ems/user/queryScore"><i class="icon icon-home"></i><span>查询成绩</span></a></li>
- </shiro:hasAnyRoles>
- <shiro:hasAnyRoles name="teacher,admin">
- <li id="li_showStudentInfo"><a href="/ems/student/showStudentInfo"><i class="icon icon-home"></i><span>查询学生信息</span></a></li>
- </shiro:hasAnyRoles>
- <shiro:hasAnyRoles name="admin">
- <li id="li_showTeacherInfo"><a href="/ems/teacher/showTeacherInfo"><i class="icon icon-home"></i><span>查询教师信息</span></a></li>
- </shiro:hasAnyRoles>
- <shiro:hasAnyRoles name="teacher,admin">
- <li><a href="/ems/html/tables.html"><i class="icon icon-th"></i><span>统计</span></a></li>
- </shiro:hasAnyRoles>
- <shiro:hasAnyRoles name="student,teacher,admin">
- <li><a href="/ems/html/grid.html"><i class="icon icon-fullscreen"></i><span>打印</span></a></li>
- <li id="li_password"><a href="/ems/user/password"><i class="icon icon-inbox"></i><span>密码修改</span></a> </li>
- </shiro:hasAnyRoles>
- <shiro:hasRole name="admin">
- <li id="li_showPrivilege"><a href="/ems/priv/showPrivilege"><i class="icon icon-fullscreen"></i><span>权限设置</span></a></li>
- </shiro:hasRole>
- <shiro:hasAnyRoles name="teacher,admin">
- <li id="li_setting"><a href="/ems/set/setting"><i class="icon icon-tint"></i><span>成绩比例和录入设置</span></a></li>
- </shiro:hasAnyRoles>
- <shiro:hasAnyRoles name="student,teacher">
- <li id="li_queryReExam"><a href="/ems/user/queryReExam"><i class="icon icon-pencil"></i><span>补考名单</span></a></li>
- <li id="li_queryReLearn"><a href="/ems/user/queryReLearn"><i class="icon icon-pencil"></i><span>重修名单</span></a></li>
- </shiro:hasAnyRoles>
- </ul>
- </div>
- <!--sidebar-menu-->
- <!--main-container-part-->
- <sitemesh:write property='body' />
- <!-- end-Footer-part -->
- </body>
- </html>
上面的模板页面使用了:<sitemesh:write property='title' />, <sitemesh:write property='head' />,<sitemesh:write property='body' />
然后我们看下被装饰的页面:
- <%@ page contentType="text/html; charset=UTF-8" %>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE html>
- <html>
- <head>
- <title>index</title>
- </head>
- <body>
- <!--main-container-part-->
- <div id="content">
- <!--breadcrumbs-->
- <div id="content-header">
- <div id="breadcrumb"> <a href="tables.html" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a></div>
- </div>
- <!--End-breadcrumbs-->
- <!--Action boxes-->
- <div class="container-fluid">
- <shiro:hasAnyRoles name="student,teacher">
- <input type="hidden" name="user_type" id="user_type" value="${user.userType}" />
- 学年:<select id="learn_time" name="learn_time">
- <option value="2014">2014</option>
- <option value="2015" selected = "selected">2015</option>
- <option value="2016">2016</option>
- <option value="2017">2017</option>
- </select>
- 学期:<input type="radio" name="which_term" value="1" />上学期
- <input type="radio" name="which_term" value="2" />下学期
- </shiro:hasAnyRoles>
- <c:if test="${user.userType == '1'}">
- <input type="button" value="查询" id="queryScoreForStudent"/>
- </c:if>
- <c:if test="${user.userType == '2'}">
- <br/>
- 班级:<input type="text" name="className" id="class_name" />
- 课程:
- <select id="course_type_select" name="courseId">
- <c:forEach items="${courseList }" var="course" varStatus="status">
- <c:if test="${status.index == 0}">
- <option selected="selected" value="${course.id}"><c:out value="${course.name}"/></option>
- </c:if>
- <c:if test="${status.index > 0}">
- <option value="${course.id}"><c:out value="${course.name}"/></option>
- </c:if>
- </c:forEach>
- </select>
- <input type="button" value="查询课程成绩" id="queryScoreForTeacher"/>
- </c:if>
- <div class="row-fluid">
- <div class="span12">
- <div class="widget-box">
- <div class="widget-content nopadding">
- <table class="table table-bordered table-striped" id="score_table">
- </table>
- </div>
- </div>
- </div>
- </div>
- </div>
- <!--End-Action boxes-->
- </div>
- <!--end-main-container-part-->
- <!--Footer-part-->
- <div class="row-fluid">
- <div id="footer" class="span12"> 2013 © Matrix Admin. Brought to you by <a href="http://themedesigner.in/">Themedesigner.in</a> </div>
- </div>
- <!-- end-Footer-part -->
- <script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
- <script src="${pageContext.request.contextPath}/js/highLight.js"></script>
- <script type="text/javascript">
- $.namespace("ems.index");
- ems.index = function(){
- var divShow = $("#DivShow");
- return {
- queryScoreForStudent : function(){
- var time = $.trim($("#learn_time").val());
- var term = $.trim($("input[name='which_term']:checked").val());
- var indata = {time:time, term:term};
- $.post("/ems/user/queryScoreForStudent",indata, function(data){
- $("#score_table").empty();
- if(data != null && data.result == 'ok'){
- var d = data.data;
- console.log(d);
- if(data.data != null){
- var score_html = '<thead><tr><th>课程</th><th>分数</th><th>老师</th><th>时间</th></tr></thead><tbody>';
- for(var i=0; i<d.length; i++){
- score_html += '<tr class="odd gradeX"><td>' + d[i].courseName + '</td>';
- score_html += '<td>' + d[i].score + '</td>';
- score_html += '<td>' + d[i].teacherName + '</td>';
- score_html += '<td>' + new Date(d[i].learnTime).format("yyyy-MM-dd"); + '</td>';
- }
- score_html += '</tbody>';
- $("#score_table").empty().append(score_html);
- }
- }
- },'json');
- },
- queryScoreForTeacher : function(){
- var courseId = $.trim($("#course_type_select").val());
- if(courseId == null || courseId == '')
- return;
- var className = $.trim($("#class_name").val());
- var time = $.trim($("#learn_time").val());
- var term = $.trim($("input[name='which_term']:checked").val());
- var indata = {courseId:courseId, time:time, term:term, className:className};
- $.post("/ems/user/queryScoreForTeacher", indata, function(data){
- $("#score_table").empty();
- if(data != null && data.result == 'ok'){
- var d = data.data;
- console.log(d);
- if(data.data != null){
- var score_html = '<thead><tr><th>学号</th><th>姓名</th><th>专业</th><th>课程</th>'
- + '<th>分数</th><th>时间</th><th>录入成绩</th></tr></thead><tbody>';
- for(var i=0; i<d.length; i++){
- score_html += '<tr class="odd gradeX"><td>' + d[i].no + '</td>';
- score_html += '<td>' + d[i].studentName + '</td>';
- score_html += '<td>' + d[i].major + '</td>';
- score_html += '<td>' + d[i].courseName + '</td>';
- score_html += '<td>' + d[i].score + '</td>';
- score_html += '<td>' + new Date(d[i].learnTime).format("yyyy-MM-dd"); + '</td>';
- score_html += '<td><input type="button" id="'+ d[i].id + '" name="'+ d[i].flag
- + '" value="录入成绩" onclick="ems.index.showDiv(this)"/></td>';
- }
- score_html += '</tbody>';
- $("#score_table").empty().append(score_html);
- }
- }
- },'json');
- },
- showDiv : function(myThis){
- divShow.find("#student_course_id").val(myThis.id);
- divShow.find("#student_course_flag").val(myThis.name);
- divShow.show();
- },
- closeDiv : function(){
- divShow.find("#student_course_id").val("");
- divShow.find("#student_course_flag").val("");
- divShow.hide();
- },
- updateStudentScore : function(){
- var value1 = divShow.find("#value1").val();
- var value2 = divShow.find("#value2").val();
- var value3 = divShow.find("#value3").val();
- var id = divShow.find("#student_course_id").val();
- var flag = divShow.find("#student_course_flag").val();
- var indata = {id:id,flag:flag,score1:value1,score2:value2,score3:value3};
- $.post("/ems/user/updateStudentScore", indata, function(data){
- if(data != null && data.result == 'ok'){
- console.log(data.msg);
- ems.index.queryScoreForTeacher();
- }else{
- alert(data.msg);
- }
- }, 'json');
- ems.index.closeDiv();
- },
- createLearnTimeSelect : function(){
- var begin = 2008;
- var current_year = new Date().format("yyyy");
- var sel_html = '';
- for(var i=0; i<20; i++){
- if(begin+i == current_year)
- sel_html +='<option value="' + (begin+i) +'" selected = "selected">'+ (begin+i) +"</option>";
- sel_html += '<option value="' + (begin+i) +'">'+ (begin+i) +"</option>";
- }
- $("#learn_time").empty().append(sel_html);
- }
- };
- }();
- $(document).ready(function(){
- ems.index.createLearnTimeSelect(); // 生成学年下拉框
- $(document).on('click', '#queryScoreForTeacher', ems.index.queryScoreForTeacher);
- $(document).on('click', '#queryScoreForStudent', ems.index.queryScoreForStudent);
- $(document).on('click', '#btnCancel', ems.index.closeDiv);
- $(document).on('click', '#btnOK', ems.index.updateStudentScore);
- });
- </script>
- </body>
- </html>
我们看到被装饰页面的整个 body 部分,会填充到装饰页面中的 <sitemesh:write property='body' /> 处。
正好是注释: <!--main-container-part--> 和 <!-- end-Footer-part --> 相匹配和对应。
注意:模板页面的 <sitemesh:write property='head' /> 最好放置head标签的最后面,因为被装饰页面中的head部分由可能存在依赖于模板页面中的js库。
我们看到sitemesh的使用还是十分简单的。
SiteMesh, SpringMVC, Shiro 配置的更多相关文章
- SpringMVC+Shiro权限管理【转】
1.权限的简单描述 2.实例表结构及内容及POJO 3.Shiro-pom.xml 4.Shiro-web.xml 5.Shiro-MyShiro-权限认证,登录认证层 6.Shiro-applica ...
- SpringMVC+Shiro权限管理
什么是权限呢?举个简单的例子: 我有一个论坛,注册的用户分为normal用户,manager用户.对论坛的帖子的操作有这些:添加,删除,更新,查看,回复我们规定:normal用户只能:添加,查看,回复 ...
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(二)基于SpringMVC+Shiro的用户登录权限验证
序: 在上一篇中,咱们已经对于项目已经做了基本的配置,这一篇文章开始学习Shiro如何对登录进行验证. 教学: 一.Shiro配置的简要说明. 有心人可能注意到了,在上一章的applicationCo ...
- springmvc shiro UnauthorizedException 异常解决方案
springMVC 整合 shiro 时,配置了当访问某个URL没有权限时的配置处理: <!-- 通过unauthorizedUrl指定没有权限操作时跳转页面 --><propert ...
- springmvc shiro整合cas单点登入
shiro cas分为登入跟登出 maven依赖: <dependency> <groupId>org.apache.shiro</groupId> <art ...
- (转)Springboot+shiro配置笔记+错误小结
springboot不像springmvc,它没有xml配置文件,那该如何配置shiro呢,其实也不难,用java代码+注解来解决这个问题.仅以此篇记录我对shiro的学习,如有对过客造成不便,实在抱 ...
- Springboot+shiro配置笔记+错误小结
软件152 尹以操 springboot不像springmvc,它没有xml配置文件,那该如何配置shiro呢,其实也不难,用java代码+注解来解决这个问题.仅以此篇记录我对shiro的学习,如有对 ...
- SpringMVC+Shiro权限管理(转载)
源码 http://pan.baidu.com/s/1pJzG4t1 SpringMVC+Shiro权限管理 博文目录 权限的简单描述 实例表结构及内容及POJO Shiro-pom.xml Shir ...
- springmvc+shiro+freemarker实现的安全及权限管理
本文讲述了基于springmvc+shiro实现安全管理,shiro+freemarker实现权限验证. 首先我们从web.xml开始: <?xml version="1.0" ...
随机推荐
- Python+Selenium进行UI自动化测试项目中,常用的小技巧1:读取excel表,转化成字典(dict)输出
从今天开始我将会把在项目中遇到的问题,以及常用的一些技巧来分享出来,以此来促进自己的学习和提升自己:更加方便我以后的查阅. 现在要说的是:用Python来读取excel表的数据,返回字典(dict), ...
- 使用.NET统计文件夹中文件总数
软件下载: http://hovertree.com/h/bjaf/hwqtjwjs.htm 截图: 使用方法:点击按钮,选择文件夹,就可以显示文件夹中包含的文件总数. 这个项目包含在HoverTre ...
- 【转载】JS获取屏幕大小
前些日子需要给项目的弹窗上面罩,因为项目左侧是树形菜单,右侧嵌套的iframe ,iframe 的内容不是固定大小,那么,面罩的大小也就不是固定的 因此,用到了JQuery获取当前页面的窗口大小,于是 ...
- 重新想象 Windows 8.1 Store Apps (79) - 控件增强: MediaElement, Frame
[源码下载] 重新想象 Windows 8.1 Store Apps (79) - 控件增强: MediaElement, Frame 作者:webabcd 介绍重新想象 Windows 8.1 St ...
- Dom4j 锁竞争性能低下解决
在最近的项目中使用 Dom4j 解析 xml 发现性能低下,有锁竞争的情况,解决如下: SAXParserFactory factory = new org.apache.xerces.jaxp.SA ...
- PHP调用SQL Server存储过程
一.安装SQL Server Driver for PHP 在微软官网上发现了这个东西,他提供了一套PHP对MS2005/2008操作的全新函数库,并且支持UTF8,作为PHP的扩展运行.看来 ...
- JS中检测数据类型的几种方式及优缺点
1.typeof 用来检测数据类型的运算符 typeof value 返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."string&quo ...
- sqlserver 死锁原因及解决方法
其实所有的死锁最深层的原因就是一个:资源竞争 表现一: 一个用户A 访问表A(锁住了表A),然后又访问表B,另一个用户B 访问表B(锁住了表B),然后企图访问表A,这时用户A由于用户B已经锁住表B,它 ...
- 如何启动或关闭oracle的归档(ARCHIVELOG)模式
参考文献: http://www.eygle.com/archives/2004/10/oracle_howtoeci.html 1.管理员身份连接数据库 C:\Users\Administrator ...
- MySQL全文索引应用简明教程
本文从以下几个方面介绍下MySQL全文索引的基础知识: MySQL全文索引的几个注意事项 全文索引的语法 几种搜索类型的简介 几种搜索类型的实例 全文索引的几个注意事项 搜索必须在类型为fulltex ...