📄 studentactions.java
字号:
/* * StudentActions.java * * Created on 2006年6月6日, 下午1:15 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package enova.web.struts.action;import javax.servlet.http.*;import org.apache.struts.action.*;import enova.service.*;import enova.pojo.*;import enova.util.Const;/** * * @author vlinux */public class StudentActions extends DispatchAction { private Integer id; private String number; private String name; private String sex; private String nation; private String birthday; private String birthPlace; private Integer enrollYear; private Integer classId; private String password; private StudentService ss = (StudentService)ServiceFactory.makeService("StudentService"); private void loadForm(javax.servlet.http.HttpServletRequest request){ this.number = request.getParameter("number"); this.name = request.getParameter("name"); this.sex = request.getParameter("sex"); this.nation = request.getParameter("nation"); this.birthday = request.getParameter("birthday"); this.birthPlace = request.getParameter("birthPlace"); this.password = request.getParameter("password"); try{ int intId = Integer.parseInt(request.getParameter("id")); this.id = new Integer(intId); }catch(Exception e){ this.id = null; } try{ int intClassId = Integer.parseInt(request.getParameter("classId")); this.classId = new Integer(intClassId); }catch(Exception e){ this.classId = null; } try{ int intEnrollYear = Integer.parseInt(request.getParameter("enrollYear")); this.enrollYear = new Integer(intEnrollYear); }catch(Exception e){ this.enrollYear = null; } } //添加学生 public ActionForward add(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { if( this.verifyRoot(request) ) return mapping.findForward(Const.ACCESS_DENY); try{ loadForm(request); Student s = new Student(number,name,enrollYear,password,classId); s.setBirthPlace(birthPlace); s.setBirthday(java.sql.Date.valueOf(birthday)); s.setNation(nation); s.setSex(sex); ss.updateOrCreate(s); }catch(StoreException se){ request.setAttribute(Const.MESSAGES,"数据库出错"); return mapping.findForward(Const.STORE_EXCEPTION); }catch(UniqueException ue){ request.setAttribute(Const.MESSAGES,"该记录已经存在"); return mapping.findForward(Const.ADD); } request.setAttribute(Const.MESSAGES,"创建成功"); return mapping.findForward(Const.ADD); } //删除学生 public ActionForward delete(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { if( this.verifyRoot(request) ) return mapping.findForward(Const.ACCESS_DENY); try{ loadForm(request); ss.delete(id); request.setAttribute("students",ss.getByClassId(null)); }catch(StoreException se){ request.setAttribute(Const.MESSAGES,"数据库出错"); return mapping.findForward(Const.STORE_EXCEPTION); }catch(NotExistException nee){ request.setAttribute(Const.MESSAGES,"记录不存在"); return mapping.findForward(Const.VIEW); } request.setAttribute(Const.MESSAGES,"删除成功"); return mapping.findForward(Const.VIEW); } //修改学生 public ActionForward update(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { if( this.verifyRoot(request) ) return mapping.findForward(Const.ACCESS_DENY); try{ loadForm(request); Student s = new Student(number,name,enrollYear,password,classId); s.setBirthPlace(birthPlace); s.setBirthday(java.sql.Date.valueOf(birthday)); s.setNation(nation); s.setSex(sex); s.setId(id); ss.updateOrCreate(s); }catch(StoreException se){ request.setAttribute(Const.MESSAGES,"数据库出错"); return mapping.findForward(Const.STORE_EXCEPTION); }catch(UniqueException ue){ request.setAttribute(Const.MESSAGES,"该记录已经存在"); return mapping.findForward(Const.UPDATE); }finally{ try{ request.setAttribute("student",ss.get(id)); }catch(StoreException se){ request.setAttribute(Const.MESSAGES,"数据库出错"); return mapping.findForward(Const.STORE_EXCEPTION); } } request.setAttribute(Const.MESSAGES,"修改成功"); return mapping.findForward(Const.UPDATE); } //获取学生 public ActionForward get(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { if( this.verifyRoot(request) ) return mapping.findForward(Const.ACCESS_DENY); try{ loadForm(request); request.setAttribute("student",ss.get(id)); }catch(StoreException se){ request.setAttribute(Const.MESSAGES,"数据库出错"); return mapping.findForward(Const.STORE_EXCEPTION); } return mapping.findForward(Const.UPDATE); } //查看学生 public ActionForward view(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { if( this.verifyRoot(request) ) return mapping.findForward(Const.ACCESS_DENY); try{ loadForm(request); java.util.List list = null; if( number!=null && number.length()>0 ) list = ss.getByNumber(number); else if( classId!=null && (name==null||name.length()<=0) ) list = ss.getByClassId(classId); else if( classId==null && name!=null ) list = ss.getByName(name); else if( classId!=null && name!=null ) list = ss.getByClassIdAndName(classId,name); else{ list = ss.getByClassId(null); } request.setAttribute("students",list); }catch(StoreException se){ request.setAttribute(Const.MESSAGES,"数据库出错"); return mapping.findForward(Const.STORE_EXCEPTION); } return mapping.findForward(Const.VIEW); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -