📄 studentaction.java
字号:
package com.zjxy.hibernate.action;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;
import com.zjxy.hibernate.base.PageDAO;
import com.zjxy.hibernate.base.StudentManager;
import com.zjxy.hibernate.form.StudentForm;
import com.zjxy.hibernate.model.Student;
import com.zjxy.hibernate.model.User;
import com.zjxy.hibernate.pagerHelp.Pager;
import com.zjxy.hibernate.pagerHelp.PagerHelp;
public class StudentAction extends DispatchAction{
/**
* 学生信息一览画面显示
* @param actionMapping
* @param actionForm
* @param httpServletRequest
* @param httpServletresponse
* @return
*/
public ActionForward display(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletresponse) {
// 用于输出到页面的记录集合
List clInfos = null;
String sqlBean = "from com.zjxy.hibernate.model.Student";
// 记录总行数
int totalRows = 0;
StudentManager studentManager = new StudentManager();
List list = studentManager.getStudentList();
totalRows = list.size();
PageDAO pageDAO = new PageDAO();
// 取得当前表中的总行数
// 通过PagerHelper类来获取用于输出到页面的pager对象
Pager pager=PagerHelp.getPager(httpServletRequest,totalRows);
// 取出从startRow开始的pageSize行记录
try {
clInfos = pageDAO.findWithPage(pager.getPageSize(), pager.getStartRow(),sqlBean);
} catch (Exception ex) {
servlet.log(ex.toString());
}
Integer total = Integer.valueOf(totalRows);
//把输出的记录集和pager对象保存到request对象中
httpServletRequest.setAttribute("TOTALROWS", total);
httpServletRequest.setAttribute("CLINFOS", clInfos);
httpServletRequest.setAttribute("PAGER", pager);
return actionMapping.findForward("display");
}
/**
* 学生信息添加画面初期表示
* @param actionMapping
* @param actionForm
* @param httpServletRequest
* @param httpServletresponse
* @return
*/
public ActionForward addDisplay(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletresponse) {
return actionMapping.findForward("addDisplay");
}
/**
* 学生信息添加
* @param actionMapping
* @param actionForm
* @param request
* @param response
* @return
*/
public ActionForward add(ActionMapping actionMapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
// 定义StudentForm对象用于获取页面信息
StudentForm studentForm = (StudentForm) form;
Student student = new Student();
User user = new User();
StudentManager studentManager = new StudentManager();
// 定义existFlag变量,用于判断学生帐号存在性判断
boolean existFlag = false;
// 定义ActionErrors对象
ActionErrors errors = new ActionErrors();
// 将页面取得的信息设置值到Student JavaBean中
// 学生帐号
student.setStudentID(studentForm.getStudentID());
// 学生姓名
student.setStudentName(studentForm.getStudentName());
// 学生性别
student.setStudentSex(studentForm.getStudentSex());
// 学生联系电话
student.setStudentCall(studentForm.getStudentCall());
// 学生联系地址
student.setStudentAddress(studentForm.getStudentAddress());
// 学生所在班级
student.setStudentClass(studentForm.getStudentClass());
// 学生初始密码
student.setStudentNO(studentForm.getStudentNO());
// 将页面取得的信息设置值到User JavaBean中
// 学生帐号
user.setUserID(studentForm.getStudentID());
// 学生密码
user.setPassword(studentForm.getStudentNO());
// 学生权限
user.setUserFlag("S");
// 判断输入的学生帐号是否已经存在,存在则返回true,否则返回false
existFlag = studentManager.getStudentByID(studentForm.getStudentID());
if (existFlag) {
// 学生帐号已经存在的情况
errors.add("studentIDIsExist", new ActionError("oes.student.error.studentIDIsExist"));
saveErrors(request, errors);
return (new ActionForward(actionMapping.getInput()));
}
// 调用AdminManager类的addAdmin方法向数据库添加教师信息
studentManager.addStudent(student, user);
//重定向:页面跳转
return actionMapping.findForward("addSuccess");
}
/**
* 学生信息修改画面初期表示
* @param actionMapping
* @param actionForm
* @param request
* @param response
* @return
*/
public ActionForward modifyDisplay(ActionMapping actionMapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
// 定义StudentForm对象用于获取页面信息
StudentForm studentForm = (StudentForm) form;
Student student = new Student();
StudentManager studentManager = new StudentManager();
// 从页面取得要修改数据的sequenceID值
int sequenceID = Integer.parseInt(request.getParameter("sequenceID"));
// 调用getModifyInfoByID方法取得要修改的数据
student = studentManager.getModifyInfoByID(sequenceID);
// 将页面取得的信息设置值到Student JavaBean中
try {
BeanUtils.copyProperties(studentForm, student);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 重定向:页面跳转
return actionMapping.findForward("modifyDisplay");
}
/**
* 学生信息修改
* @param actionMapping
* @param actionForm
* @param request
* @param response
* @return
*/
public ActionForward modify(ActionMapping actionMapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
// 定义StudentForm对象用于获取页面信息
StudentForm studentForm = (StudentForm) form;
Student student = new Student();
Student studentOld = new Student();
User user = new User();
StudentManager studentManager = new StudentManager();
ActionErrors errors = new ActionErrors();
// 将页面取得的信息设置值到Admin JavaBean中
student.setSequenceID(studentForm.getSequenceID());
student.setStudentID(studentForm.getStudentID());
student.setStudentName(studentForm.getStudentName());
student.setStudentSex(studentForm.getStudentSex());
student.setStudentCall(studentForm.getStudentCall());
student.setStudentAddress(studentForm.getStudentAddress());
student.setStudentClass(studentForm.getStudentClass());
student.setStudentNO(studentForm.getStudentNO());
// 将页面取得的信息设置值到User JavaBean中
user.setSequenceID(studentForm.getSequenceID());
user.setUserID(studentForm.getStudentID());
user.setPassword(studentForm.getStudentNO());
user.setUserFlag("S");
// 判断修改过的学生帐号是否在数据库中已经存在,若存在则报错
studentOld = studentManager.getModifyInfoByID(studentForm.getSequenceID());
if (!studentForm.getStudentID().equals(studentOld.getStudentID())) {
// 学生帐号已经被修改
if (studentManager.getStudentByID(studentForm.getStudentID())) {
// 学生帐号已经存在的情况
errors.add("studentIDIsExist", new ActionError("oes.student.error.studentIDIsExist"));
saveErrors(request, errors);
return (new ActionForward(actionMapping.getInput()));
}
}
// 修改学生信息:调用modifyAdminInfo方法
studentManager.modifyStudentInfo(student, user);
return actionMapping.findForward("modifySuccess");
}
/**
* 学生信息删除
* @param actionMapping
* @param actionForm
* @param request
* @param response
* @return
*/
public ActionForward delete(ActionMapping actionMapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
StudentManager studentManager = new StudentManager();
// 从页面取得要删除数据的sequenceID值
int sequenceID = Integer.parseInt(request.getParameter("sequenceID"));
studentManager.delete(sequenceID);
return actionMapping.findForward("deleteSuccess");
}
/**
* 学生信息查询
* @param actionMapping
* @param actionForm
* @param request
* @param response
* @return
*/
public ActionForward search(ActionMapping actionMapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
StudentForm studentForm = (StudentForm) form;
StudentManager studentManager = new StudentManager();
String name = studentForm.getStudentIDSearch();
// 用于输出到页面的记录集合
List clInfos = null;
String sqlBean = "from com.zjxy.hibernate.model.Student as student where student.studentID=?";
// 记录总行数
int totalRows = 0;
List list = studentManager.getStudentList();
totalRows = list.size();
PageDAO pageDAO = new PageDAO();
// 取得当前表中的总行数
// 通过PagerHelper类来获取用于输出到页面的pager对象
Pager pager=PagerHelp.getPager(request,totalRows);
// 取出从startRow开始的pageSize行记录
try {
clInfos = pageDAO.findWithPage_search(pager.getPageSize(), pager.getStartRow(), sqlBean, name);
} catch (Exception ex) {
servlet.log(ex.toString());
}
if (clInfos.size() == 0) {
request.setAttribute("noRecord", new ActionMessage("oes.student.error.noRecord"));
}
Integer total = Integer.valueOf(totalRows);
//把输出的记录集和pager对象保存到request对象中
request.setAttribute("TOTALROWS", total);
request.setAttribute("CLINFOS", clInfos);
request.setAttribute("PAGER", pager);
request.setAttribute("searchPage", new ActionMessage("searchPage"));
return actionMapping.findForward("searchSuccess");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -