首页 > 代码库 > javaWeb校园宿舍管理解析(四)

javaWeb校园宿舍管理解析(四)

    关于3种身份的登录验证已经完成,现在我们开始逐步完成菜单中的选项。

    首先看管理员进入系统的菜单选项,第一个是楼宇管理员,看源码关于该功能,只是简单的增删改查,工作中其实往往大多数也是针对实体类的增删改查,无非是持久层技术不同或者企业有大牛将这些方法统一,方便拿来直接用,先看效果页面。

    技术分享上面一个页面关于楼宇管理员管理包含四个功能:查询(条件查询和全查询)、删除、修改、新增。

首先我们来理清楚思路,管理员身份登录,进入index.jsp页面,我们会发现菜单相应操作都在left.jsp中,首先定位到--楼宇管理者管理

<a href="http://www.mamicode.com/TeacherManager.action">楼宇管理员管理</a>

从这里我们就知道关于请求,我们首先得配置struts2的action,下面我们在struts.xml中添加

<!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 -->
		<action name="GoLogin" class="com.action.GoLogin">
			<result name="input">/Login.jsp</result>
			<result name="success">/Index.jsp</result>
		</action>
		<!-- 楼宇管理者管理action -->
		<action name="TeacherManager" class="com.action.TeacherManager">
			<result name="success">/TeacherManager.jsp</result>
		</action>		
	</package>
</struts>

增加action类--TeacherManager以及TeacherDao增加查询方法

package com.dao;
import java.util.List;

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);
	/**
	 * 
	  * @date 2016-10-19 下午4:35:52
	  * @Title: getTeacherList
	  * @Description: 获取teahcer
	  * @return List<TeacherBean>    返回类型
	  * @throws
	 */
	public List<TeacherBean> getTeacherList(String SearchRow,String Searchkey);
}

TeacherDao的实现类TeacherDaoImpl

package com.dao.impl;

import java.util.HashMap;
import java.util.List;
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;
	}

	@Override
	public List<TeacherBean> getTeacherList(String SearchRow,String Searchkey) {
		
		SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
		Map<String,Object> params = new HashMap<String ,Object>();
		params.put("filed", SearchRow);
		params.put("value", Searchkey);
		
		List<TeacherBean> teacherbean=session.selectList("com.mybatis.model.Teacher.selectTeacherList",params);
		return teacherbean;
	}

}

当然在少不了在mabatis配置查询语句,这里我用了mybatis的if以及动态列添加,用if来判断是否是条件查询,同时动态传参当列名条件,因此注意map中field的传递需跟表字段保持一致(不区分大小写)

<?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>
  	
  	<select id="selectTeacherList" parameterType="map" resultType="Teacher">
    	SELECT * FROM TEACHER where 1=1
    	<if test="value!=‘‘ and value!=null"> and ${filed}=#{value}</if>
  	</select>
  	
</mapper>


package com.action;

import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.bean.*;
import com.dao.*;
import com.dao.impl.TeacherDaoImpl;

@SuppressWarnings("serial")
public class TeacherManager extends ActionSupport {

	//下面是Action内用于封装用户请求参数的属性
	private List<TeacherBean> teacherlist;
	
	public List<TeacherBean> getTeacherlist() {
		return teacherlist;
	}
	public void setTeacherlist(List<TeacherBean> teacherlist) {
		this.teacherlist = teacherlist;
	}
	private String SearchRow;
	private String SearchKey;
	public String getSearchRow() {
		return SearchRow;
	}
	public void setSearchRow(String searchRow) {
		SearchRow = searchRow;
	}
	public String getSearchKey() {
		return SearchKey;
	}
	public void setSearchKey(String searchKey) {
		SearchKey = searchKey;
	}
	//处理用户请求的execute方法
	public String execute() throws Exception {
		
		//解决乱码,用于页面输出
		HttpServletResponse response=null;
		response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		//创建session对象
		HttpSession session = ServletActionContext.getRequest().getSession();
		//验证是否正常登录
		if(session.getAttribute("id")==null){
			out.print("<script language=‘javascript‘>alert(‘请重新登录!‘);window.location=‘Login.jsp‘;</script>");
			out.flush();out.close();return null;
		}
		TeacherDao dao=new TeacherDaoImpl();
		teacherlist=dao.getTeacherList(SearchRow,SearchKey);
		return SUCCESS;
		
	}

}

增加页面TeacherManager.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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">&nbsp;</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="top" bgcolor="#F6F9FE"><table width="709" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td height="30" background="Images/mainMenuBg.jpg" style="padding-left:25px;">楼宇管理员管理</td>
            </tr>
            <tr>
              <td height="470" align="center" valign="top" bgcolor="#F6F9FE"><form name="form1" method="post" action="TeacherManager.action">
                <table width="100%%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="28%" height="30" style="padding-left:20px;"> 功能导航: <a href="http://www.mamicode.com/TeacherAdd.jsp">添加楼宇管理员</a></td>
                    <td width="72%">查询:
                      <select name="SearchRow" id="SearchRow">
                        <option value="http://www.mamicode.com/Teacher_Name" <s:if test=‘"Teacher_Name"==SearchRow‘>selected</s:if>>姓名</option>
                        <option value="http://www.mamicode.com/Teacher_Tel" <s:if test=‘"Teacher_Tel"==SearchRow‘>selected</s:if>>电话</option>
                        <option value="http://www.mamicode.com/Teacher_Username" <s:if test=‘"Teacher_Username"==SearchRow‘>selected</s:if>>用户名</option>
                      </select>
                      <input name="SearchKey" type="text" class="text1" id="SearchKey" value="http://www.mamicode.com/" >
                      <input type="submit" name="button" id="button" value="http://www.mamicode.com/点击查询"></td>
                  </tr>
                </table>
              </form>
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr align="center"  class="t1">
                    <td height="25" bgcolor="#D5E4F4"><strong>姓名</strong></td>
                    <td bgcolor="#D5E4F4"><strong>性别</strong></td>
                    <td bgcolor="#D5E4F4"><strong>电话</strong></td>
                    <td bgcolor="#D5E4F4"><strong>用户名</strong></td>
                    <td bgcolor="#D5E4F4"><strong>操作</strong></td>
                  </tr>
                  <s:iterator value="http://www.mamicode.com/teacherlist">
                    <tr align="center">
                      <td height="25" align="center"><s:property value="http://www.mamicode.com/Teacher_Name"/></td>
                      <td> <s:property value="http://www.mamicode.com/Teacher_Sex"/></td>
                      <td><s:property value="http://www.mamicode.com/Teacher_Tel"/></td>
                      <td align="center"><s:property value="http://www.mamicode.com/Teacher_Username"/></td>
                      <td align="center"><a href="http://www.mamicode.com/TeacherUpdate.action?Teacher_ID=">修改</a> <a href="http://www.mamicode.com/TeacherDel.action?Teacher_ID=${Teacher_ID}" onClick="return confirm(‘确定要删除该楼宇管理员吗?‘)">删除</a></td>
                    </tr>
                  </s:iterator>
                </table></td>
            </tr>
          </table></td>
        </tr>
      </table></td>
    </tr>
    <tr>
      <td height="35" background="Images/bootBg.jpg">&nbsp;</td>
    </tr>
  </table>

</center>
</body>
</html>

以上关于楼宇管理员查询包含条件查询已经完成,剩下的思路都是一样的,都是先增加action,然后增加dao方法,action需要增加页面的就增加页面,下面我不详细说了,只贴代码。

涉及新增,因此需要创建序列:

CREATE SEQUENCE seq_teacher
INCREMENT BY 1    
START WITH 1  
NOMAXVALUE  
NOCYCLE
CACHE 10;

增加增删改的action配置

<!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 -->
		<action name="GoLogin" class="com.action.GoLogin">
			<result name="input">/Login.jsp</result>
			<result name="success">/Index.jsp</result>
		</action>
		<!-- 楼宇管理者管理action -->
		<action name="TeacherManager" class="com.action.TeacherManager">
			<result name="success">/TeacherManager.jsp</result>
		</action>	
		
		<action name="TeacherAddSave" class="com.action.TeacherAddSave">
		  <result name="success"  type="redirect">TeacherManager.action</result>
		</action>
		
		<action name="TeacherDel" class="com.action.TeacherDel">
		  <result name="success" type="redirect">TeacherManager.action</result>
		</action>	
		<action name="TeacherUpdate" class="com.action.TeacherUpdate">
	        <result name="success">/TeacherUpdate.jsp</result>
		</action>		
		<action name="TeacherUpdateSave" class="com.action.TeacherUpdateSave">
		  <result name="success" type="redirect">TeacherManager.action</result>
		</action>	
		
	</package>
</struts>

增加dao层的代码

package com.dao;
import java.util.List;

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);
	/**
	 * 
	  * @date 2016-10-19 下午4:35:52
	  * @Title: getTeacherList
	  * @Description: 获取teahcer
	  * @return List<TeacherBean>    返回类型
	  * @throws
	 */
	public List<TeacherBean> getTeacherList(String SearchRow,String Searchkey,String TeacherId);
	/**
	 * 
	  * @author zangyn
	  * @date 2016-10-20 下午3:53:34
	  * @Title: addTeacher
	  * @Description: TODO
	  * @return void    返回类型
	  * @throws
	 */
	
	public void addTeacher(TeacherBean teacher);
	/**
	 * 
	  * @author zangyn
	  * @date 2016-10-21 上午9:04:46
	  * @Title: delTeacher
	  * @Description: 根据楼宇管理员ID删除
	  * @return void    返回类型
	  * @throws
	 */
	
	public void delTeacher(String Teahcer_id);
	/**
	 * 
	  * @author zangyn
	  * @date 2016-10-21 上午10:00:48
	  * @Title: getTeacherBean
	  * @Description: 通过楼宇管理者ID获取对象
	  * @return TeacherBean    返回类型
	  * @throws
	 */
	public TeacherBean getTeacherBean(int Teacher_id);
	/**
	 * 
	  * @author zangyn
	  * @date 2016-10-21 上午10:31:55
	  * @Title: updateTeacher
	  * @Description: 修改楼宇管理员信息
	  * @return void    返回类型
	  * @throws
	 */
	public void updateTeacher(TeacherBean teacher);
}
package com.dao.impl;

import java.util.HashMap;
import java.util.List;
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;
	}

	@Override
	public List<TeacherBean> getTeacherList(String SearchRow,String Searchkey,String TeacheId) {
		
		SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
		Map<String,Object> params = new HashMap<String ,Object>();
		params.put("filed", SearchRow);
		params.put("value", Searchkey);
		params.put("id", TeacheId);
		List<TeacherBean> teacherbean=session.selectList("com.mybatis.model.Teacher.selectTeacherList",params);
		session.close();
		return teacherbean;
	}

	@Override
	public void addTeacher(TeacherBean teacher) {
		SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
		session.insert("com.mybatis.model.Teacher.insertTeacher",teacher);
		session.commit();
		session.close();
	}

	@Override
	public void delTeacher(String Teahcer_id) {
		SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
		session.delete("com.mybatis.model.Teacher.delTeacher", Teahcer_id);
		session.commit();
		session.close();
	}

	@Override
	public TeacherBean getTeacherBean(int Teacher_id) {
		SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
		TeacherBean bean=session.selectOne("com.mybatis.model.Teacher.selectTeacherById", Teacher_id);
		session.close();
		return bean;
	}

	@Override
	public void updateTeacher(TeacherBean teacher) {
		SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
		session.update("com.mybatis.model.Teacher.updateTeacher",teacher);
		session.commit();
		session.close();
	}

}

关于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.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>
  	
  	<select id="selectTeacherList" parameterType="map" resultType="Teacher" >
    	SELECT * FROM TEACHER where 1=1
    	<if test="value!=‘‘ and value!=null"> and ${filed}=#{value}</if>
    	<if test="id!=null"> and TEACHER_ID!=#{id}</if>
  	</select>
  	
  	
  	<select id="selectTeacherById" parameterType="int" resultType="Teacher" >
    	SELECT * FROM TEACHER where TEACHER_ID=#{id}
  	</select>
  	
  	
  	<insert id="insertTeacher" parameterType="Teacher">
		<selectKey resultType="INTEGER" order="BEFORE" keyProperty="Teacher_ID">  
       SELECT SEQ_TEACHER.NEXTVAL  FROM DUAL
       </selectKey> 
		INSERT INTO TEACHER(TEACHER_ID,TEACHER_USERNAME,TEACHER_PASSWORD,TEACHER_NAME,TEACHER_SEX,TEACHER_TEL) VALUES(#{Teacher_ID},#{Teacher_Username},#{Teacher_Password},#{Teacher_Name},#{Teacher_Sex},#{Teacher_Tel})
	</insert>
	
	<update id="delTeacher" parameterType="int">
	DELETE FROM TEACHER T WHERE T.TEACHER_ID= #{TEACHER_ID}
	</update>
	
	<update id="updateTeacher" parameterType="Teacher">
	UPDATE TEACHER SET TEACHER_USERNAME= #{Teacher_Username},Teacher_Password= #{Teacher_Password},Teacher_Name= #{Teacher_Name},Teacher_Sex= #{Teacher_Sex},Teacher_Tel= #{Teacher_Tel} WHERE TEACHER_ID=#{Teacher_ID}
	</update>
  	
</mapper>

关于Struts2的核心action类,增删改。

package com.action;

import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.bean.*;
import com.dao.*;
import com.dao.impl.TeacherDaoImpl;


@SuppressWarnings("serial")
public class TeacherAddSave extends ActionSupport {

	//下面是Action内用于封装用户请求参数的属性
	private String Teacher_Username ;
    private String Teacher_Password ;
    private String Teacher_Name ;
    private String Teacher_Sex ;
    private String Teacher_Tel ;
	public String getTeacher_Username() {
		return Teacher_Username;
	}

	public void setTeacher_Username(String cookUsername) {
		Teacher_Username = cookUsername;
	}

	public String getTeacher_Password() {
		return Teacher_Password;
	}

	public void setTeacher_Password(String cookPassword) {
		Teacher_Password = cookPassword;
	}

	public String getTeacher_Name() {
		return Teacher_Name;
	}

	public void setTeacher_Name(String cookName) {
		Teacher_Name = cookName;
	}

	public String getTeacher_Sex() {
		return Teacher_Sex;
	}

	public void setTeacher_Sex(String cookSex) {
		Teacher_Sex = cookSex;
	}

	public String getTeacher_Tel() {
		return Teacher_Tel;
	}

	public void setTeacher_Tel(String cookTel) {
		Teacher_Tel = cookTel;
	}

	//处理用户请求的execute方法
	public String execute() throws Exception {
		
		//解决乱码,用于页面输出
		HttpServletResponse response=null;
		response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		//创建session对象
		HttpSession session = ServletActionContext.getRequest().getSession();
		//验证是否正常登录
		if(session.getAttribute("id")==null){
			out.print("<script language=‘javascript‘>alert(‘请重新登录!‘);window.location=‘Login.jsp‘;</script>");
			out.flush();out.close();return null;
		}
		
		//查询用户名是否存在
		TeacherDao dao=new TeacherDaoImpl();
		List<TeacherBean> list=dao.getTeacherList("Teacher_Username",Teacher_Username,null);
		if(list.size()>0)
		{
			out.print("<script language=‘javascript‘>alert(‘用户名已经存在!‘);history.back(-1);</script>");
			out.flush();out.close();return null;
		}
		//添加
		TeacherBean cnbean=new TeacherBean();
		cnbean.setTeacher_Username(Teacher_Username);
		cnbean.setTeacher_Password(Teacher_Password);
		cnbean.setTeacher_Name(Teacher_Name);
		cnbean.setTeacher_Sex(Teacher_Sex);
		cnbean.setTeacher_Tel(Teacher_Tel);
		
		dao.addTeacher(cnbean);
		return SUCCESS;
	}

}
package com.action;

import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.bean.*;
import com.dao.*;
import com.dao.impl.TeacherDaoImpl;


public class TeacherDel extends ActionSupport {

	//下面是Action内用于封装用户请求参数的属性
	private String Teacher_ID ;
	public String getTeacher_ID() {
		return Teacher_ID;
	}

	public void setTeacher_ID(String userID) {
		Teacher_ID = userID;
	}

	//处理用户请求的execute方法
	public String execute() throws Exception {
		
		//解决乱码,用于页面输出
		HttpServletResponse response=null;
		response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		//创建session对象
		HttpSession session = ServletActionContext.getRequest().getSession();
		//验证是否正常登录
		if(session.getAttribute("id")==null){
			out.print("<script language=‘javascript‘>alert(‘请重新登录!‘);window.location=‘Login.jsp‘;</script>");
			out.flush();out.close();return null;
		}
		TeacherDao dao=new TeacherDaoImpl();
		dao.delTeacher(Teacher_ID);
		    
		return SUCCESS;
		
	}
	
}
package com.action;

import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.bean.*;
import com.dao.*;
import com.dao.impl.TeacherDaoImpl;


public class TeacherUpdate extends ActionSupport {

	//下面是Action内用于封装用户请求参数的属性
	private String Teacher_ID;
	private TeacherBean cnbean;
	public String getTeacher_ID() {
		return Teacher_ID;
	}

	public void setTeacher_ID(String studentID) {
		Teacher_ID = studentID;
	}

	public TeacherBean getCnbean() {
		return cnbean;
	}

	public void setCnbean(TeacherBean cnbean) {
		this.cnbean = cnbean;
	}

	//处理用户请求的execute方法
	public String execute() throws Exception {
		
		//解决乱码,用于页面输出
		HttpServletResponse response=null;
		response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		//创建session对象
		HttpSession session = ServletActionContext.getRequest().getSession();
		//验证是否正常登录
		if(session.getAttribute("id")==null){
			out.print("<script language=‘javascript‘>alert(‘请重新登录!‘);window.location=‘Login.jsp‘;</script>");
			out.flush();out.close();return null;
		}
		
		TeacherDao dao=new TeacherDaoImpl();

		cnbean=dao.getTeacherBean(Integer.parseInt(Teacher_ID));
		return SUCCESS;
		
	}
	
}
package com.action;

import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.bean.*;
import com.dao.*;
import com.dao.impl.TeacherDaoImpl;


public class TeacherUpdateSave extends ActionSupport {

	//下面是Action内用于封装用户请求参数的属性
	private String Teacher_ID ;
    private String Teacher_Username ;
    private String Teacher_Password ;
    private String Teacher_Name ;
    private String Teacher_Sex ;
    private String Teacher_Tel ;
	public String getTeacher_ID() {
		return Teacher_ID;
	}

	public void setTeacher_ID(String cookID) {
		Teacher_ID = cookID;
	}

	public String getTeacher_Username() {
		return Teacher_Username;
	}

	public void setTeacher_Username(String cookUsername) {
		Teacher_Username = cookUsername;
	}

	public String getTeacher_Password() {
		return Teacher_Password;
	}

	public void setTeacher_Password(String cookPassword) {
		Teacher_Password = cookPassword;
	}

	public String getTeacher_Name() {
		return Teacher_Name;
	}

	public void setTeacher_Name(String cookName) {
		Teacher_Name = cookName;
	}

	public String getTeacher_Sex() {
		return Teacher_Sex;
	}

	public void setTeacher_Sex(String cookSex) {
		Teacher_Sex = cookSex;
	}

	public String getTeacher_Tel() {
		return Teacher_Tel;
	}

	public void setTeacher_Tel(String cookTel) {
		Teacher_Tel = cookTel;
	}

	//处理用户请求的execute方法
	public String execute() throws Exception {
		
		//解决乱码,用于页面输出
		HttpServletResponse response=null;
		response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		//创建session对象
		HttpSession session = ServletActionContext.getRequest().getSession();
		//验证是否正常登录
		if(session.getAttribute("id")==null){
			out.print("<script language=‘javascript‘>alert(‘请重新登录!‘);window.location=‘Login.jsp‘;</script>");
			out.flush();out.close();return null;
		}
		TeacherDao dao=new TeacherDaoImpl();
		//查询用户名是否存在
		List<TeacherBean> list=dao.getTeacherList("Teacher_Username",Teacher_Username,Teacher_ID);
		if(list.size()>0)
		{
			out.print("<script language=‘javascript‘>alert(‘用户名已经存在!‘);history.back(-1);</script>");
			out.flush();out.close();return null;
		}
		//修改
	
		TeacherBean cnbean=new TeacherBean();
		cnbean=dao.getTeacherBean(Integer.parseInt(Teacher_ID));
		cnbean.setTeacher_Username(Teacher_Username);
		cnbean.setTeacher_Name(Teacher_Name);
		cnbean.setTeacher_Sex(Teacher_Sex);
		cnbean.setTeacher_Tel(Teacher_Tel);
		if(Teacher_Password!=null)
		{
			cnbean.setTeacher_Password(Teacher_Password);
		}
		dao.updateTeacher(cnbean);
		return SUCCESS;
		
	}
	
}

以上是相关功能的所有代码,其他功能大致做法都是相同的,这里我就不叙述了,我会将改造过后的源代码上传到51CTO上,下载地址:

本文出自 “进击的程序猿” 博客,请务必保留此出处http://zangyanan.blog.51cto.com/11610700/1864150

javaWeb校园宿舍管理解析(四)