user表的结构如下
加入struts
采用DispatchAction
Struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans > <form-bean name="userForm" type="dj.fantlam.myssh.struts.form.UserForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action attribute="userForm" input="/jsp/errors.jsp" name="userForm" parameter="status" path="/jsp/user" scope="request" type="dj.fantlam.myssh.struts.action.UserAction" /> <forward name="regsuc" path="/jsp/index.jsp" /> <forward name="regfail" path="/jsp/register.jsp"></forward> </action-mappings> <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller> <message-resources parameter="dj.fantlam.myssh.struts.ApplicationResources" /> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" /> </plug-in> </struts-config>
errors.jsp
</body>
<META NAME="Generator" CONTENT="Struts + Spring + Hibernate + MySQL + Tomcat + CP">
</html:html>
参考struts的API
public class ActionErrors
extends ActionMessages
implements java.io.Serializable
Action
saveErrors(javax.servlet.http.HttpSession session, ActionMessages errors)
Save the specified error messages keys into the appropriate session attribute for use by the <html:messages> tag (if messages="false") or <html:errors>, if any error messages are required.
Userform.java
加多三个属性(注意都要生成setter和getter方法)
private String confirmpwd; private String checkcode; private int type; // 1:表示注册功能 // 2:表示登陆 // 3:表示忘记密码,确认用户是否存在 // 4:表示修改密码 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { //Struts前台表单的验证,根据type做不同的验证 ActionErrors errors = new ActionErrors(); if (type == 1) { if (this.userid == null || "".equals(this.userid)) { errors.add("userid", new ActionMessage("user.userid.null")); } if (this.userpwd == null || "".equals(this.userpwd)) { errors.add("userpwd", new ActionMessage("user.userpwd.null")); } else { if (!(this.userpwd.equals(this.confirmpwd))) { errors.add("confpwd", new ActionMessage( "user.confpwd.error")); } } if (this.userques == null || "".equals(this.userques)) { errors.add("userques", new ActionMessage("user.userques.null")); } if (this.userans == null || "".equals(this.userans)) { errors.add("userans", new ActionMessage("user.userans.null")); } if(this.checkcode==null||"".equals(this.checkcode)){ errors.add("checkcode", new ActionMessage("user.checkcode.null")); } } return errors; }
UserAction.java
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package dj.fantlam.myssh.struts.action; 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 org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import org.apache.struts.actions.DispatchAction; import dj.fantlam.myssh.struts.form.UserForm; /** * MyEclipse Struts * Creation date: 07-15-2008 * * XDoclet definition: * @struts.action path="/user" name="userForm" input="error.jsp" parameter="status" scope="request" validate="true" */ UserAction是继承自DispatchAction的,根据传来的status的值判断交给那个ActionForward处理 public class UserAction extends DispatchAction { /* * Generated Methods */ /** * Method execute * @param mapping * @param form * @param request * @param response * @return ActionForward */ public ActionForward register(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form;// TODO Auto-generated method stub /* ActionErrors和ActionMessages的关系如下 * public class ActionErrors extends ActionMessages implements * java.io.Serializable * * * 类Action * saveErrors(javax.servlet.http.HttpSession session, * ActionMessages errors) Save the specified error messages keys into * the appropriate session attribute for use by the <html:messages>tag * (if messages="false") or <html:errors>, if any error messages are required. */ //验证输入的验证码是否正确 String ccode=(String)request.getSession().getAttribute("ccode"); String checkcode=userForm.getCheckcode(); if(!(ccode.equals(checkcode))){ ActionMessages errors=new ActionMessages(); errors.add("ccode", new ActionMessage("checkcode.error")); super.saveErrors(request, errors); return mapping.getInputForward(); } return null; }