?? studentaction.java
字號(hào):
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
{
/**
* 獲得班級(jí)學(xué)生的方法
*/
public ActionForward list( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
// 得到班級(jí)的ID值
String classesid = request.getParameter( "classesid" );
// 得到班級(jí)DAO對(duì)象
ClassesDAO dao = DAOFactory.getDao( ClassesDAO.class );
// 得到班級(jí)信息
Classes c = dao.findById( classesid, false );
// 將班級(jí)信息設(shè)置為請(qǐng)求的屬性
request.setAttribute( "classes", c );
// 跳轉(zhuǎn)到學(xué)生信息列表頁面
return mapping.findForward( "list" );
}
/**
* 進(jìn)入錄入學(xué)生信息頁面
*/
public ActionForward addin( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
// 得到班級(jí)的ID值
String classesid = request.getParameter( "classesid" );
// 得到班級(jí)信息
ClassesDAO dao = DAOFactory.getDao( ClassesDAO.class );
Classes c = dao.findById( classesid, false );
request.setAttribute( "classes", c );
// 跳轉(zhuǎn)到錄入學(xué)生信息頁面
return mapping.findForward( "add" );
}
/**
* 保存學(xué)生信息
*/
public ActionForward save( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
// 得到學(xué)生對(duì)象
Student s = HxexBeanUtils.copyProperties( new Student(), form );
// 得到UserDAO對(duì)象的實(shí)例
UserDAO dao = DAOFactory.getDao( UserDAO.class );
// 判斷用戶名是否存在
if( dao.getUserByName( s.getName() )==null )
{
// 得到班級(jí)的ID
String classesid = request.getParameter( "classesid" );
// 得到班級(jí)對(duì)象
ClassesDAO classesdao = DAOFactory.getDao( ClassesDAO.class );
Classes c = classesdao.findById( classesid, false );
// 保存學(xué)生對(duì)象
s.setClasses( c );
dao.makePersistent( s );
// 設(shè)置提示信息
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" );
}
// 返回到學(xué)生信息錄入頁面
return addin( mapping, form, request, response );
}
/**
* 進(jìn)入學(xué)生信息修改頁面的Action方法
*/
public ActionForward updatein( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
// 得到用戶傳遞的參數(shù)
StudentForm student = (StudentForm)form;
// 得到StudentDAO對(duì)象的實(shí)例
StudentDAO dao = DAOFactory.getDao( StudentDAO.class );
// 得到學(xué)生對(duì)象
Student s = dao.findById( student.getId(), false );
request.setAttribute( "studentForm", s );
request.setAttribute( "classes", s.getClasses() );
// 跳轉(zhuǎn)到學(xué)生信息修改頁面
return mapping.findForward( "update" );
}
/**
* 保存學(xué)生信息
*/
public ActionForward update( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
// 得到學(xué)生對(duì)象
Student s = HxexBeanUtils.copyProperties( new Student(), form );
// 得到UserDAO對(duì)象的實(shí)例
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() );
// 設(shè)置提示信息
addMessage(request, "student.msg.update.success");
}
else
{
// 設(shè)置提示信息
addMessage(request, "student.msg.update.notexist");
}
}
else
{
// 設(shè)置用戶名重復(fù)提示信息
addMessage( request, "student.msg.add.nameexist" );
if( s.getId()!=null && s.getId().length()>0 )
{
// 返回學(xué)生信息修改頁面
return updatein( mapping, form, request, response );
}
}
// 返回到學(xué)生信息列表頁面
return list( mapping, form, request, response );
}
/**
* 刪除學(xué)生信息功能
*/
public ActionForward delete( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
// 得到用戶傳遞的參數(shù)
StudentForm student = (StudentForm)form;
// 得到要?jiǎng)h除的學(xué)生對(duì)象
StudentDAO dao = DAOFactory.getDao( StudentDAO.class );
Student s = dao.findById( student.getId(), false );
if( s!=null )
{
// 刪除學(xué)生對(duì)象
Classes c = s.getClasses();
c.getStudents().remove( s );
dao.makeTransient( s );
// 設(shè)置提示信息
addMessage( request, "student.msg.delete.success" );
request.setAttribute( "classes", c );
}
else
{
addMessage( request, "student.msg.update.notexist" );
}
return mapping.findForward( "list" );
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -