SiteMesh,SpringMVC,Shiro構成
37194 ワード
1.まずweb.xmlファイルにSiteMeshとshiroのフィルタを入れ、SiteMeshのフィルタ配置をshiroのフィルタの後ろに置くことを保証します.そうしないと、shiroのラベルが正しく処理されません.
2.pom.xmlにsitemeshを導入する依存
3.WEB-INFの下にsitemeshのプロファイル(sitemesh 3.xml)を入れる:
main.jspはテンプレートページであり、はsitemesh処理を経ないリソースとページを表す.
4.テンプレートページの作成
テンプレートページでは、次のラベルをプレースホルダとして使用できます.
飾られたページは、自分のページの対応する部分(title,head,body)を使って、これらのラベルの位置に埋め込むことができます.
テンプレートページの例を次に示します.
上のテンプレートページは、を使用しています. ,
装飾されたページを見てみましょう
被装飾ページのbody部分全体が、装飾ページのに埋め込まれているのを見ました.
ちょうどコメント:とマッチングと対応.
注意:テンプレートページのは、被装飾ページのhead部分がテンプレートページに依存するjsライブラリが存在する可能性があるため、headラベルの一番後ろに配置することが望ましい.
sitemeshの使用は非常に簡単であることを見ました.
<?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のプロファイル(sitemesh 3.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はテンプレートページであり、
4.テンプレートページの作成
テンプレートページでは、次のラベルをプレースホルダとして使用できます.
飾られたページは、自分のページの対応する部分(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>
上のテンプレートページは、
装飾されたページを見てみましょう
<%@ 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の使用は非常に簡単であることを見ました.