1、public class ForumExceptionHandler extends ExceptionHandler Logger log = Logger.getLogger(getClass(); Override public ActionForward execute(Exception exception, ExceptionConfig config, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletExc
2、eption request.setAttribute(“exception“, exception); log.info(exception, exception); if (exception instanceof AccountException) return new ActionForward(“login“, “/form/person/login.jsp“, false); return new ActionForward(“exception“, “/form/exception.jsp“, false); public abstract class ForumAction e
3、xtends DispatchAction protected Log log = LogFactory.getLog(getClass(); Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ForumForm forumForm = (ForumForm) form; forumForm.setTitle(“轻量级 Java EE 论坛程
4、序“); if (forumForm.getAction() = null | forumForm.getAction().trim().length() = 0) return this.list(mapping, form, request, response); return super.execute(mapping, form, request, response); public abstract ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpSe
5、rvletResponse response) throws Exception; public class PersonAction extends ForumAction private IPersonService personService; /* * 默认方法 返回到注册页面 */ public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) / PersonForm personForm = (Pe
6、rsonForm) form; / / List personList = personService.list(“ from Person “, 0, 100, / null); / / request.setAttribute(“personList“, personList); / / return mapping.findForward(“list“); return this.initAdd(mapping, form, request, response); /* * 显示注册页面 * param mapping * param form * param request * par
7、am response * return */ public ActionForward initAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) PersonForm personForm = (PersonForm) form; personForm.setTitle(“用户注册“); return mapping.findForward(“add“); /* * 显示登录页面 * param mapping * param form *
8、 param request * param response * return */ public ActionForward initLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) PersonForm personForm = (PersonForm) form; personForm.setTitle(“用户登录“); return new ActionForward(“login“, “/form/person/login.j
9、sp“, false); /* * 注册 * param mapping * param form * param request * param response * return */ public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) PersonForm personForm = (PersonForm) form; personForm.setTitle(“用户注册“); Person per
10、son = personForm.getPerson(); person.setIpCreated(request.getRemoteAddr(); person.setIpLastActived(request.getRemoteAddr(); person.setDateCreated(new Date(); person.setDateLastActived(new Date(); if (person.getAccount() = null | person.getAccount().trim().length() = 0) request.setAttribute(“message“
11、, “请输入帐号“); return this.initAdd(mapping, form, request, response); if (person.getPassword() = null | person.getPassword().trim().length() = 0 | !person.getPassword().equals(personForm.getPassword() request.setAttribute(“message“, “密码不一致“); return this.initAdd(mapping, form, request, response); try p
12、ersonService.create(person); PersonUtil.setPersonInf(request, response, person); request.setAttribute(“message“, “注册成功“); return new ActionForward(“success“, “/form/person/success.jsp“, false); catch (Exception e) request.setAttribute(“message“, “注册失败,原因:“ + e.getMessage(); return this.initAdd(mappi
13、ng, form, request, response); /* * 登录 * param mapping * param form * param request * param response * return * throws Exception */ public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception PersonForm personForm = (Pe
14、rsonForm) form; personForm.setTitle(“用户登录“); Person person = personService.getPerson(personForm.getPerson() .getAccount(), personForm.getPerson().getPassword(); if (person = null) throw new AccountException(“用户名密码错误“); PersonUtil.setPersonInf(request, response, person); person.setIpLastActived(reque
15、st.getRemoteAddr(); person.setDateLastActived(new Date(); personService.save(person); request.setAttribute(“message“, “欢迎回来“); return new ActionForward(“success“, “/form/person/success.jsp“, false); /* * 注销 * param mapping * param form * param request * param response * return * throws Exception */
16、public ActionForward logout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception PersonForm personForm = (PersonForm) form; personForm.setTitle(“用户注销“); request.getSession(true).setAttribute(PersonUtil.PERSON_INFO, null); return new Actio
17、nForward(“success“, “/“, true); /* * 查看用户信息 * param mapping * param form * param request * param response * return * throws Exception */ public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception PersonForm personForm
18、= (PersonForm) form; personForm.setTitle(“查看用户资料“); Person person = personService.find(Person.class, personForm.getPerson() .getId(); request.setAttribute(“person“, person); return new ActionForward(“view“, “/form/person/viewPerson.jsp“, false); public IPersonService getPersonService() return person
19、Service; public void setPersonService(IPersonService personService) this.personService = personService; public class PersonAction extends ForumAction private IPersonService personService; /* * 默认方法 返回到注册页面 */ public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest reques
20、t, HttpServletResponse response) / PersonForm personForm = (PersonForm) form; / / List personList = personService.list(“ from Person “, 0, 100, / null); / / request.setAttribute(“personList“, personList); / / return mapping.findForward(“list“); return this.initAdd(mapping, form, request, response);
21、/* * 显示注册页面 * param mapping * param form * param request * param response * return */ public ActionForward initAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) PersonForm personForm = (PersonForm) form; personForm.setTitle(“用户注册“); return mapping.
22、findForward(“add“); /* * 显示登录页面 * param mapping * param form * param request * param response * return */ public ActionForward initLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) PersonForm personForm = (PersonForm) form; personForm.setTitle(“用
23、户登录“); return new ActionForward(“login“, “/form/person/login.jsp“, false); /* * 注册 * param mapping * param form * param request * param response * return */ public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) PersonForm personFor
24、m = (PersonForm) form; personForm.setTitle(“用户注册“); Person person = personForm.getPerson(); person.setIpCreated(request.getRemoteAddr(); person.setIpLastActived(request.getRemoteAddr(); person.setDateCreated(new Date(); person.setDateLastActived(new Date(); if (person.getAccount() = null | person.ge
25、tAccount().trim().length() = 0) request.setAttribute(“message“, “请输入帐号“); return this.initAdd(mapping, form, request, response); if (person.getPassword() = null | person.getPassword().trim().length() = 0 | !person.getPassword().equals(personForm.getPassword() request.setAttribute(“message“, “密码不一致“)
26、; return this.initAdd(mapping, form, request, response); try personService.create(person); PersonUtil.setPersonInf(request, response, person); request.setAttribute(“message“, “注册成功“); return new ActionForward(“success“, “/form/person/success.jsp“, false); catch (Exception e) request.setAttribute(“me
27、ssage“, “注册失败,原因:“ + e.getMessage(); return this.initAdd(mapping, form, request, response); /* * 登录 * param mapping * param form * param request * param response * return * throws Exception */ public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletR
28、esponse response) throws Exception PersonForm personForm = (PersonForm) form; personForm.setTitle(“用户登录“); Person person = personService.getPerson(personForm.getPerson() .getAccount(), personForm.getPerson().getPassword(); if (person = null) throw new AccountException(“用户名密码错误“); PersonUtil.setPerso
29、nInf(request, response, person); person.setIpLastActived(request.getRemoteAddr(); person.setDateLastActived(new Date(); personService.save(person); request.setAttribute(“message“, “欢迎回来“); return new ActionForward(“success“, “/form/person/success.jsp“, false); /* * 注销 * param mapping * param form *
30、param request * param response * return * throws Exception */ public ActionForward logout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception PersonForm personForm = (PersonForm) form; personForm.setTitle(“用户注销“); request.getSession(true
31、).setAttribute(PersonUtil.PERSON_INFO, null); return new ActionForward(“success“, “/“, true); /* * 查看用户信息 * param mapping * param form * param request * param response * return * throws Exception */ public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpSer
32、vletResponse response) throws Exception PersonForm personForm = (PersonForm) form; personForm.setTitle(“查看用户资料“); Person person = personService.find(Person.class, personForm.getPerson() .getId(); request.setAttribute(“person“, person); return new ActionForward(“view“, “/form/person/viewPerson.jsp“, false); public IPersonService getPersonService() return personService; public void setPersonService(IPersonService personService) this.personService = personService;