首页 > 代码库 > javaWeb校园宿舍管理解析(三)
javaWeb校园宿舍管理解析(三)
前两章主要集成Struts2以及mybatis,并进行的相应的小测试,现在我们来实现整个登录功能,说简单一下,登录功能就是登录页-->发送请求-->action-->dao-->主菜单。
Struts2做为一个声明性架构,struts.xml作为他的入口,用来定义动作默认地方,很多企业习惯将功能模块化,然后在引入struts.xml,因为我们这个项目比较简单,我就不模块化了,所有动作都定义在struts.xml中。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="strutsqs" extends="struts-default" namespace="/" > <action name="GoLogin" class="com.action.GoLogin"> <result name="input">/Login.jsp</result> <result name="success">/Index.jsp</result> </action> </package> </struts>
Struts2中的Action采用了低侵入式的设计,Struts2不要求Action类继承任何的Struts2的基类或实现Struts2接口。(但是,我们为了方便实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重写此类里的public String execute() throws Exception方法。因为此类中实现了很多的实用接口,提供了很多默认方法,这些默认方法包括获取国际化信息的方法、数据校验的方法、默认的处理用户请求的方法等,这样可以大大的简化Action的开发。)
Struts2中通常直接使用Action来封装HTTP请求参数,因此,Action类里还应该包含与请求参数对应的属性,并且为属性提供对应的getter和setter方法。(当然,Action类中还可以封装处理结果,把处理结果信息当作一属性,提供对应的getter和setter方法)。
登录核心action类,用来根据登录人类型不同进行不同的判断。
package com.Action; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.bean.AdminBean; import com.dao.AdminDao; import com.dao.impl.AdminDaoImpl; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class GoLogin extends ActionSupport{ private String Type; private String Username; private String Password; private String Msg; public String getType() { return Type; } public void setType(String type) { Type = type; } public String getUsername() { return Username; } public void setUsername(String username) { Username = username; } public String getPassword() { return Password; } public void setPassword(String password) { Password = password; } public String getMsg() { return Msg; } public void setMsg(String msg) { Msg = msg; } public String execute() throws Exception { if(Type.equals("系统管理员")) { AdminBean admin=new AdminBean(); admin.setAdmin_Name(Username); admin.setAdmin_Password(Password); AdminDao dao=new AdminDaoImpl(); if (dao.CheckLogin(admin)==null) { Msg = "用户名或者密码错误"; return INPUT; } else { //获取ID String Admin_ID=dao.CheckLogin(admin); //创建session HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("id", Admin_ID); session.setAttribute("type", "1"); return SUCCESS; } }else { Msg = "身份类型错误"; return INPUT; } } }
将Login.jsp中的form action属性修改为
<form name="form1" action="GoLogin.action" method="post" onSubmit="return mycheck()">
加入Index.jsp以及Left.jsp,这两个页面加入后,运行项目输入zhangsan/123,这是我们就可以进入主页面了,项目非常简单的权限控制,从账号、角色以及角色权限都是清一色的固定,不要小看简单的项目,往往一个项目的搭建都是从简至繁。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>校园宿舍管理系统</title> <base href="http://www.mamicode.com/"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="http://www.mamicode.com/Style/Style.css" rel="stylesheet" type="text/css" /> </head> <body> <center> <table width="900" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="60" bgcolor="#E6F5FF" style="color:#06F; font-size:19px; font-weight:bolder; padding-left:50px;">校园宿舍管理系统</td> </tr> <tr> <td height="30" background="Images/MenuBg.jpg"> </td> </tr> <tr> <td height="500" align="center" valign="top"><table width="900" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="191" height="500" align="center" valign="top" background="Images/leftbg.jpg"> <%@ include file="Left.jsp"%> </td> <td width="709" align="center" valign="middle" bgcolor="#F6F9FE"><h1>欢迎使用校园宿舍管理系统</h1></td> </tr> </table></td> </tr> <tr> <td height="35" background="Images/bootBg.jpg"> </td> </tr> </table> </center> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <link href="http://www.mamicode.com/Style/Style.css" rel="stylesheet" type="text/css" /> <table width="155" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="31" align="center" background="Images/left1.jpg"><strong>系统选项</strong></td> </tr> <tr> <td height="50" align="center" valign="top"><table width="150" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/Index.jsp">后台首页</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <%if(session.getAttribute("type").toString().equals("1")){%> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/TeacherManager.action">楼宇管理员管理</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/StudentManager.action">学生管理</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/BuildingManager.action">楼宇管理</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/DomitoryManager.action">宿舍管理</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/StudentRZ.action">学生入住登记</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/StudentTH.jsp">学生寝室调换</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/StudentQC.jsp">学生迁出登记</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/AdminLog.action">学生缺寝记录</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/OutList.action">迁出记录</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <%}%> <%if(session.getAttribute("type").toString().equals("2")){%> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/MyStudent.action">学生管理</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/MyLog.action">学生缺寝记录</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <%}%> <%if(session.getAttribute("type").toString().equals("3")){%> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/StudentLog.action">我的缺寝记录</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <%}%> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/PasswordUpdate.jsp">修改密码</a></td> </tr> <tr> <td height="5" align="center"><img src="http://www.mamicode.com/Images/ic.gif" width="1" height="1"></td> </tr> <tr> <td height="30" align="center" background="Images/left2.jpg" style="text-align:left; padding-left:40px;"><a href="http://www.mamicode.com/Quit.action" onclick="return confirm(‘确定要退出系统吗?‘)">退出系统</a></td> </tr> </table> </td> </tr> </table>
下面我们来看看输入正确的账号后进入的主页:
以上已经完成管理员的初步,现在我们分别创建楼宇管理者以及学生类型的验证,同admin一样,先建表,创建实体类,创建mybatis映射文件然后创建dao并在登录的action中添加楼宇管理者以及学生类型的判断操作,大致如下:
create table TEACHER ( teacher_id NUMBER, teacher_username VARCHAR2(20), teacher_password VARCHAR2(20), teacher_name VARCHAR2(20), teacher_sex VARCHAR2(10), teacher_tel VARCHAR2(20) )
create table STUDENT ( student_id NUMBER, student_username VARCHAR2(20), student_password VARCHAR2(20), student_name VARCHAR2(20), student_sex VARCHAR2(10), student_tel VARCHAR2(20) )
创建实体类:
package com.bean; public class TeacherBean { private int Teacher_ID ; private String Teacher_Username ; private String Teacher_Password ; private String Teacher_Name ; private String Teacher_Sex ; private String Teacher_Tel ; public int getTeacher_ID() { return Teacher_ID; } public void setTeacher_ID(int teacherID) { Teacher_ID = teacherID; } public String getTeacher_Username() { return Teacher_Username; } public void setTeacher_Username(String teacherUsername) { Teacher_Username = teacherUsername; } public String getTeacher_Password() { return Teacher_Password; } public void setTeacher_Password(String teacherPassword) { Teacher_Password = teacherPassword; } public String getTeacher_Name() { return Teacher_Name; } public void setTeacher_Name(String teacherName) { Teacher_Name = teacherName; } public String getTeacher_Sex() { return Teacher_Sex; } public void setTeacher_Sex(String teacherSex) { Teacher_Sex = teacherSex; } public String getTeacher_Tel() { return Teacher_Tel; } public void setTeacher_Tel(String teacherTel) { Teacher_Tel = teacherTel; } }
package com.bean; public class StudentBean { private int Student_ID ; private int Student_DomitoryID ; private String Student_Username ; private String Student_Password ; private String Student_Name ; private String Student_Sex ; private String Student_Class ; private String Student_State ; public int getStudent_ID() { return Student_ID; } public void setStudent_ID(int studentID) { Student_ID = studentID; } public int getStudent_DomitoryID() { return Student_DomitoryID; } public void setStudent_DomitoryID(int studentDomitoryID) { Student_DomitoryID = studentDomitoryID; } public String getStudent_Username() { return Student_Username; } public void setStudent_Username(String studentUsername) { Student_Username = studentUsername; } public String getStudent_Password() { return Student_Password; } public void setStudent_Password(String studentPassword) { Student_Password = studentPassword; } public String getStudent_Name() { return Student_Name; } public void setStudent_Name(String studentName) { Student_Name = studentName; } public String getStudent_Sex() { return Student_Sex; } public void setStudent_Sex(String studentSex) { Student_Sex = studentSex; } public String getStudent_Class() { return Student_Class; } public void setStudent_Class(String studentClass) { Student_Class = studentClass; } public String getStudent_State() { return Student_State; } public void setStudent_State(String studentState) { Student_State = studentState; } private String Domitory_Name ; private String Building_Name ; public String getDomitory_Name() { return Domitory_Name; } public void setDomitory_Name(String domitoryName) { Domitory_Name = domitoryName; } public String getBuilding_Name() { return Building_Name; } public void setBuilding_Name(String buildingName) { Building_Name = buildingName; } private String Domitory_Type ; private String Domitory_Number ; private String Domitory_Tel ; public String getDomitory_Type() { return Domitory_Type; } public void setDomitory_Type(String domitoryType) { Domitory_Type = domitoryType; } public String getDomitory_Number() { return Domitory_Number; } public void setDomitory_Number(String domitoryNumber) { Domitory_Number = domitoryNumber; } public String getDomitory_Tel() { return Domitory_Tel; } public void setDomitory_Tel(String domitoryTel) { Domitory_Tel = domitoryTel; } }
创建mybatis映射关系
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <!-- namespace用于java代码调用时识别指定xml的mapper文件 --> <mapper namespace="com.mybatis.model.Student"> <!-- 配置ORM映射 --> <resultMap type="Student" id="student_orm"> <result property="Student_ID" column="STUDENT_ID"/> <result property="Student_Username" column="STUDENT_USERNAME"/> <result property="Student_Password" column="STUDENT_PASSWORD"/> <result property="Student_Name" column="STUDENT_NAME"/> <result property="Student_Sex" column="STUDENT_SEX"/> <result property="Student_Tel" column="STUDENT_TEL"/> </resultMap> <select id="selectByUsernameAndPassword" parameterType="map" resultType="Student"> SELECT * FROM STUDENT WHERE STUDENT_USERNAME= #{Student_Username} and STUDENT_PASSWORD=#{Student_Password} </select> </mapper>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <!-- namespace用于java代码调用时识别指定xml的mapper文件 --> <mapper namespace="com.mybatis.model.Teacher"> <!-- 配置ORM映射 --> <resultMap type="Teacher" id="teacher_orm"> <result property="Teacher_ID" column="TEACHER_ID"/> <result property="Teacher_Username" column="TEACHERUSERNAME"/> <result property="Teacher_Password" column="TEACHER_PASSWORD"/> <result property="Teacher_Name" column="TEACHER_NAME"/> <result property="Teacher_Sex" column="TEACHER_SEX"/> <result property="Teacher_Tel" column="TEACHER_TEL"/> </resultMap> <select id="selectByUsernameAndPassword" parameterType="map" resultType="Teacher"> SELECT * FROM TEACHER WHERE TEACHER_USERNAME= #{Teacher_Username} and TEACHER_PASSWORD=#{Teacher_Password} </select> </mapper>
修改configuration.xml,增加实体类别名以及注册学生、教师的映射文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!-- 注意:每个标签必须按顺序写,会提示错误:--> <configuration> <!-- 属性配置 --> <properties resource="jdbc.properties"/> <!-- 设置缓存和延迟加载等等重要的运行时的行为方式 --> <settings> <!-- 设置超时时间,它决定驱动等待一个数据库响应的时间 --> <setting name="defaultStatementTimeout" value="http://www.mamicode.com/25000"/> </settings> <!-- 别名 --> <typeAliases> <typeAlias alias="Admin" type="com.bean.AdminBean"/> <typeAlias alias="Student" type="com.bean.StudentBean"/> <typeAlias alias="Teacher" type="com.bean.TeacherBean"/> </typeAliases> <environments default="development"> <!-- environment 元素体中包含对事务管理和连接池的环境配置 --> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="http://www.mamicode.com/${driver}" /> <property name="url" value="http://www.mamicode.com/${url}" /> <property name="username" value="http://www.mamicode.com/${username}" /> <property name="password" value="http://www.mamicode.com/${password}" /> </dataSource> </environment> </environments> <!-- ORM映射文件 --> <mappers> <mapper resource="com/mybatis/model/AdminUserMapper.xml" /> <mapper resource="com/mybatis/model/StudentUserMapper.xml" /> <mapper resource="com/mybatis/model/TeacherUserMapper.xml" /> </mappers> </configuration>
增加验证学生、教师的dao类。
package com.dao; import com.bean.StudentBean; public interface StudentDao { /** * @date 2016-10-18 下午6:03:50 * @Title: CheckLogin * @Description: 根据用户名称以及密码验证是否存在该管理员账号 * @return String 返回类型 * @throws */ public String CheckLogin(StudentBean student); }
package com.dao; import com.bean.TeacherBean; public interface TeacherDao { /** * @date 2016-10-18 下午6:03:50 * @Title: CheckLogin * @Description: 根据用户名称以及密码验证是否存在该管理员账号 * @return String 返回类型 * @throws */ public String CheckLogin(TeacherBean teacher); }
package com.dao.impl; import java.util.HashMap; import java.util.Map; import org.apache.ibatis.session.SqlSession; import com.bean.StudentBean; import com.dao.StudentDao; import com.utils.SessionFactoryUtil; public class StudentDaoImpl implements StudentDao{ @Override public String CheckLogin(StudentBean student) { SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession(); Map<String,Object> params = new HashMap<String ,Object>(); params.put("Student_Username", student.getStudent_Username()); params.put("Student_Password", student.getStudent_Password()); StudentBean studentbean = (StudentBean) session.selectOne("com.mybatis.model.Student.selectByUsernameAndPassword", params); session.close(); if(studentbean!=null){ return studentbean.getStudent_ID()+""; } return null; } }
package com.dao.impl; import java.util.HashMap; import java.util.Map; import org.apache.ibatis.session.SqlSession; import com.bean.TeacherBean; import com.dao.TeacherDao; import com.utils.SessionFactoryUtil; public class TeacherDaoImpl implements TeacherDao{ @Override public String CheckLogin(TeacherBean teacher) { SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession(); Map<String,Object> params = new HashMap<String ,Object>(); params.put("Teacher_Username", teacher.getTeacher_Username()); params.put("Teacher_Password", teacher.getTeacher_Password()); TeacherBean teacherbean = (TeacherBean) session.selectOne("com.mybatis.model.Teacher.selectByUsernameAndPassword", params); session.close(); if(teacherbean!=null){ return teacherbean.getTeacher_ID()+""; } return null; } }
修改登录action--GoLogin.java,增加身份类型为学生、楼宇管理员的if条件判断。
package com.action; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.bean.AdminBean; import com.bean.StudentBean; import com.bean.TeacherBean; import com.dao.AdminDao; import com.dao.StudentDao; import com.dao.TeacherDao; import com.dao.impl.AdminDaoImpl; import com.dao.impl.StudentDaoImpl; import com.dao.impl.TeacherDaoImpl; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class GoLogin extends ActionSupport{ private String Type; private String Username; private String Password; private String Msg; public String getType() { return Type; } public void setType(String type) { Type = type; } public String getUsername() { return Username; } public void setUsername(String username) { Username = username; } public String getPassword() { return Password; } public void setPassword(String password) { Password = password; } public String getMsg() { return Msg; } public void setMsg(String msg) { Msg = msg; } public String execute() throws Exception { if(Type.equals("系统管理员")){ AdminBean admin=new AdminBean(); admin.setAdmin_Name(Username); admin.setAdmin_Password(Password); AdminDao dao=new AdminDaoImpl(); if (dao.CheckLogin(admin)==null) { Msg = "用户名或者密码错误"; return INPUT; }else{ //获取ID String Admin_ID=dao.CheckLogin(admin); //创建session HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("id", Admin_ID); session.setAttribute("type", "1"); return SUCCESS; } }else if(Type.equals("学生")){ StudentBean student=new StudentBean(); student.setStudent_Username(Username); student.setStudent_Password(Password); StudentDao dao=new StudentDaoImpl(); if (dao.CheckLogin(student)==null) { Msg = "用户名或者密码错误"; return INPUT; }else{ //获取ID String Teacher_ID=dao.CheckLogin(student); //创建session HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("id", Teacher_ID); session.setAttribute("type", "2"); return SUCCESS; } }else if(Type.equals("楼宇管理员")) { TeacherBean Teacher=new TeacherBean(); Teacher.setTeacher_Username(Username); Teacher.setTeacher_Password(Password); TeacherDao dao=new TeacherDaoImpl(); if (dao.CheckLogin(Teacher)==null) { Msg = "用户名或者密码错误"; return INPUT; }else{ //获取ID String Teacher_ID=dao.CheckLogin(Teacher); //创建session HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("id", Teacher_ID); session.setAttribute("type", "2"); return SUCCESS; } }else{ Msg = "身份类型错误"; return INPUT; } } }
数据库插入测试数据:
insert into Student (STUDENT_ID, STUDENT_USERNAME, STUDENT_PASSWORD, STUDENT_NAME, STUDENT_SEX, STUDENT_TEL) values (1, ‘wangwu‘, ‘123‘, ‘王五‘, ‘男‘, ‘159*********‘);
insert into Teacher (TEACHER_ID, TEACHER_USERNAME, TEACHER_PASSWORD, TEACHER_NAME, TEACHER_SEX, TEACHER_TEL) values (1, ‘lisi‘, ‘123‘, ‘李四‘, ‘女‘, ‘159**********‘);
现在我们根据不同身份输入不同账号,可有查看各个账号有不同的权限,这种权限控制在页面根据身份判断写死,在企业中是不可能出现的,非常不利于维护以及安全问题,在这里我只是解析网上这款项目的代码,业务以及逻辑方面暂不改动,适合新手来自己搭建一款项目。
本文出自 “进击的程序猿” 博客,请务必保留此出处http://zangyanan.blog.51cto.com/11610700/1863482
javaWeb校园宿舍管理解析(三)