Struts学习---------注册与登录

login.jsp

View Code
 1 <%@ page contentType="text/html" pageEncoding="GBK"%>
 2 <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
 3 <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
 4 <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
 5 <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
 6 <center>
 7     <html:form action="/login" method="POST" focus="loginUser.username">        
 8         <table border="0">
 9             <tr>
10                 <th>用户登录</th>
11             </tr>
12             <tr>
13                 <td>用户名:<html:text property="loginUser.username" ></html:text>    </td>
14                 <td><font color="red"><html:errors property="username.null"/></font></td>
15             </tr>
16             <tr>
17                 <td>&nbsp;&nbsp;码:<html:password property="loginUser.password"></html:password></td>
18                 <td><font color="red"><html:errors property="password.null"/></font></td>
19             </tr>
20             <tr>
21                 <td>验证码:&nbsp;<html:text property="code" maxlength="4" size="4"></html:text>    <html:image src="${pageContext.request.contextPath}/Login/image.jsp"/></td>
22                 <td><font color="red"><html:errors property="code.null"/><html:errors property="code.error"/></font></td>
23             </tr>
24             <tr>
25                 <td>
26                     保存密码:<html:select property="saveTime">
27                     <html:option value="0">不保存</html:option>
28                     <html:option value="3600*24">保存一天</html:option>
29                     <html:option value="3600*24*7">保存一周</html:option>
30                     <html:option value="3600*24*30">保存一月</html:option>
31                     <html:option value="3600*24*30*12">保存一年</html:option>
32                 </html:select>
33                 </td>                
34             </tr>
35             <tr>
36                 <th><html:submit value="登录"></html:submit></th>
37                 <td><html:link action="/registerUser">新用户注册</html:link></td>
38                 <td><font color="red"><html:errors property="check.error"/></font></td>
39             </tr>                    
40         </table>
41     </html:form>
42 </center>

RegisterUser.jsp

View Code
 1 <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
 2 <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
 3 <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
 4 <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
 5 <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 7 <html>
 8 <head>
 9     <title>新用户注册页</title>    
10     <script type="text/javascript">
11         var url = "${pageContext.request.contextPath}" ;
12     </script>
13     <script type="text/javascript" language="javascript" src="${pageContext.request.contextPath}/js-auxiliary/validateRegisterInfomation.js"></script>
14 </head>
15 
16 <body>
17 <html:form action="/register" method="POST" focus="newUser.id" onsubmit="return validate(this.value)">        
18         <table border="0">
19             <tr>
20                 <th>新用户注册</th>    
21             </tr>
22             <tr>
23                 <td>&nbsp;&nbsp;ID:<html:text property="newUser.id" onblur="validateUserId(this.value)"></html:text><span id="userid_pic"><font color="red">*</font></span>    </td>
24                 <td><font color="red"><html:errors property="username.null"/><html:errors property="newUser.id" /></font></td>
25             </tr>
26             <tr>
27                 <td>&nbsp;&nbsp;名:<html:text property="newUser.username" onblur="validateUsername(this.value)"></html:text>    <span id="username_pic"><font color="red">*</font></span></td>
28                 <td><font color="red"><html:errors property="username.null"/><html:errors property="newUser.username" /></font></td>
29             </tr>
30             <tr>
31                 <td>&nbsp;&nbsp;&nbsp;&nbsp;码:<html:password property="newUser.password" onblur="validatePassword(this.value)"></html:password><span id="password_pic"><font color="red">*</font></span></td>
32                 <td><font color="red"><html:errors property="password.null"/><html:errors property="newUser.password" /></font></td>
33             </tr>
34             <tr>
35                 <td>重复一遍:<html:password property="password2" onblur="validatePasswordAg(this.value)"></html:password><span id="passwordag_pic"><font color="red">*</font></span></td>
36                 <td><font color="red"><html:errors property="password.null"/><html:errors property="password2" /></font></td>
37             </tr>
38             <tr>
39                 <td>详细地址:<html:text property="newUser.address" onblur="validateAddress(this.value)"></html:text><span id="address_pic"><font color="red">*</font></span></td>
40                 <td><font color="red"><html:errors property="code.null"/><html:errors property="code.error"/><html:errors property="newUser.address" /></font></td>
41             </tr>
42             <tr>
43                 <td>联系方式:<html:text property="newUser.telephone" onblur="validateTelephone(this.value)"></html:text><span id="telephone_pic"><font color="red">*</font></span></td>
44                 <td><font color="red"><html:errors property="code.null"/><html:errors property="code.error"/><html:errors property="newUser.telephone" /></font></td>
45             </tr>
46             <tr>
47                 <td>邮政编码:<html:text property="newUser.zipcode" onblur="validateZipcode(this.value)"></html:text><span id="zipcode_pic"><font color="red">*</font></span></td>
48                 <td><font color="red"><html:errors property="code.null"/><html:errors property="code.error"/><html:errors property="newUser.zipcode" /></font></td>
49             </tr>            
50             <tr>
51                 <th><html:submit value="注册"></html:submit>
52                 <html:reset value="重置"></html:reset></th>
53                 <td><font color="red"><html:errors property="check.error"/></font></td>
54             </tr>                    
55         </table>
56     </html:form>        
57 </body>
58 </html>

validateRegisterInfomation.js

View Code
  1 var xmlHttp;
  2 var flag2 = false;
  3 function creatXMLHttp() {
  4     if (window.XMLHttpRequset) {
  5         xmlHttp = new XMLHttpRequest();
  6     } else {
  7         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  8     }
  9 }
 10 
 11 function checkUserId(userid) {
 12     creatXMLHttp();
 13     xmlHttp.open("POST", "registerAjax.do?newUser.id=" + userid);
 14     xmlHttp.onreadystatechange = checkUserIdCallBack;
 15     xmlHttp.send(null);
 16 }
 17 
 18 function checkUserIdCallBack() {
 19     if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
 20         var flag = xmlHttp.responseText;
 21         if (flag == "true") {
 22             flag2 = false;
 23             document.getElementById("userid_pic").innerHTML = "<img src=\"" + url+ "/Login/images/wrong.gif\"/>"
 24                     + "<font color=\"red\">用户编号已经被注册!</font>"
 25         } else {
 26             flag2 = true;
 27             document.getElementById("userid_pic").innerHTML = "<img src=\"" + url+ "/Login/images/right.gif\"/>"
 28                     + "<font color=\"green\">恭喜,用户编号未被注册!</font>"
 29         }
 30     }
 31 }
 32 function checkRes() {
 33     return flag2;
 34 }
 35 
 36 function validateUserId(userid) {
 37     if (!/^\d{4}$/.test(userid)) {
 38         document.getElementById("userid_pic").innerHTML = "<img src=\"" + url
 39                 + "/Login/images/wrong.gif\"/>"
 40                 + "<font color=\"red\">用户编号必须是四位数字!</font>"
 41         return false;
 42     } else {
 43         checkUserId(userid);
 44         return true;
 45     }
 46 }
 47 function validateUsername(username) {
 48     if (username == "") {
 49         document.getElementById("username_pic").innerHTML = "<img src=\"" + url
 50                 + "/Login/images/wrong.gif\"/>"
 51                 + "<font color=\"red\">用户编号不能为空!</font>"
 52         return false;
 53     } else {
 54         document.getElementById("username_pic").innerHTML = "<img src=\"" + url
 55                 + "/Login/images/right.gif\"/>"
 56                 + "<font color=\"green\">用户名输入正确!</font>"
 57         return true;
 58     }
 59 }
 60 function validatePassword(password) {
 61     if (password == "") {
 62         document.getElementById("password_pic").innerHTML = "<img src=\"" + url
 63                 + "/Login/images/wrong.gif\"/>"
 64                 + "<font color=\"red\">密码不能为空!</font>"
 65         return false;
 66     } else {
 67         document.getElementById("password_pic").innerHTML = "<img src=\"" + url
 68                 + "/Login/images/right.gif\"/>"
 69                 + "<font color=\"green\">密码输入正确!</font>"
 70         return true;
 71     }
 72 }
 73 function validatePasswordAg(passwordag) {
 74     if (passwordag == "") {
 75         document.getElementById("passwordag_pic").innerHTML = "<img src=\""
 76                 + url + "/Login/images/wrong.gif\"/>"
 77                 + "<font color=\"red\">密码不能为空!</font>"
 78         return false;
 79     } else {
 80         if (document.getElementById("newUser.password").value != passwordag) {
 81             document.getElementById("passwordag_pic").innerHTML = "<img src=\""
 82                     + url + "/Login/images/wrong.gif\"/>"
 83                     + "<font color=\"red\">输入密码与上一次输入不一致!</font>"
 84             return false;
 85         } else {
 86             document.getElementById("passwordag_pic").innerHTML = "<img src=\""
 87                     + url + "/Login/images/right.gif\"/>"
 88                     + "<font color=\"green\">密码输入正确!</font>"
 89             return true;
 90         }
 91     }
 92 }
 93 function validateAddress(address) {
 94     if (address == "") {
 95         document.getElementById("address_pic").innerHTML = "<img src=\"" + url
 96                 + "/Login/images/wrong.gif\"/>"
 97                 + "<font color=\"red\">联系地址不能为空!</font>"
 98         return false;
 99     } else {
100         document.getElementById("address_pic").innerHTML = "<img src=\"" + url
101                 + "/Login/images/right.gif\"/>"
102                 + "<font color=\"green\">联系地址输入正确!</font>"
103         return true;
104     }
105 }
106 function validateTelephone(telephone) {
107     if (telephone == "") {
108         document.getElementById("telephone_pic").innerHTML = "<img src=\""
109                 + url + "/Login/images/wrong.gif\"/>"
110                 + "<font color=\"red\">联系方式不能为空!</font>"
111         return false;
112     } else {
113         document.getElementById("telephone_pic").innerHTML = "<img src=\""
114                 + url + "/Login/images/right.gif\"/>"
115                 + "<font color=\"green\">联系方式输入正确!</font>"
116         return true;
117     }
118 }
119 function validateZipcode(zipcode) {
120     if (zipcode == "") {
121         document.getElementById("zipcode_pic").innerHTML = "<img src=\"" + url
122                 + "/Login/images/wrong.gif\"/>"
123                 + "<font color=\"red\">邮政编码不能为空!</font>"
124         return false;
125     } else {
126         if (!/^\d{6}$/.test(zipcode)) {
127             document.getElementById("zipcode_pic").innerHTML = "<img src=\""
128                     + url + "/Login/images/wrong.gif\"/>"
129                     + "<font color=\"red\">邮政编码格式不正确!</font>"
130             return false;
131         } else {
132             document.getElementById("zipcode_pic").innerHTML = "<img src=\""
133                     + url + "/Login/images/right.gif\"/>"
134                     + "<font color=\"green\">邮政编码输入正确!</font>"
135             return true;
136         }
137     }
138 }
139 function validate(formValue) {
140     return  checkRes()&&validateUserId(formValue.id.value)
141             && validateUsername(formValue.username.value)
142             && validatePassword(formValue.password.value)
143             && validatePasswordAg(formValue.passwordag.value)
144             && validateAddress(formValue.address.value)
145             && validateTelephone(formValue.telephone.value)
146             && validateZipcode(formValue.zipcode.value);
147 }

validation.xml

View Code
  1 <!DOCTYPE form-validation PUBLIC
  2           "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
  3           "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
  4 <!--
  5   $Id: validator-rules.xml,v 1.1.2.1 2006/07/01 20:50:46 eugene-proddev Exp $
  6 
  7    This file contains the default Struts Validator pluggable validator
  8    definitions.  It should be placed somewhere under /WEB-INF and
  9    referenced in the struts-config.xml under the plug-in element
 10    for the ValidatorPlugIn.
 11 
 12       <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
 13         <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
 14                                                   /WEB-INF/validation.xml"/>
 15       </plug-in>
 16 
 17    These are the default error messages associated with
 18    each validator defined in this file.  They should be
 19    added to your projects ApplicationResources.properties
 20    file or you can associate new ones by modifying the
 21    pluggable validators msg attributes in this file.
 22 
 23    # Struts Validator Error Messages
 24    errors.required={0} is required.
 25    errors.minlength={0} can not be less than {1} characters.
 26    errors.maxlength={0} can not be greater than {1} characters.
 27    errors.invalid={0} is invalid.
 28 
 29    errors.byte={0} must be a byte.
 30    errors.short={0} must be a short.
 31    errors.integer={0} must be an integer.
 32    errors.long={0} must be a long.
 33    errors.float={0} must be a float.
 34    errors.double={0} must be a double.
 35 
 36    errors.date={0} is not a date.
 37    errors.range={0} is not in the range {1} through {2}.
 38    errors.creditcard={0} is an invalid credit card number.
 39    errors.email={0} is an invalid e-mail address.
 40 
 41    Note: Starting in Struts 1.2.0 the default javascript definitions have
 42          been consolidated to commons-validator. The default can be overridden
 43          by supplying a <javascript> element with a CDATA section, just as
 44          in struts 1.1.
 45 
 46 -->
 47 
 48 <form-validation>
 49     <formset>
 50         <form name="registerForm">
 51             <field property="newUser.id" depends="mask">
 52                 <arg key="prompt.id" name="mask" position="0" resource="true"/>
 53                 <var>
 54                     <var-name>mask</var-name>
 55                     <var-value>^\d{4}$</var-value>
 56                   </var>                    
 57             </field>
 58             
 59             <field property="newUser.username" depends="required">
 60                 <arg key="prompt.username"/>            
 61             </field>
 62             
 63             <field property="newUser.password" depends="required,minlength,maxlength">
 64                 <arg key="prompt.password" position="0"/>
 65                 <arg key="${var:minlength}" name="minlength" resource="false" position="1"/>
 66                 <arg key="${var:maxlength}" name="maxlength" resource="false" position="1"/>            
 67                 <var>
 68                     <var-name>
 69                         maxlength
 70                     </var-name>
 71                     <var-value>
 72                         16
 73                     </var-value>
 74                 </var>
 75                 <var>
 76                     <var-name>
 77                         minlength
 78                     </var-name>
 79                     <var-value>
 80                         3
 81                     </var-value>
 82                 </var>
 83             </field>
 84             <field property="password2" depends="required,validwhen">
 85                 <arg key="prompt.password"/>
 86                 <msg name="validwhen" key="errors.password2"/>
 87                 <var>
 88                     <var-name>test</var-name>
 89                     <var-value>(*this* == newUser.password)</var-value>
 90                 </var>        
 91             </field>
 92             
 93             <field property="newUser.address" depends="required">
 94                 <arg key="prompt.address"/>                        
 95             </field>
 96             
 97             <field property="newUser.telephone" depends="required,mask">
 98                 <arg key="prompt.telephone" name="required"/>
 99                 <arg key="prompt.telephone" name="mask"/>
100                 <var>
101                     <var-name>mask</var-name>
102                     <var-value>^[1-9]\d{0,2}-0\d{1,4}-\d{8,9}$</var-value>
103                 </var>        
104             </field>            
105             <field property="newUser.zipcode" depends="mask">
106                 <arg key="prompt.zipcode"/>    
107                   <var>
108                     <var-name>mask</var-name>
109                     <var-value>^[1-9][0-9]{5}$</var-value>
110                   </var>        
111             </field>
112         </form>
113     </formset>
114 </form-validation>

struts-config.xml

View Code
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
 3 
 4 <struts-config>
 5   <form-beans >
 6     <form-bean name="loginForm" type="org.xiong.struts.form.LoginForm" />
 7     <form-bean name="registerForm" type="org.apache.struts.validator.DynaValidatorForm">
 8         <form-property name="newUser" type="org.xiong.demo.vo.RegisterUser"></form-property>
 9         <form-property name="password2" type="java.lang.String"></form-property>
10     </form-bean>    
11     <form-bean name="registerAjaxForm" type="org.xiong.struts.form.RegisterAjaxForm" />
12   </form-beans>
13 
14   <global-exceptions />
15   <global-forwards />
16   <action-mappings >
17     <action
18       attribute="loginForm"
19       input="/Login/login.jsp"
20       name="loginForm"
21       path="/login"
22       scope="request"
23       type="com.xiong.struts.action.LoginAction"
24       cancellable="true" >
25           <forward name="sucess" path="/Login/welcome.jsp"></forward>
26       </action>
27       
28     <action path="/registerUser" type="org.apache.struts.actions.ForwardAction"  parameter="/Login/RegisterUser.jsp">
29     </action>
30         
31     <action
32       attribute="registerForm"
33       input="/Login/RegisterUser.jsp"
34       name="registerForm"
35       path="/register"
36       scope="request"
37       type="com.xiong.struts.action.RegisterAction"
38       cancellable="true">
39       <forward
40         name="sucess"
41         path="/Login/welcome.jsp"
42         redirect="true" />
43     </action>
44     
45     <action
46       attribute="registerAjaxForm"
47       input="/Login/RegisterUser.jsp"
48       name="registerAjaxForm"
49       path="/registerAjax"
50       scope="request"
51       type="com.xiong.struts.action.RegisterAjaxAction"
52       cancellable="true" >
53           <forward name="sucess" path="/Login/welcome.jsp"></forward>
54     </action>
55    
56 
57   </action-mappings>
58 
59   <message-resources parameter="com.xiong.struts.demo.ApplicationResources" />
60 
61     <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
62         <set-property property="pathnames"
63             value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
64         <set-property property="stopOnFirstError" value="true" />
65     </plug-in>
66 </struts-config>

LoginAction.java

View Code
  1 /*
  2  * Generated by MyEclipse Struts
  3  * Template path: templates/java/JavaClass.vtl
  4  */
  5 package com.xiong.struts.action;
  6 
  7 import javax.servlet.http.Cookie;
  8 import javax.servlet.http.HttpServletRequest;
  9 import javax.servlet.http.HttpServletResponse;
 10 
 11 import org.apache.struts.action.Action;
 12 import org.apache.struts.action.ActionForm;
 13 import org.apache.struts.action.ActionForward;
 14 import org.apache.struts.action.ActionMapping;
 15 import org.apache.struts.action.ActionMessage;
 16 import org.apache.struts.action.ActionMessages;
 17 import org.xiong.demo.factory.DaoFactory;
 18 import org.xiong.demo.util.MessageUtil;
 19 import org.xiong.demo.vo.RegisterUser;
 20 import org.xiong.struts.form.LoginForm;
 21 
 22 /**
 23  * MyEclipse Struts Creation date: 04-17-2012
 24  * 
 25  * XDoclet definition:
 26  * 
 27  * @struts.action path="/login" name="loginForm" input="/Login/login.jsp"
 28  *                scope="request" validate="true"
 29  */
 30 public class LoginAction extends Action
 31 {
 32     /*
 33      * Generated Methods
 34      */
 35 
 36     /**
 37      * Method execute
 38      * 
 39      * @param mapping
 40      * @param form
 41      * @param request
 42      * @param response
 43      * @return ActionForward
 44      */
 45     public ActionForward execute(ActionMapping mapping, ActionForm form,
 46             HttpServletRequest request, HttpServletResponse response)
 47     {
 48         LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method                                                // stub
 49         ActionMessages errors = new ActionMessages();
 50         RegisterUser user = new RegisterUser();
 51         user.setUsername(loginForm.getLoginUser().getUsername());
 52         user.setPassword(loginForm.getLoginUser().getPassword());        
 53         long cookieTime = 0;
 54         try
 55         {
 56             cookieTime = Long.parseLong(loginForm.getSaveTime());
 57         }
 58         catch (Exception ex)
 59         {
 60 
 61         }
 62         RegisterUser register = null;
 63         try
 64         {
 65             register = DaoFactory.getUserDaoInstance().findLongin(user);
 66         }
 67         catch (Exception e)
 68         {
 69             e.printStackTrace();
 70         }
 71         if (register != null)
 72         {
 73             if (register.getId() > 0
 74                     && loginForm.getCode().equalsIgnoreCase(
 75                             (String) request.getSession().getAttribute("rand")))
 76             {
 77                 request.getSession().setAttribute("username",
 78                         loginForm.getLoginUser().getUsername());
 79                 request.getSession().setAttribute("userid", register.getId());
 80                 request.getSession().setAttribute("user", register);
 81                 Cookie c1 = new Cookie("username", loginForm.getLoginUser()
 82                         .getUsername());
 83                 Cookie c2 = new Cookie("password", loginForm.getLoginUser()
 84                         .getPassword());
 85                 c1.setMaxAge((int) cookieTime);
 86                 c2.setMaxAge((int) cookieTime);
 87                 response.addCookie(c1);
 88                 response.addCookie(c2);
 89                 return mapping.findForward("sucess");
 90             }
 91             else
 92             {
 93                 errors.add("check.error",
 94                         new ActionMessage("check.input.error"));
 95                 this.addErrors(request, errors);
 96                 return mapping.getInputForward();
 97             }
 98         }
 99         else
100         {
101             errors.add("check.error", new ActionMessage("check.input.error"));
102             this.addErrors(request, errors);
103             return mapping.getInputForward();
104         }
105     }
106 }

RegisterAction.java

View Code
 1 /*
 2  * Generated by MyEclipse Struts
 3  * Template path: templates/java/JavaClass.vtl
 4  */
 5 package com.xiong.struts.action;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import org.apache.struts.action.Action;
11 import org.apache.struts.action.ActionForm;
12 import org.apache.struts.action.ActionForward;
13 import org.apache.struts.action.ActionMapping;
14 import org.apache.struts.action.ActionMessage;
15 import org.apache.struts.action.ActionMessages;
16 import org.apache.struts.validator.DynaValidatorForm;
17 import org.xiong.demo.factory.DaoFactory;
18 import org.xiong.demo.util.MessageUtil;
19 import org.xiong.demo.vo.RegisterUser;
20 
21 /**
22  * MyEclipse Struts Creation date: 04-17-2012
23  * 
24  * XDoclet definition:
25  * 
26  * @struts.action path="/register" name="registerForm"
27  *                input="/Login/RegisterUser.jsp" scope="request"
28  *                validate="true"
29  * @struts.action-forward name="sucess" path="/Login/welcome.jsp"
30  *                        redirect="true"
31  */
32 public class RegisterAction extends Action
33 {
34     /*
35      * Generated Methods
36      */
37 
38     /**
39      * Method execute
40      * 
41      * @param mapping
42      * @param form
43      * @param request
44      * @param response
45      * @return ActionForward
46      */
47     public ActionForward execute(ActionMapping mapping, ActionForm form,
48             HttpServletRequest request, HttpServletResponse response)
49     {
50         DynaValidatorForm registerForm = (DynaValidatorForm) form;
51         RegisterUser user = (RegisterUser) registerForm.get("newUser");
52         user.setLastdate(new java.util.Date());
53         ActionMessages errors = new ActionMessages();
54         try
55         {
56             if (DaoFactory.getUserDaoInstance().doInsert(user))
57             {
58                 errors.add("register.sucess", new ActionMessage(
59                         "register.add.true"));
60                 super.addErrors(request, errors);
61                 request.getSession().setAttribute("username",
62                         user.getUsername());
63                 request.getSession().setAttribute("id", user.getId());
64                 return mapping.findForward("sucess");
65 
66             }
67         }
68         catch (Exception e)
69         {
70             e.printStackTrace();
71         }
72 
73         errors.add("register.failure", new ActionMessage("register.add.fase"));
74         super.addErrors(request, errors);
75         return mapping.getInputForward();
76 
77     }
78 }

RegisterAjaxAction.java

View Code
 1 /*
 2  * Generated by MyEclipse Struts
 3  * Template path: templates/java/JavaClass.vtl
 4  */
 5 package com.xiong.struts.action;
 6 
 7 import java.io.PrintWriter;
 8 
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import org.apache.struts.action.Action;
12 import org.apache.struts.action.ActionForm;
13 import org.apache.struts.action.ActionForward;
14 import org.apache.struts.action.ActionMapping;
15 import org.xiong.demo.factory.DaoFactory;
16 import org.xiong.demo.vo.RegisterUser;
17 import org.xiong.struts.form.RegisterAjaxForm;
18 
19 /**
20  * MyEclipse Struts Creation date: 04-18-2012
21  * 
22  * XDoclet definition:
23  * 
24  * @struts.action path="/registerAjax" name="registerAjaxForm"
25  *                input="/Login/RegisterUser.jsp" scope="request"
26  *                validate="true"
27  */
28 public class RegisterAjaxAction extends Action
29 {
30     /*
31      * Generated Methods
32      */
33 
34     /**
35      * Method execute
36      * 
37      * @param mapping
38      * @param form
39      * @param request
40      * @param response
41      * @return ActionForward
42      */
43     public ActionForward execute(ActionMapping mapping, ActionForm form,
44             HttpServletRequest request, HttpServletResponse response)
45     {
46         RegisterAjaxForm registerAjaxForm = (RegisterAjaxForm) form;
47 
48         int id = registerAjaxForm.getNewUser().getId();
49         try
50         {
51             PrintWriter out = response.getWriter();
52             RegisterUser user = DaoFactory.getUserDaoInstance().findById(id);
53             if (user == null)
54             {
55                 response.setContentType("text/html;charset=GBK");
56                 out.print("false");
57                 out.close();
58             }
59             else
60             {
61                 out.print("true");
62                 out.close();
63             }
64         }
65         catch (Exception e)
66         {
67             e.printStackTrace();
68         }
69         return null;
70     }
71 }

LoginForm.java

View Code
  1 /*
  2  * Generated by MyEclipse Struts
  3  * Template path: templates/java/JavaClass.vtl
  4  */
  5 package org.xiong.struts.form;
  6 
  7 import javax.servlet.http.Cookie;
  8 import javax.servlet.http.HttpServletRequest;
  9 
 10 import org.apache.struts.action.ActionErrors;
 11 import org.apache.struts.action.ActionForm;
 12 import org.apache.struts.action.ActionMapping;
 13 import org.apache.struts.action.ActionMessage;
 14 import org.xiong.demo.factory.DaoFactory;
 15 import org.xiong.demo.util.MessageUtil;
 16 import org.xiong.demo.vo.RegisterUser;
 17 
 18 /**
 19  * MyEclipse Struts Creation date: 04-17-2012
 20  * 
 21  * XDoclet definition:
 22  * 
 23  * @struts.form name="loginForm"
 24  */
 25 public class LoginForm extends ActionForm
 26 {
 27     private RegisterUser loginUser = new RegisterUser();
 28     private String code;
 29     private String saveTime;
 30 
 31     public String getSaveTime()
 32     {
 33         return saveTime;
 34     }
 35 
 36     public void setSaveTime(String saveTime)
 37     {
 38         this.saveTime = saveTime;
 39     }
 40 
 41     public RegisterUser getLoginUser()
 42     {
 43         return loginUser;
 44     }
 45 
 46     public void setLoginUser(RegisterUser loginUser)
 47     {
 48         this.loginUser = loginUser;
 49     }
 50 
 51     /*
 52      * Generated Methods
 53      */
 54 
 55     public String getCode()
 56     {
 57         return code;
 58     }
 59 
 60     public void setCode(String code)
 61     {
 62         this.code = code;
 63     }
 64 
 65     /**
 66      * Method validate
 67      * 
 68      * @param mapping
 69      * @param request
 70      * @return ActionErrors
 71      */
 72     public ActionErrors validate(ActionMapping mapping,
 73             HttpServletRequest request)
 74     {
 75         
 76         ActionErrors errors = new ActionErrors();
 77         String rand = (String) request.getSession().getAttribute("rand");
 78 
 79         if (("".equals(this.loginUser.getUsername()) || null == this.loginUser
 80                 .getUsername())
 81                 || ("".equals(this.getLoginUser().getPassword()) || null == this
 82                         .getLoginUser().getPassword()))
 83         {
 84             errors.add("username.null",
 85                     new ActionMessage("username.input.null"));
 86         }
 87         if ("".equals(this.loginUser.getPassword())
 88                 || null == this.loginUser.getPassword())
 89         {
 90             errors.add("password.null",
 91                     new ActionMessage("password.input.null"));
 92         }
 93 
 94         if ("".equals(code) || null == code)
 95         {
 96             errors.add("code.null", new ActionMessage("code.input.null"));
 97         }
 98         else
 99         {
100             if (!code.equalsIgnoreCase(rand))
101             {
102                 errors.add("code.error", new ActionMessage("code.input.error"));
103             }
104         }
105         return errors;
106     }
107 
108     /**
109      * Method reset
110      * 
111      * @param mapping
112      * @param request
113      */
114     public void reset(ActionMapping mapping, HttpServletRequest request)
115     {
116         // TODO Auto-generated method stub
117     }
118 }

RegisterAjaxForm.java

View Code
 1 /*
 2  * Generated by MyEclipse Struts
 3  * Template path: templates/java/JavaClass.vtl
 4  */
 5 package org.xiong.struts.form;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 
 9 import org.apache.struts.action.ActionErrors;
10 import org.apache.struts.action.ActionForm;
11 import org.apache.struts.action.ActionMapping;
12 import org.xiong.demo.vo.RegisterUser;
13 
14 /**
15  * MyEclipse Struts Creation date: 04-18-2012
16  * 
17  * XDoclet definition:
18  * 
19  * @struts.form name="registerAjaxForm"
20  */
21 public class RegisterAjaxForm extends ActionForm
22 {
23     private RegisterUser newUser=new RegisterUser();
24 
25     /*
26      * Generated Methods
27      */
28 
29     public RegisterUser getNewUser()
30     {
31         return newUser;
32     }
33 
34     public void setNewUser(RegisterUser newUser)
35     {
36         this.newUser = newUser;
37     }
38 
39     /**
40      * Method validate
41      * 
42      * @param mapping
43      * @param request
44      * @return ActionErrors
45      */
46     public ActionErrors validate(ActionMapping mapping,
47             HttpServletRequest request)
48     {
49         // TODO Auto-generated method stub
50         return null;
51     }
52 
53     /**
54      * Method reset
55      * 
56      * @param mapping
57      * @param request
58      */
59     public void reset(ActionMapping mapping, HttpServletRequest request)
60     {
61         // TODO Auto-generated method stub
62     }
63 }

 

 

 

 

posted on 2012-04-18 12:59  茫然若失  阅读(330)  评论(0编辑  收藏  举报

导航