一、介绍
主要技术:
(1)s2sh之间的整合
(2)dwr和s2sh整合(验证姓名是否相同)
(3)强大的jquery validator验证框架验证表单
(4)分页bean的编写
(5)过滤器,拦截器的编写
(6)泛型dao的编写
二、实例
1、数据库脚本
本例是采用mysql数据库,脚本如下:
- CREATE DATABASE /*!32312 IF NOT EXISTS*/`practice` /*!40100 DEFAULT CHARACTER SET gbk */;
- USE `practice`;
- DROP TABLE IF EXISTS `student`;
- CREATE TABLE `student` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `name` varchar(50) DEFAULT NULL,
- `age` int(11) DEFAULT NULL,
- `sex` varchar(1) DEFAULT NULL,
- `address` varchar(100) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=gbk;
- insert into `student`(`id`,`name`,`age`,`sex`,`address`) values (2,'张民生',24,'男','江苏淮安市'),(26,'李军',41,'男','江苏南京'),(27,'李丽丽',25,'女','浙江杭州'),(28,'周塔利',36,'男','黑龙江'),(29,'李丽',24,'女','湖北'),(30,'凯萨',21,'女','新疆乌鲁木齐');
- DROP TABLE IF EXISTS `users`;
- CREATE TABLE `users` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `username` varchar(50) DEFAULT NULL,
- `password` varchar(50) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;
- insert into `users`(`id`,`username`,`password`) values (1,'admin','123456');
2、表现层
(1)login.jsp(登录页面)
- <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
- <%@taglib prefix="s" uri="/struts-tags"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <html>
- <head>
- <base href="<%=basePath%>">
- <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/style.css" mce_href="${pageContext.request.contextPath}/css/style.css">
- <mce:script type="text/javascript" src="<%=path%><!--
- /js/jquery-1.4.2.min.js">
- // --></mce:script>
- <mce:script type="text/javascript" src="<%=path%><!--
- /js/jquery.validate_pack.js">
- // --></mce:script>
- <mce:script type="text/javascript" src="<%=path%><!--
- /js/login.js">
- // --></mce:script>
- </head>
- <body style="text-align: center" mce_style="text-align: center">
- <form action="user_login.action" method="post" id="loginForm">
- <s:actionmessage/>
- <p>
- <label for="username">用户名:</label>
- <input type="text" name="user.username" id="username"/>
- <span id="usernamespan"></span>
- </p>
- <p>
- <label for="password">密 码:</label>
- <input type="password" name="user.password" id="password"/>
- <span id="passwordspan"></span>
- </p>
- <input type="submit" value="提交" /><input type="reset" value="重置" />
- </form>
- </body>
- </html>
(2)logout.jsp(退出页面)
- <%@ page language="java" pageEncoding="ISO-8859-1"%>
- <%
- 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%>">
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="refresh" content="0; url=<%=path%>/login.jsp" />
- <title>My JSP 'logout.jsp' starting page</title>
- </head>
- <body>
- <%
- response.setHeader("Pragma","No-cache");
- response.setHeader("Cache-Control","no-cache");
- response.setHeader("Cache-Control", "no-store");
- response.setDateHeader("Expires",0);
- if(session!=null)
- session.invalidate();
- %>
- </body>
- </html>
(3)exception(错误异常显示页面)
- <%@ page contentType="text/html; charset=gbk"%>
- <%@taglib prefix="s" uri="/struts-tags"%>
- <html>
- <head>
- <title><s:text name="exception_title"/></title>
- </head>
- <body style="padding:10px;background-color:#D6D3CE;" mce_style="padding:10px;background-color:#D6D3CE;">
- <center><h2><s:text name="exception_title"/></h2></center>
- <font color="#FF0000"><b><s:text name="exception_prompt"/></b></font><br/>
- <textarea rows="22" cols="106">
- <!-- 输出异常信息内容 -->
- <s:property value="exception.message"/>
(4)studentList.jsp(学生列表页面)
- <%@ page language="java" import="java.util.*" pageEncoding="gbk"
- isELIgnored="false"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>学生信息管理列表</title>
- <mce:script type="text/javascript" src="<%=path%><!--
- /js/jquery-1.4.2.min.js">
- // --></mce:script>
- <mce:script type="text/javascript" src="<%=path%><!--
- /js/main.js">
- // --></mce:script>
- <mce:style type="text/css"><!--
- body {
- margin-left: 0px;
- margin-top: 0px;
- margin-right: 0px;
- margin-bottom: 0px;
- }
- .tabfont01 {
- font-family: "宋体";
- font-size: 9px;
- color: #555555;
- text-decoration: none;
- text-align: center;
- }
- .font051 {
- font-family: "宋体";
- font-size: 12px;
- color: #333333;
- text-decoration: none;
- line-height: 20px;
- }
- .font201 {
- font-family: "宋体";
- font-size: 12px;
- color: #FF0000;
- text-decoration: none;
- }
- .button {
- font-family: "宋体";
- font-size: 14px;
- height: 37px;
- }
- html {
- overflow-x: auto;
- overflow-y: auto;
- border: 0;
- }
- -->
- a:link {color:green;text-decoration:none
- }
- a:visited {color:red;text-decoration:none
- }
- a:hover {color:blue;text-decoration:overline;font-size:20pt
- }
- --></mce:style><style type="text/css" mce_bogus="1">body {
- margin-left: 0px;
- margin-top: 0px;
- margin-right: 0px;
- margin-bottom: 0px;
- }
- .tabfont01 {
- font-family: "宋体";
- font-size: 9px;
- color: #555555;
- text-decoration: none;
- text-align: center;
- }
- .font051 {
- font-family: "宋体";
- font-size: 12px;
- color: #333333;
- text-decoration: none;
- line-height: 20px;
- }
- .font201 {
- font-family: "宋体";
- font-size: 12px;
- color: #FF0000;
- text-decoration: none;
- }
- .button {
- font-family: "宋体";
- font-size: 14px;
- height: 37px;
- }
- html {
- overflow-x: auto;
- overflow-y: auto;
- border: 0;
- }
- -->
- a:link {color:green;text-decoration:none
- }
- a:visited {color:red;text-decoration:none
- }
- a:hover {color:blue;text-decoration:overline;font-size:20pt
- }</style>
- </head>
- <body style="text-align: center" mce_style="text-align: center">
- <form name="fom" id="fom" method="post" action="student_query.action">
- <table width="98%" border="0" align="center" cellpadding="0"
- cellspacing="0">
- <tr>
- <td colspan="2">
- <span style="text-align: center" mce_style="text-align: center">
- 当前用户:
- <s:property value="#session.user.username" />
- 登录时间:
- <mce:script type="text/javascript"><!--
- document.write(new Date().toLocaleString())
- // --></mce:script>
- </span>
- <span style="margin-right: 100px; float: right;" mce_style="margin-right: 100px; float: right;"><a
- class=style2 style="text-decoration: none;"
- href="<%=path%>/logout.jsp"><font
- color="red" size="3">退出</font> </a>
- </span>
- </td>
- </tr>
- <tr>
- <td width="880">
- 查询方式:
- <select name="querytype">
- <option <s:if test='querytype==1'>selected="selected"</s:if>
- value="1">
- 学生姓名
- </option>
- <option <s:if test='querytype==2'>selected="selected"</s:if>
- value="2">
- 性别
- </option>
- </select>
- <input name="condition" value="<s:property value='condition'/>"
- type="text" size="12" />
- <input type="submit" class="right-button02" value="查 询" />
- </td>
- <td width="20">
- <input type="button"
- onclick="javascript:window.location.href='<%=path%>/student_toAdd.action'"
- class="right-button02" value="添加">
- </td>
- </tr>
- </table>
- <table width="100%" border="0" cellpadding="4" cellspacing="1"
- bgcolor="#464646" class="newfont03">
- <thead>
- <tr class="CTitle">
- <td height="22" colspan="10" align="center"
- style="font-size: 16px">
- 学生信息管理列表
- </td>
- </tr>
- </thead>
- <tbody>
- <tr bgcolor="#EEEEEE" align="center">
- <td width="4%">
- 编号
- </td>
- <td width="15%" align="center" height="30">
- 姓名
- </td>
- <td width="10%" align="center" height="30">
- 性别
- </td>
- <td width="10%">
- 年龄
- </td>
- <td width="13%">
- 地址
- </td>
- <td>
- 操作
- </td>
- </tr>
- <s:if
- test="(pageListData.dataList!=null)&&(!pageListData.dataList.isEmpty())">
- <s:iterator value="#request.pageListData.dataList" id="u"
- status="st">
- <tr id="changecolor" bgcolor="#FFFFFF" align="center">
- <td>
- <s:property value="#st.index+1+(currentPage-1)*pageSize" />
- </td>
- <td>
- <s:property value="#u.name" />
- </td>
- <td>
- <s:property value="#u.sex" />
- </td>
- <td>
- <s:property value="#u.age" />
- </td>
- <td>
- <s:property value="#u.address" />
- </td>
- <td>
- <a
- href="${pageContext.request.contextPath}/student_toUpdate.action?stu.id=${u.id}&¤tPage=${currentPage}">修改</a>
- <a
- href="${pageContext.request.contextPath}/student_delete.action?stu.id=${u.id}"
- onclick="return confirm('删除后无法恢复,确定要删除吗')">删除</a>
- </td>
- </tr>
- </s:iterator>
- </s:if>
- </tbody>
- </table>
- <s:property value="#request.pageListData.footer" escape="false" />
- </form>
- </body>
- </html>
(5)studentEdit.jsp(学生信息修改页面)
- <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <%
- 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>
- <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/style.css" mce_href="${pageContext.request.contextPath}/css/style.css">
- <mce:script type="text/javascript" src="<%=path%><!--
- /js/jquery-1.4.2.min.js">
- // --></mce:script>
- <mce:script type="text/javascript" src="<%=path%><!--
- /js/jquery.validate_pack.js">
- // --></mce:script>
- <mce:script type="text/javascript" src="<%=path%><!--
- /js/student.js">
- // --></mce:script>
- <mce:script type='text/javascript' src="<%=request.getContextPath()%><!--
- /dwr/interface/studentService.js">
- // --></mce:script>
- <mce:script type='text/javascript' src="<%=request.getContextPath()%><!--
- /dwr/engine.js">
- // --></mce:script>
- <mce:script type='text/javascript' src="<%=request.getContextPath()%><!--
- /dwr/util.js">
- // --></mce:script>
- </head>
- <body style="text-align:center" mce_style="text-align:center">
- <form action="<%=request.getContextPath()%>/student_update.action" id="studentUpdateForm"
- method="post">
- <input type="hidden" name="stu.id" value="<s:property value='stu.id'/>"/>
- <input type="hidden" name="currentPage" value="<s:property value='currentPage'/>"/>
- 学生信息修改
- <p>
- <label class="required">
- 姓名:
- </label>
- <input type="text" id="name"
- value="<s:property value='stu.name'/>"
- name="stu.name" />
- <input type="hidden"
- value="<s:property value='stu.name'/>"
- id="stuname" />
- <span id="namespan"></span>
- </p>
- <p>
- <label class="required">
- 年龄:
- </label>
- <input type="text" id="chooseone"
- value="<s:property value='stu.age'/>"
- name="stu.age" />
- <span id="agespan"></span>
- </p>
- <p>
- <label class="required">
- 性别:
- </label>
- <input type="text" id="choosetwo" name="stu.sex" value="<s:property value='stu.sex'/>" />
- <span id="sexspan"></span>
- </p>
- <p>
- <label class="required">
- 地址:
- </label>
- <input type="text" id="choosethree" value="<s:property value='stu.address'/>"
- name="stu.address" />
- <span id="addressspan"></span>
- </p>
- <center>
- <input type="submit"
- id="send" value="提交" />
- <input type="button" value="返回" onclick="javascript:window.location.href='<%=path%>/student_query.action?currentPage=<s:property value='currentPage'/>'"/>
- </center>
- </form>
- </body>
- </html>
(6)studentAdd.jsp(学生信息添加页面)
- <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>学生信息添加</title>
- <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/style.css" mce_href="${pageContext.request.contextPath}/css/style.css">
- <mce:script type="text/javascript" src="<%=path%><!--
- /js/jquery-1.4.2.min.js">
- // --></mce:script>
- <mce:script type="text/javascript" src="<%=path%><!--
- /js/jquery.validate_pack.js">
- // --></mce:script>
- <mce:script type="text/javascript" src="<%=path%><!--
- /js/student.js">
- // --></mce:script>
- <mce:script type='text/javascript' src="<%=request.getContextPath()%><!--
- /dwr/interface/studentService.js">
- // --></mce:script>
- <mce:script type='text/javascript' src="<%=request.getContextPath()%><!--
- /dwr/engine.js">
- // --></mce:script>
- <mce:script type='text/javascript' src="<%=request.getContextPath()%><!--
- /dwr/util.js">
- // --></mce:script>
- </head>
- <body style="text-align: center" mce_style="text-align: center">
- <form action="<%=request.getContextPath()%>/student_add.action"
- method="post" id="studentForm">
- 学生信息添加
- <p>
- <label for="name" class="required">
- 姓名:
- </label>
- <input type="text" id="name" name="stu.name"/>
- <span id="namespan"></span>
- </p>
- <p>
- <label for="age" class="required">
- 年龄:
- </label>
- <input type="text" id="age" name="stu.age" />
- <span id="agespan"></span>
- </p>
- <p>
- <label for="sex" class="required">
- 性别:
- </label>
- <input type="text" id="sex" name="stu.sex" />
- <span id="sexspan"></span>
- </p>
- <p>
- <label for="address" class="required">
- 地址:
- </label>
- <input type="text" id="address" name="stu.address" />
- <span id="addressspan"></span>
- </p>
- <input type="submit" value="提 交" />
- <input type="button" value="返回"
- onclick="javascript:window.location.href='<%=path%>/student_query.action?currentPage=<s:property value='currentPage'/>'" />
- </form>
- </body>
- </html>
(7)js脚本
student.js(采用jquery validator验证框架验证表单以及采用dwr框架验证姓名是否存在)
- $(document).ready(function() {
- // 字符验证
- jQuery.validator.addMethod("stringCheck",
- function(value, element) {
- return this.optional(element)
- || /^[/u0391-/uFFE5/w]+$/.test(value);
- }, "只能包括中文字、英文字母、数字和下划线");
- // 中文字两个字节
- jQuery.validator.addMethod("byteRangeLength", function(value, element,
- param) {
- var length = value.length;
- for ( var i = 0; i < value.length; i++) {
- if (value.charCodeAt(i) > 127) {
- length++;
- }
- }
- return this.optional(element)
- || (length >= param[0] && length <= param[1]);
- }, "请确保输入的值在3-15个字节之间(一个中文字算2个字节)");
- //结合dwr框架验证姓名是否存在
- jQuery.validator.addMethod("checkName", function(value, element) {
- var flag = false;
- dwr.engine.setAsync(false);
- studentService.isExistName(value,"add",function(data) {
- flag = !data;
- });
- dwr.engine.setAsync(true);
- return flag;
- }, "用户名已经存在,请重新输入!");
- jQuery.validator.addMethod("checkNameForUpdate", function(value, element) {
- var flag = false;
- dwr.engine.setAsync(false);
- var temp=document.getElementById("stuname").value==value;
- studentService.isExistName(value+":"+temp,"update",function(data) {
- flag = !data;
- });
- dwr.engine.setAsync(true);
- return flag;
- }, "用户名已经存在,请重新输入!");
- // 验证年龄 1-149之间
- jQuery.validator.addMethod("ageCheck", function(value, element) {
- return this.optional(element)
- || /^([1-9]{1}|[1-9]{1}[0-9]{1}|1[1-4]{1}[0-9]{1})$/
- .test(value);
- }, "只能1-149之间的数字!");
- // 验证性别
- jQuery.validator.addMethod("sexCheck",
- function(value, element) {
- return this.optional(element)
- || /^[/u4E00-/u9FFF]{1}$/.test(value);
- }, "只能是一个汉字");
- //// 身份证号码验证
- //jQuery.validator.addMethod("isIdCardNo", function(value, element) {
- // return this.optional(element) || isIdCardNo(value);
- //}, "请正确输入您的身份证号码");
- //
- //// 手机号码验证
- //jQuery.validator.addMethod("isMobile", function(value, element) {
- // var length = value.length;
- // var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+/d{8})$/;
- // return this.optional(element) || (length == 11 && mobile.test(value));
- //}, "请正确填写您的手机号码");
- //
- //// 电话号码验证
- //jQuery.validator.addMethod("isTel", function(value, element) {
- // var tel = /^/d{3,4}-?/d{7,9}$/; //电话号码格式010-12345678
- // return this.optional(element) || (tel.test(value));
- //}, "请正确填写您的电话号码");
- //
- //// 联系电话(手机/电话皆可)验证
- //jQuery.validator.addMethod("isPhone", function(value,element) {
- // var length = value.length;
- // var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+/d{8})$/;
- // var tel = /^/d{3,4}-?/d{7,9}$/;
- // return this.optional(element) || (tel.test(value) || mobile.test(value));
- //
- //}, "请正确填写您的联系电话");
- //
- //// 邮政编码验证
- //jQuery.validator.addMethod("isZipCode", function(value, element) {
- // var tel = /^[0-9]{6}$/;
- // return this.optional(element) || (tel.test(value));
- //}, "请正确填写您的邮政编码");
- //开始验证 studentUpdateForm
- $("#studentForm").validate( {
- /**//* 设置验证规则 */
- rules : {
- "stu.name" : {
- required : true,
- stringCheck : true,
- checkName : true,
- byteRangeLength : [ 3, 15 ]
- },
- "stu.age" : {
- required : true,
- ageCheck : true
- },
- "stu.sex" : {
- required : true,
- sexCheck : true
- },
- "stu.address" : {
- required : true,
- stringCheck : true,
- byteRangeLength : [ 3, 100 ]
- }
- },
- //设置错误信息
- messages : {
- "stu.name" : {
- required : "请填写用户名",
- stringCheck : "用户名只能包括中文字、英文字母、数字和下划线",
- checkName : "该姓名已经存在",
- byteRangeLength : "用户名必须在3-15个字符之间(一个中文字算2个字符)"
- },
- "stu.age" : {
- required : "请输入年龄",
- email : "请输入1-149之间的数字"
- },
- "stu.sex" : {
- required : "请输入您的性别",
- isPhone : "请输入一个汉字"
- },
- "stu.address" : {
- required : "请输入您的联系地址",
- stringCheck : "请正确输入您的联系地址",
- byteRangeLength : "请详实您的联系地址以便于我们联系您"
- }
- },
- errorPlacement : function(error, element) {
- element.parent().find("span").html(error)
- },
- success : function(label) {
- label.html(" ").addClass("ok");
- }
- });
- //开始验证 修改页面
- $("#studentUpdateForm").validate( {
- /**//* 设置验证规则 */
- rules : {
- "stu.name" : {
- required : true,
- stringCheck : true,
- checkNameForUpdate : true,
- byteRangeLength : [ 3, 15 ]
- },
- "stu.age" : {
- required : true,
- ageCheck : true
- },
- "stu.sex" : {
- required : true,
- sexCheck : true
- },
- "stu.address" : {
- required : true,
- stringCheck : true,
- byteRangeLength : [ 3, 100 ]
- }
- },
- //设置错误信息
- messages : {
- "stu.name" : {
- required : "请填写用户名",
- stringCheck : "用户名只能包括中文字、英文字母、数字和下划线",
- checkNameForUpdate : "该姓名已经存在",
- byteRangeLength : "用户名必须在3-15个字符之间(一个中文字算2个字符)"
- },
- "stu.age" : {
- required : "请输入年龄",
- email : "请输入1-149之间的数字"
- },
- "stu.sex" : {
- required : "请输入您的性别",
- isPhone : "请输入一个汉字"
- },
- "stu.address" : {
- required : "请输入您的联系地址",
- stringCheck : "请正确输入您的联系地址",
- byteRangeLength : "请详实您的联系地址以便于我们联系您"
- }
- },
- errorPlacement : function(error, element) {
- element.parent().find("span").html(error)
- },
- success : function(label) {
- label.html(" ").addClass("ok");
- }
- });
- });
main.js
- //检查分页列表页面页码输入框
- function checkCurrentPage(input, totalPages) {
- if (!input.match(/^/d*$/)) {
- alert("只能输入数字");
- document.getElementById("jumpPageBox").value = document
- .getElementById("pages").value;
- document.getElementById("jumpPageBox").focus();
- } else if (input > totalPages || parseInt(input) <= 0) {
- alert("对不起,你输入的页码有误!");
- document.getElementById("jumpPageBox").value = document
- .getElementById("pages").value;
- document.getElementById("jumpPageBox").focus();
- }
- }
- //去掉前后空格
- function trim(s) {
- try {
- return s.replace(/(^/s*)|(/s*$)/g, "");
- } catch (e) {
- return s;
- }
- }
- //给列表中id为changecolor的那些行变色
- $(document).ready(function() {
- $("table tbody tr[id*='changecolor']").mouseover(function() {
- $(this).css("background", "orange");
- }).mouseout(function() {
- $(this).css("background", "white");
- })
- });
login.js(采用jquery validator验证框架验证登录表单)
- $(document).ready(function() {
- $("#loginForm").validate( {
- /**//* 设置验证规则 */
- rules : {
- "user.username" : {
- required : true
- },
- "user.password" : {
- required : true
- }
- },
- //设置错误信息
- messages : {
- "user.username" : {
- required : "请填写用户名"
- },
- "user.password" : {
- required : "请填写密码"
- }
- },
- errorPlacement : function(error, element) {
- element.parent().find("span").html(error)
- },
- success : function(label) {
- label.html(" ").addClass("ok");
- }
- });
- });
除此之外,还用到jquery及其验证框架的js库,请自行下载
(8)CSS
style.css
- label.required:after {
- content:"(*)";
- color:red;
- font-family:"Lucida Grande",Verdana,Arial,Helvetica,sans-serif;
- }
- label.ok {
- background:url("../images/valid.gif") no-repeat;
- padding-left:16px;
- }
- label.error {
- color:#d00;
- text-transform:none;
- margin-left:6px;
- }
- label.choice {
- vertical-align:middle;
- font-weight:normal;
- text-transform:none;
- }
3、数据访问层及其业务层
(1)DAO层
BaseDAO.java
- package com.anxin.dao;
- import java.util.List;
- import com.anxin.util.PageListData;
- public interface BaseDAO<T,PK> {
- public void save(T entity);
- public void delete(T entity);
- public void deleteById(Class<T> entityClass, PK id);
- public void saveOrUpdate(T entity);
- public List<T> findAll(Class<T> entityClass);
- public PageListData findList(Class<T> entityClass, String hql, Object[] params, int currentPage, int pageSize);
- public List<T> findByProperty(Class<T> entityClass, String propertyName, Object value,int type);
- public T findById(Class<T> entityClass, PK id);
- }
BaseDAOImpl.java
- package com.anxin.dao.impl;
- import java.io.Serializable;
- import java.util.List;
- import org.hibernate.Query;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.anxin.dao.BaseDAO;
- import com.anxin.util.PageListData;
- public class BaseDAOImpl<T, PK extends Serializable> extends
- HibernateDaoSupport implements BaseDAO<T, PK> {
- static Logger logger = LoggerFactory.getLogger(BaseDAOImpl.class);
- // 保存
- public void save(T entity) {
- try {
- getHibernateTemplate().save(entity);
- } catch (RuntimeException re) {
- throw re;
- }
- }
- // 保存或者更新
- public void saveOrUpdate(T entity) {
- try {
- getHibernateTemplate().merge(entity);
- } catch (RuntimeException re) {
- throw re;
- }
- }
- // 删除
- public void delete(T entity) {
- try {
- getHibernateTemplate().delete(entity);
- } catch (RuntimeException re) {
- throw re;
- }
- }
- // 根据id删除某个对象
- public void deleteById(Class<T> entityClass, PK id) {
- try {
- getHibernateTemplate().delete(findById(entityClass, id));
- } catch (RuntimeException re) {
- throw re;
- }
- }
- // 根据id加载某个对象
- public T findById(Class<T> entityClass, PK id) {
- try {
- return (T) getHibernateTemplate().get(entityClass, id);
- } catch (RuntimeException re) {
- logger.error("findById " + entityClass.getName() + " failed :{}",
- re);
- throw re;
- }
- }
- // 查找所有的对象
- public List<T> findAll(Class<T> entityClass) {
- try {
- return getHibernateTemplate().loadAll(entityClass);
- } catch (RuntimeException re) {
- throw re;
- }
- }
- // 根据某个属性及其值精确或模糊查找对象
- public List<T> findByProperty(Class<T> entityClass, String propertyName,
- Object value, int type) {
- String queryString = "";
- try {
- if (type == 1) {// type=1是精确查找
- queryString = "from " + entityClass.getName()
- + " as model where model." + propertyName + "= ?";
- } else if (type == 2) {// type=2是模糊查找
- queryString = "from " + entityClass.getName()
- + " as model where model." + propertyName + "like ?";
- }
- return getHibernateTemplate().find(queryString, value);
- } catch (RuntimeException re) {
- throw re;
- }
- }
- // 根据hql语句及其查询参数,当前页数,每页显示的数目得到分页列表
- public PageListData findList(Class<T> entityClass, String hql,
- Object[] params, int currentPage, int pageSize) {
- PageListData listdata=null;
- try {
- Query query = getSession().createQuery(getCountsHql(hql));
- if (null != params && 0 != params.length) {
- for (int i = 0; i < params.length; i++) {
- query.setParameter(i, params[i]);
- }
- } else {
- logger.warn("参数为空");
- }
- int total = ((Long) query.uniqueResult()).intValue();
- logger.debug("总记录数:", total);
- query = getSession().createQuery(hql);
- if (null != params && 0 != params.length) {
- for (int i = 0; i < params.length; i++) {
- query.setParameter(i, params[i]);
- }
- }
- if (0 != pageSize) {
- query.setFirstResult(
- (currentPage == 0 ? 0 : currentPage - 1) * pageSize)
- .setMaxResults(pageSize);
- }
- List data = query.list();
- listdata=new PageListData(total,pageSize,currentPage,
- data);
- } catch (RuntimeException re) {
- throw re;
- }
- return listdata;
- }
- private String getCountsHql(String hql) {
- int index = hql.indexOf("from");
- if (index != -1) {
- return "select count(*) " + hql.substring(index);
- }
- throw new RuntimeException("sql语句异常" + hql);
- }
- }
UserDAO.java
- package com.anxin.dao;
- import com.anxin.bean.User;
- public interface UserDAO extends BaseDAO<User,Integer>{
- public User checkLogin(User user);
- }
UserDAOImpl.java
- package com.anxin.dao.impl;
- import org.hibernate.Query;
- import com.anxin.bean.User;
- import com.anxin.dao.UserDAO;
- public class UserDAOImpl extends BaseDAOImpl<User,Integer> implements UserDAO{
- public User checkLogin(User user){
- String hql="from User u where u.username=? and u.password=?";
- Query q=getSession().createQuery(hql);
- q.setString(0, user.getUsername()).setString(1, user.getPassword());
- return q.list()!=null&&q.list().size()>0?(User)q.list().get(0):null;
- }
- }
StudentDAO.java
- package com.anxin.dao;
- import com.anxin.bean.Student;
- public interface StudentDAO extends BaseDAO<Student,Integer>{
- }
StudentDAOImpl.java
- package com.anxin.dao.impl;
- import com.anxin.bean.Student;
- import com.anxin.dao.StudentDAO;
- public class StudentDAOImpl extends BaseDAOImpl<Student,Integer> implements StudentDAO{
- }
(2)service层
StudentService.java
- package com.anxin.service;
- import java.util.List;
- import java.util.Map;
- import com.anxin.bean.Student;
- import com.anxin.bean.User;
- import com.anxin.util.PageListData;
- public interface StudentService{
- public void save(Student stu);
- public void delete(Student stu);
- public void deleteById(Integer id);
- public void update(Student stu);
- public PageListData findList(Map param, int currentPage, int pageSize);
- public boolean isExistSameProperty(Map param);
- public Student findById(Integer id);
- public List<Student> findAll();
- public List<Student> findByCondition(Map param,int type);
- }
StudentServiceImpl.java
- package com.anxin.service.impl;
- import java.util.List;
- import java.util.Map;
- import com.anxin.bean.Student;
- import com.anxin.bean.User;
- import com.anxin.dao.StudentDAO;
- import com.anxin.dao.UserDAO;
- import com.anxin.service.StudentService;
- import com.anxin.service.UserService;
- import com.anxin.util.PageListData;
- public class StudentServiceImpl implements StudentService {
- private StudentDAO dao;
- public void save(Student stu) {
- dao.save(stu);
- }
- public void delete(Student stu) {
- dao.delete(stu);
- }
- public void deleteById(Integer id) {
- dao.deleteById(Student.class, id);
- }
- public void update(Student stu) {
- dao.saveOrUpdate(stu);
- }
- public PageListData findList(Map param, int pageNum, int pageSize) {
- String hql = "from Student where 1=1 ";
- for (Object o : param.keySet()) {
- hql += " and " + o.toString() + " ? ";
- }
- Object params[] = param.values().toArray();
- return dao.findList(Student.class, hql, params, pageNum, pageSize);
- }
- public boolean isExistSameProperty(Map param) {
- return true;
- }
- public Student findById(Integer id) {
- return dao.findById(Student.class, id);
- }
- public List<Student> findAll() {
- return dao.findAll(Student.class);
- }
- public boolean isExistName(String name, String type) {
- boolean flag=false;
- if ("add".equals(type)) {
- List list = dao.findByProperty(Student.class, "name", name, 1);
- flag=(list != null && list.size() > 0 ? true : false);
- } else if ("update".equals(type)) {
- String temp[]=name.split(":");
- List list = dao.findByProperty(Student.class, "name", temp[0], 1);
- if(Boolean.parseBoolean(temp[1]))
- flag=(list!=null&&list.size()>1?true:false);
- else
- flag=(list!=null&&list.size()>0?true:false);
- }
- return flag;
- }
- public List<Student> findByCondition(Map param, int type) {
- return dao.findByProperty(Student.class, null, null, type);
- }
- public StudentDAO getDao() {
- return dao;
- }
- public void setDao(StudentDAO dao) {
- this.dao = dao;
- }
- }
UserService.java
- package com.anxin.service;
- import java.util.List;
- import java.util.Map;
- import com.anxin.bean.User;
- import com.anxin.util.PageListData;
- public interface UserService{
- public void save(User user);
- public void delete(User user);
- public void deleteById(Integer id);
- public void update(User user);
- public PageListData findList(Map param, int currentPage, int pageSize);
- public boolean isExistSameProperty(Map param);
- public User findById(Integer id);
- public List<User> findAll();
- public List<User> findByCondition(Map param,int type);
- public User checkLogin(User user);
- }
UserServiceImpl.java
- package com.anxin.service.impl;
- import java.util.List;
- import java.util.Map;
- import com.anxin.bean.User;
- import com.anxin.dao.UserDAO;
- import com.anxin.service.UserService;
- import com.anxin.util.PageListData;
- public class UserServiceImpl implements UserService {
- private UserDAO dao;
- public void save(User user) {
- dao.save(user);
- }
- public void delete(User user) {
- dao.delete(user);
- }
- public void deleteById(Integer id) {
- dao.deleteById(User.class, id);
- }
- public void update(User user) {
- dao.saveOrUpdate(user);
- }
- public PageListData findList(Map param, int pageNum, int pageSize) {
- String hql = "from User";
- Object params[] = param.values().toArray();
- return dao.findList(User.class, hql, params, pageNum, pageSize);
- }
- public boolean isExistSameProperty(Map param) {
- return true;
- }
- public User findById(Integer id) {
- return dao.findById(User.class, id);
- }
- public List<User> findAll() {
- return dao.findAll(User.class);
- }
- public List<User> findByCondition(Map param, int type) {
- return dao.findByProperty(User.class, null, null, type);
- }
- public User checkLogin(User user) {
- return dao.checkLogin(user);
- }
- public UserDAO getDao() {
- return dao;
- }
- public void setDao(UserDAO dao) {
- this.dao = dao;
- }
- }
(3)bean及其Hibernate实体映射文件
Student.java
- package com.anxin.bean;
- public class Student {
- private int id;
- private String name;
- private int age;
- private String sex;
- private String address;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- }
User.java
- package com.anxin.bean;
- public class User implements java.io.Serializable {
- private int id;
- private String username;
- private String password;
- public User(){}
- public User(String username,String password){
- this.username=username;
- this.password=password;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
Student.hbm.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.anxin.bean.Student" table="student" catalog="practice">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="native" />
- </id>
- <property name="name" type="java.lang.String">
- <column name="name" length="50"/>
- </property>
- <property name="age" type="java.lang.Integer">
- <column name="age"/>
- </property>
- <property name="sex" type="java.lang.String">
- <column name="sex" length="1"/>
- </property>
- <property name="address" type="java.lang.String">
- <column name="address" length="100"/>
- </property>
- </class>
- </hibernate-mapping>
User.hbm.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.anxin.bean.User" table="users" catalog="practice">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="native" />
- </id>
- <property name="username" type="java.lang.String">
- <column name="username" length="50"/>
- </property>
- <property name="password" type="java.lang.String">
- <column name="password" length="50"/>
- </property>
- </class>
- </hibernate-mapping>
4、逻辑处理Action层
BaseAction.java
- package com.anxin.struts.action;
- import com.anxin.util.OVLoadProperties;
- import com.anxin.util.PageListData;
- import com.opensymphony.xwork2.ActionSupport;
- public class BaseAction extends ActionSupport{
- protected PageListData pageListData;//分页组件
- protected int currentPage=1;//当前页
- protected int pageSize=Integer.parseInt(OVLoadProperties.getInstance().getProperties("pageSize"));//每页显示的数目
- protected int querytype;//查询类型
- protected String condition;//查询条件
- public PageListData getPageListData() {
- return pageListData;
- }
- public void setPageListData(PageListData pageListData) {
- this.pageListData = pageListData;
- }
- public int getCurrentPage() {
- return currentPage;
- }
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public int getQuerytype() {
- return querytype;
- }
- public void setQuerytype(int querytype) {
- this.querytype = querytype;
- }
- public String getCondition() {
- return condition==null?"":condition.trim();
- }
- public void setCondition(String condition) {
- this.condition=(condition==null?"":condition.trim());
- }
- }
UserAction.java
- package com.anxin.struts.action;
- import com.anxin.bean.User;
- import com.anxin.service.UserService;
- import com.opensymphony.xwork2.ActionContext;
- public class UserAction extends BaseAction{
- private UserService service;
- private User user;
- public String login(){
- user=service.checkLogin(user);
- if(user!=null){
- ActionContext.getContext().getSession().put("user", user);
- return "success";
- }
- else{
- this.addActionMessage("用户名或密码错误!");
- return "input";
- }
- }
- public UserService getService() {
- return service;
- }
- public void setService(UserService service) {
- this.service = service;
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- }
StudentAction.java
- package com.anxin.struts.action;
- import java.util.HashMap;
- import java.util.Map;
- import com.anxin.bean.Student;
- import com.anxin.service.StudentService;
- public class StudentAction extends BaseAction {
- private StudentService service;
- private Student stu;
- // 转到增加页面
- public String toAdd() {
- return "add";
- }
- // 添加学生
- public String add() {
- service.save(stu);
- return "success";
- }
- // 转到修页面
- public String toUpdate() {
- stu = service.findById(stu.getId());
- return "update";
- }
- // 更新信息
- public String update() {
- Student temp = service.findById(stu.getId());
- temp.setId(stu.getId());
- temp.setAddress(stu.getAddress());
- temp.setAge(stu.getAge());
- temp.setName(stu.getName());
- temp.setSex(stu.getSex());
- service.update(temp);
- return "success";
- }
- // 查询列表
- public String query() {
- Map<String, String> param = new HashMap<String, String>();
- if (!"".equals(condition)) {
- if (querytype == 1) {
- param.put("name like", "%" + condition + "%");
- } else if (querytype == 2) {
- param.put("sex =", condition);
- }
- }
- pageListData = service.findList(param, currentPage, pageSize);
- return "query";
- }
- // 删除学生
- public String delete() {
- service.deleteById(stu.getId());
- return "success";
- }
- public StudentService getService() {
- return service;
- }
- public void setService(StudentService service) {
- this.service = service;
- }
- public Student getStu() {
- return stu;
- }
- public void setStu(Student stu) {
- this.stu = stu;
- }
- }
5、公用的一些类
OpenSessionInViewFilter.java(重写spring包中的OpenSessionInViewFilter类,目的是解决事务提交出现的bug)
- package com.anxin.util;
- import org.hibernate.FlushMode;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.springframework.dao.DataAccessResourceFailureException;
- import org.springframework.orm.hibernate3.SessionFactoryUtils;
- public class OpenSessionInViewFilter extends org.springframework.orm.hibernate3.support.OpenSessionInViewFilter {
- /**
- * we do a different flushmode than in the codebase
- * here
- */
- protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
- Session session = SessionFactoryUtils.getSession(sessionFactory, true);
- session.setFlushMode(FlushMode.COMMIT);
- return session;
- }
- /**
- * we do an explicit flush here just in case
- * we do not have an automated flush
- */
- protected void closeSession(Session session, SessionFactory factory) {
- session.flush();
- super.closeSession(session, factory);
- }
- }
PageListData.java(分页组件)
- package com.anxin.util;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Locale;
- import javax.servlet.http.HttpServletRequest;
- public class PageListData {
- // 分页结果集
- private List dataList = null;
- // 记录总数
- private int totalcount = 0;
- // 每页显示记录数
- private int pageSize = 0;
- // 当前页数
- private int currentPage = 1;
- // 总页数
- private int totalPageCount = 1;
- //分页页脚
- private String footer;
- /*初始化分页组件*/
- public PageListData(int totalcount, int pageSize, int currentPage,
- List dataList) {
- setTotalcount(totalcount);
- setPageSize(pageSize);
- setCurrentPage(currentPage);
- setDataList(dataList);
- setFooter(getFooter());
- }
- /**
- * 封装分页栏函数 必需被包含在某个Form之中
- *
- * @return String pages 当前页号 pageSize 每页显示行数
- */
- public String getFooter() {
- StringBuffer pageStr = new StringBuffer("");
- pageStr.append("<center><p class='pages'>");
- int totalPages = getTotalPageCount(); // 总页数
- int prevPage = currentPage - 1; // 上一页
- int nextPage = currentPage + 1; // 下一页
- pageStr.append("<span style="color:#6795B4;" mce_style="color:#6795B4;">共有" + totalcount
- + "条记录</span> ");
- pageStr.append("<span style="color:#6795B4;" mce_style="color:#6795B4;">第" + currentPage + "页/共"
- + totalPages + "页</span> ");
- if (currentPage > 1)
- pageStr
- .append("<span><a style="cursor: pointer;text-decoration:underline;color:#6795B4" mce_style="cursor: pointer;text-decoration:underline;color:#6795B4"onclick='document.getElementById(/"pages/").value=1;document.getElementById(/"pages/").form.submit();'>首页</a></span> ");
- if (currentPage == 1)
- pageStr
- .append("<span style="color:#6795B4" mce_style="color:#6795B4">首页</span> ");
- if (currentPage > 1)
- pageStr
- .append("<span><a style="cursor: pointer;text-decoration:underline;color:#6795B4" mce_style="cursor: pointer;text-decoration:underline;color:#6795B4" onclick='document.getElementById(/"pages/").value="
- + prevPage
- + ";document.getElementById(/"pages/").form.submit();'>上一页</a></span> ");
- if (currentPage <= 1)
- pageStr
- .append("<span style="color:#6795B4" mce_style="color:#6795B4">上一页</span> ");
- if (currentPage < totalPages)
- pageStr
- .append("<span><a style="cursor: pointer;text-decoration:underline;color:#6795B4;" mce_style="cursor: pointer;text-decoration:underline;color:#6795B4;" onclick='document.getElementById(/"pages/").value="
- + nextPage
- + ";document.getElementById(/"pages/").form.submit();'>下一页</a></span> ");
- if (currentPage >= totalPages)
- pageStr
- .append("<span style="color:#6795B4;" mce_style="color:#6795B4;">下一页</span> ");
- if (currentPage < totalPages)
- pageStr
- .append("<span><a style="cursor: pointer;text-decoration:underline;color:#6795B4;" mce_style="cursor: pointer;text-decoration:underline;color:#6795B4;" onclick='document.getElementById(/"pages/").value="
- + totalPages
- + ";document.getElementById(/"pages/").form.submit();'>末页</a></span> ");
- if (currentPage == totalPages)
- pageStr
- .append("<span style="color:#6795B4;" mce_style="color:#6795B4;">末页</span> ");
- pageStr
- .append("<span style="color:#6795B4;" mce_style="color:#6795B4;">跳转至第:<input type='text' value='"
- + currentPage
- + "'id='jumpPageBox' size='2' onblur='checkCurrentPage(document.getElementById(/"jumpPageBox/").value,"
- + totalPages
- + ")'/>页<input class='right-button02' type='button' value='跳转' onclick='document.getElementById(/"pages/").value=document.getElementById(/"jumpPageBox/").value;document.getElementById(/"pages/").form.submit();'/></span>");
- pageStr.append("</p></center>");
- pageStr.append("<input type='hidden' value='" + currentPage
- + "' name='currentPage' id='pages' />");
- pageStr.append("<input type='hidden' value='" + pageSize
- + "' name='pageSize' id='pageSize' />");
- return pageStr.toString();
- }
- public List getDataList() {
- return dataList;
- }
- public void setDataList(List dataList) {
- this.dataList = dataList;
- }
- public int getTotalcount() {
- return totalcount;
- }
- public void setTotalcount(int totalcount) {
- this.totalcount = totalcount;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public int getCurrentPage() {
- return currentPage;
- }
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- //计算总页数,如果为0则置为1
- public int getTotalPageCount() {
- int p;
- if (totalcount % pageSize == 0) {
- p=totalcount / pageSize;
- } else {
- p=totalcount / pageSize + 1;
- }
- return p==0?1:p;
- }
- public void setTotalPageCount(int totalPageCount) {
- this.totalPageCount = totalPageCount;
- }
- public void setFooter(String footer) {
- this.footer = footer;
- }
- }
OVLoadProperties.java(读取properties文件的内容)
- package com.anxin.util;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- //单例模式实现读取zxc.properties文件的内容
- public class OVLoadProperties {
- // 声明一个自己的实例
- private static OVLoadProperties instance = new OVLoadProperties();
- final static String fileName = "/config.properties";
- // 返回该实例
- public static synchronized OVLoadProperties getInstance() {
- return instance;
- }
- // 获取key所对应的值
- public String getProperties(String key) {
- Properties p = new Properties();
- InputStream is = null;
- try {
- // zxc.properties文件放在src目录的下边
- is = OVLoadProperties.class.getResourceAsStream(fileName);
- if (is == null)
- is = new FileInputStream(fileName);
- p.load(is);
- } catch (Exception e) {
- System.out.println("加载文件出错啦!" + e.getMessage());
- } finally {
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- System.out.println(e.getMessage());
- }
- }
- }
- return p.getProperty(key);
- }
- }
confic.properties
- pageSize=5
log4j.properties
- log4j.debug=true
- log4j.rootLogger=DEBUG,A1
- log4j.appender.A1=org.apache.log4j.ConsoleAppender
- log4j.appender.A1.Threshold=info
- log4j.appender.A1.layout=org.apache.log4j.PatternLayout
- log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n
6、过滤器和拦截器
LoginFilter.java(jsp页面过滤器)
- package com.anxin.filter;
- import java.io.IOException;
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import com.anxin.bean.User;
- public class LoginFilter implements Filter{
- public void destroy(){
- }
- public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException{
- HttpSession session=((HttpServletRequest)request).getSession();
- ((HttpServletResponse)response).setHeader("Pragma","No-cache");
- ((HttpServletResponse)response).setHeader("Cache-Control","no-cache");
- ((HttpServletResponse)response).setHeader("Cache-Control", "no-store");
- ((HttpServletResponse)response).setDateHeader("Expires",0);
- User user=(User)session.getAttribute("user");
- String url=(((HttpServletRequest)request).getRequestURI());
- if(url.endsWith("/login.jsp")||url.endsWith("/BaseProject/")){
- chain.doFilter(request, response);
- }else{
- if(session.getAttribute("user")==null){
- ((HttpServletResponse)response).sendRedirect(((HttpServletRequest)request).getContextPath()+"/login.jsp");
- }else{
- chain.doFilter(request, response);
- }
- }
- }
- public void init(FilterConfig filterConfig)throws ServletException{
- }
- }
LoginedCheckInterceptor(action拦截器)
- package com.anxin.struts.interceptor;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts2.ServletActionContext;
- import com.anxin.bean.User;
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
- /** session过期、登录有效性及操作的权限验证拦截器 */
- public class LoginedCheckInterceptor extends AbstractInterceptor {
- /** 拦截请求并进行登录有效性验证 */
- public String intercept(ActionInvocation ai) throws Exception {
- //取得请求的URL
- String url = ServletActionContext.getRequest().getRequestURL().toString();
- HttpServletResponse response=ServletActionContext.getResponse();
- response.setHeader("Pragma","No-cache");
- response.setHeader("Cache-Control","no-cache");
- response.setHeader("Cache-Control", "no-store");
- response.setDateHeader("Expires",0);
- User user = null;
- //对登录与注销请求直接放行,不予拦截
- if (url.indexOf("user_login.action")!=-1 || url.indexOf("logout.action")!=-1){
- return ai.invoke();
- }
- else{
- //验证Session是否过期
- if(!ServletActionContext.getRequest().isRequestedSessionIdValid()){
- //session过期,转向session过期提示页,最终跳转至登录页面
- return "tologin";
- }
- else{
- user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
- //验证是否已经登录
- if (user==null){
- //尚未登录,跳转至登录页面
- return "tologin";
- }else{
- return ai.invoke();
- }
- }
- }
- }
- }
7、S2SH整合核心配置文件
struts.properties
- struts.i18n.encoding = gbk
- struts.custom.i18n.resources=messageResource
- struts.locale=zh_CN
- struts.objectFactory=spring
- struts.devMode=true
- struts.ui.theme=simple
- struts.ui.templateDir=template
- struts.ui.templateSuffix=ftl
struts.xml
- <?xml version="1.0" encoding="gbk"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
- <package name="anxin" extends="struts-default">
- <!-- 配置自定义拦截器LoginedCheckInterceptor -->
- <interceptors>
- <interceptor name="loginedCheck" class="com.anxin.struts.interceptor.LoginedCheckInterceptor"/>
- <interceptor-stack name="mystack">
- <interceptor-ref name="loginedCheck" />
- <interceptor-ref name="defaultStack" />
- </interceptor-stack>
- </interceptors>
- <!-- 定义全局result -->
- <global-results>
- <!-- 定义名为exception的全局result -->
- <result name="exception">exception.jsp</result>
- <result name="tologin">login.jsp</result>
- </global-results>
- <!-- 定义全局异常映射 -->
- <global-exception-mappings>
- <!-- 捕捉到Exception异常(所有异常)时跳转到exception所命名的视图上 -->
- <exception-mapping exception="java.lang.Exception" result="exception"/>
- </global-exception-mappings>
- <action name="user_*" class="userAction" method="{1}">
- <result name="input">login.jsp</result>
- <result name="success" type="redirect">student_query.action</result>
- <interceptor-ref name="mystack" />
- </action>
- <action name="student_*" class="studentAction" method="{1}">
- <result name="add">jsp/studentAdd.jsp</result>
- <result name="update">jsp/studentEdit.jsp</result>
- <result name="query">jsp/studentList.jsp</result>
- <result name="success" type="redirect">student_query.action</result>
- <interceptor-ref name="mystack" />
- </action>
- </package>
- </struts>
applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <!-- 定义使用C3P0连接池的数据源 -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <!-- 指定连接数据库的JDBC驱动 -->
- <property name="driverClass">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <!-- 连接数据库所用的URL -->
- <property name="jdbcUrl">
- <value>jdbc:mysql://localhost:3306/practice?useUnicode=true&characterEncoding=gbk</value>
- </property>
- <!-- 连接数据库的用户名 -->
- <property name="user">
- <value>root</value>
- </property>
- <!-- 连接数据库的密码 -->
- <property name="password">
- <value>123</value>
- </property>
- <!-- 设置数据库连接池的最大连接数 -->
- <property name="maxPoolSize">
- <value>20</value>
- </property>
- <!-- 设置数据库连接池的最小连接数 -->
- <property name="minPoolSize">
- <value>2</value>
- </property>
- <!-- 设置数据库连接池的初始化连接数 -->
- <property name="initialPoolSize">
- <value>2</value>
- </property>
- <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->
- <property name="maxIdleTime">
- <value>20</value>
- </property>
- </bean>
- <!-- 定义Hibernate的SessionFactory -->
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <!-- 依赖注入上面定义的数据源dataSource -->
- <property name="dataSource" ref="dataSource"/>
- <!-- 注册Hibernate的ORM映射文件 -->
- <property name="mappingResources">
- <list>
- <value>com/anxin/orm/mapping/User.hbm.xml</value>
- <value>com/anxin/orm/mapping/Student.hbm.xml</value>
- </list>
- </property>
- <!-- 设置Hibernate的相关属性 -->
- <property name="hibernateProperties">
- <props>
- <!-- 设置Hibernate的数据库方言 -->
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
- <!-- 设置Hibernate是否在控制台输出SQL语句,开发调试阶段通常设为true -->
- <prop key="show_sql">true</prop>
- <!-- 设置Hibernate一个提交批次中的最大SQL语句数 -->
- <prop key="hibernate.jdbc.batch_size">50</prop>
- </props>
- </property>
- </bean>
- <!--定义Hibernate的事务管理器HibernateTransactionManager -->
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <!-- 依赖注入上面定义的sessionFactory -->
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <!--定义Spring的事务拦截器TransactionInterceptor -->
- <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <!-- 依赖注入上面定义的事务管理器transactionManager -->
- <property name="transactionManager" ref="transactionManager"/>
- <!-- 定义需要进行事务拦截的方法及所采用的事务控制类型 -->
- <property name="transactionAttributes">
- <props>
- <!-- 以browse、list、load、get及is开头的所有方法采用只读型事务控制类型 -->
- <prop key="browse*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
- <!-- 所有方法均进行事务控制,如果当前没有事务,则新建一个事务 -->
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <!-- 定义BeanNameAutoProxyCreatorf进行Spring的事务处理-->
- <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <!-- 针对指定的bean自动生成业务代理 -->
- <property name="beanNames">
- <list>
- <value>userService</value>
- <value>studentService</value>
- </list>
- </property>
- <!-- 这个属性为true时,表示被代理的是目标类本身而不是目标类的接口 -->
- <property name="proxyTargetClass">
- <value>true</value>
- </property>
- <!-- 依赖注入上面定义的事务拦截器transactionInterceptor -->
- <property name="interceptorNames">
- <list>
- <value>transactionInterceptor</value>
- </list>
- </property>
- </bean>
- <!-- ************************************service和dao配置开始**************************** -->
- <!-- 装配通用数据库访问类BaseDAOImpl -->
- <bean id="userdao" class="com.anxin.dao.impl.UserDAOImpl">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <bean id="studentdao" class="com.anxin.dao.impl.StudentDAOImpl">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <bean id="userService" class="com.anxin.service.impl.UserServiceImpl">
- <property name="dao" ref="userdao"/>
- </bean>
- <bean id="studentService" class="com.anxin.service.impl.StudentServiceImpl">
- <property name="dao" ref="studentdao"/>
- </bean>
- <!-- ************************************service和dao配置结束**************************** -->
- <!-- ************************************Action配置开始**************************** -->
- <!-- 部署Struts2负责系统用户管理的控制器AdminAction -->
- <bean id="userAction" class="com.anxin.struts.action.UserAction" scope="prototype">
- <property name="service" ref="userService"/>
- </bean>
- <bean id="studentAction" class="com.anxin.struts.action.StudentAction" scope="prototype">
- <property name="service" ref="studentService"/>
- </bean>
- <!-- ************************************Action配置结束**************************** -->
- </beans>
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">
- <!-- 指定Spring的配置文件 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
- </context-param>
- <!-- 指定以Listener方式启动Spring容器 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 定义Struts2的核心控制器FilterDispathcer -->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <!-- 定义编码过滤器 -->
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>gbk</param-value>
- </init-param>
- </filter>
- <filter>
- <filter-name>hibernateFilter</filter-name>
- <filter-class>com.anxin.util.OpenSessionInViewFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>hibernateFilter</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>*.jsp</url-pattern>
- </filter-mapping>
- <filter>
- <filter-name>struts-cleanup</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts-cleanup</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter>
- <!-- 定义核心Filter的名字 -->
- <filter-name>LoginFilter</filter-name>
- <!-- 定义核心Filter的实现类 -->
- <filter-class>
- com.anxin.filter.LoginFilter
- </filter-class>
- </filter>
- <filter-mapping>
- <filter-name>LoginFilter</filter-name>
- <url-pattern>*.jsp</url-pattern>
- </filter-mapping>
- <servlet>
- <servlet-name>dwr</servlet-name>
- <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
- <!--此处指定项目处于开发之中,故可通过http://localhost:8000/dwrdemo1/dwr/,而不会出现403 -->
- <init-param>
- <param-name>debug</param-name>
- <param-value>true</param-value>
- </init-param>
- <init-param>
- <param-name>crossDomainSessionSecurity</param-name>
- <param-value>false</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>dwr</servlet-name>
- <url-pattern>/dwr/*</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>login.jsp</welcome-file>
- </welcome-file-list>
- <!-- 配置404与500错误处理 -->
- <error-page>
- <error-code>404</error-code>
- <location>/404.htm</location>
- </error-page>
- <error-page>
- <error-code>500</error-code>
- <location>/500.htm</location>
- </error-page>
- </web-app>
dwr.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
- <dwr>
- <!-- without allow, DWR isn't allowed to do anything -->
- <allow>
- <!-- <create creator="new" javascript="CountyDWR"> -->
- <!-- <param name="class">com.anxin.utils.CountyDWR</param> -->
- <!-- </create> -->
- <!-- <convert match="com.anxin.utils.DWRCity" converter="bean"> -->
- <!-- <param name="include" value="cityid,cityname"/> -->
- <!-- </convert> -->
- <create javascript="studentService" creator="spring">
- <param name="beanName" value="studentService"/>
- </create>
- </allow>
- </dwr>
三、运行效果
页面没怎么修饰,比较土哈
(1)登陆页面

(2)学生信息列表页面

(3)学生信息添加页面

(4)学生信息修改页面
