📄 studentaction.java
字号:
package cn.hxex.exam.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import cn.hxex.exam.dao.ClassesDAO;
import cn.hxex.exam.dao.DAOFactory;
import cn.hxex.exam.dao.StudentDAO;
import cn.hxex.exam.dao.UserDAO;
import cn.hxex.exam.form.StudentForm;
import cn.hxex.exam.model.Classes;
import cn.hxex.exam.model.Student;
import cn.hxex.exam.model.User;
import cn.hxex.exam.struts.BaseAction;
import cn.hxex.exam.util.HxexBeanUtils;
/**
* The action about student
*
* @struts.action
* name="studentForm"
* path="/manage/student"
* scope="request"
* input="/manage/student_add.jsp"
* validate="false"
* parameter="p"
*
* @struts.action-forward
* name="add"
* path="/manage/student_add.jsp"
*
* @struts.action-forward
* name="update"
* path="/manage/student_update.jsp"
*
* @struts.action-forward
* name="list"
* path="/manage/student_list.jsp"
*
* @author galaxy
*
*/
public class StudentAction extends BaseAction
{
/**
* 获得班级学生的方法
*/
public ActionForward list( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
// 得到班级的ID值
String classesid = request.getParameter( "classesid" );
// 得到班级DAO对象
ClassesDAO dao = DAOFactory.getDao( ClassesDAO.class );
// 得到班级信息
Classes c = dao.findById( classesid, false );
// 将班级信息设置为请求的属性
request.setAttribute( "classes", c );
// 跳转到学生信息列表页面
return mapping.findForward( "list" );
}
/**
* 进入录入学生信息页面
*/
public ActionForward addin( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
// 得到班级的ID值
String classesid = request.getParameter( "classesid" );
// 得到班级信息
ClassesDAO dao = DAOFactory.getDao( ClassesDAO.class );
Classes c = dao.findById( classesid, false );
request.setAttribute( "classes", c );
// 跳转到录入学生信息页面
return mapping.findForward( "add" );
}
/**
* 保存学生信息
*/
public ActionForward save( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
// 得到学生对象
Student s = HxexBeanUtils.copyProperties( new Student(), form );
// 得到UserDAO对象的实例
UserDAO dao = DAOFactory.getDao( UserDAO.class );
// 判断用户名是否存在
if( dao.getUserByName( s.getName() )==null )
{
// 得到班级的ID
String classesid = request.getParameter( "classesid" );
// 得到班级对象
ClassesDAO classesdao = DAOFactory.getDao( ClassesDAO.class );
Classes c = classesdao.findById( classesid, false );
// 保存学生对象
s.setClasses( c );
dao.makePersistent( s );
// 设置提示信息
addMessage( request, "student.msg.add.success" );
// 清空Form中的值
StudentForm student = (StudentForm)form;
student.setName( null );
student.setPassword( null );
student.setFullname( null );
}
else
{
addMessage( request, "student.msg.add.nameexist" );
}
// 返回到学生信息录入页面
return addin( mapping, form, request, response );
}
/**
* 进入学生信息修改页面的Action方法
*/
public ActionForward updatein( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
// 得到用户传递的参数
StudentForm student = (StudentForm)form;
// 得到StudentDAO对象的实例
StudentDAO dao = DAOFactory.getDao( StudentDAO.class );
// 得到学生对象
Student s = dao.findById( student.getId(), false );
request.setAttribute( "studentForm", s );
request.setAttribute( "classes", s.getClasses() );
// 跳转到学生信息修改页面
return mapping.findForward( "update" );
}
/**
* 保存学生信息
*/
public ActionForward update( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
// 得到学生对象
Student s = HxexBeanUtils.copyProperties( new Student(), form );
// 得到UserDAO对象的实例
UserDAO dao = DAOFactory.getDao( UserDAO.class );
User u = dao.getUserByName( s.getName() );
// 判断用户名是否存在
if( u == null || u.getId().equals( s.getId() ) )
{
if( u==null )
u = dao.findById( s.getId(), false );
if( u != null )
{
Student stu = (Student)u;
stu.setName( s.getName() );
stu.setPassword( s.getPassword() );
stu.setFullname( s.getFullname() );
// 设置提示信息
addMessage(request, "student.msg.update.success");
}
else
{
// 设置提示信息
addMessage(request, "student.msg.update.notexist");
}
}
else
{
// 设置用户名重复提示信息
addMessage( request, "student.msg.add.nameexist" );
if( s.getId()!=null && s.getId().length()>0 )
{
// 返回学生信息修改页面
return updatein( mapping, form, request, response );
}
}
// 返回到学生信息列表页面
return list( mapping, form, request, response );
}
/**
* 删除学生信息功能
*/
public ActionForward delete( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
// 得到用户传递的参数
StudentForm student = (StudentForm)form;
// 得到要删除的学生对象
StudentDAO dao = DAOFactory.getDao( StudentDAO.class );
Student s = dao.findById( student.getId(), false );
if( s!=null )
{
// 删除学生对象
Classes c = s.getClasses();
c.getStudents().remove( s );
dao.makeTransient( s );
// 设置提示信息
addMessage( request, "student.msg.delete.success" );
request.setAttribute( "classes", c );
}
else
{
addMessage( request, "student.msg.update.notexist" );
}
return mapping.findForward( "list" );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -