首页 > 代码库 > struts2-15-用户名校验

struts2-15-用户名校验

校验分为:

    1:JavaScript  正则表达式

    2:JavaEE中validation()

 

一:用户注册信息  UserDetails.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="s" uri="/struts-tags"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>注册页面</title>
 9 </head>
10 <body>
11 <s:fielderror></s:fielderror>
12     <form action="UserDetailsAction" method="post">
13         用户名:<input type="text" name="username" /><br> 
14         生日:<input type="text" name="birthday" />(格式为:yyyy-mm-dd)<br> 
15         详细地址:<input type="text" name="address">&nbsp;&nbsp;
16                <input type="text" name="address">&nbsp;&nbsp;
17                <input type="text" name="address"><br>
18                <input type="text" name="address"/>        
19         <input type="submit" value="注册">
20     </form>
21 </body>
22 </html>

 

 

 

二:在action写set get方法 nuc.sw.action--->UserDetailsAction.java

  

 1 package nuc.sw.action;
 2 import java.util.Date;
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 import nuc.sw.vo.Address;
 6 public class UserDetailsAction extends ActionSupport{
 7        private String username;
 8        //注意类型
 9        private Date birthday;
10        //必须为Address类型。否则获取不到。
11        private Address address;
12     public Date getBirthday() {
13         return birthday;
14     }
15     public void setBirthday(Date birthday) {
16         this.birthday = birthday;
17     }
18     public String getUsername() {
19         return username;
20     }
21     public void setUsername(String username) {
22         this.username = username;
23     }
24 
25 
26 
27     public Address getAddress() {
28         return address;
29     }
30 
31 
32     public void setAddress(Address address) {
33         this.address = address;
34     }
35 
36 
37     public String RegisterMethod() throws Exception {
38         return SUCCESS;
39    }
40         
41 }

 

三:获取Address类型  所以写Address.java --->nuc.sw.vo

 

package nuc.sw.vo;

public class Address {

    private String province;
    private String city;
    private String county;
    private String street;
    public Address(String province, String city, String county, String street) {
        super();
        this.province = province;
        this.city = city;
        this.county = county;
        this.street = street;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCounty() {
        return county;
    }

    public void setCounty(String county) {
        this.county = county;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

}

 

 

四:配置struts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7  <constant name="struts.devMode" value="http://www.mamicode.com/true" />
 8  <package name="default" namespace="/" extends="struts-default">
 9      <action name="UserDetailsAction" class="nuc.sw.action.UserDetailsAction" method="RegisterMethod">
10          <result>/showUserDetails.jsp</result>
11          <result name="input">/UserDetails.jsp</result>
12      </action>
13  </package>
14 </struts>

 

 

五:用户提交信息后进行显示  showUserDetails.jsp

生日的月份需要MM---mm指时间

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@ taglib prefix="s" uri="/struts-tags"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 8 <title>注册成功后登录</title>
 9 </head>
10 <body>
11 用户名:<s:property value="http://www.mamicode.com/username"/><br>
12 生日:   <s:date name="birthday" format="yyyy-MM-dd"/><br>
13 您的详细地址为:<br>
14     <s:property value="http://www.mamicode.com/address.province"/>省
15     <s:property value="http://www.mamicode.com/address.city"/>市
16     <s:property value="http://www.mamicode.com/address.county"/>区
17     <s:property value="http://www.mamicode.com/address.street"/>
18 </body>
19 </html>

 

 

六:提交的地址信息为字符串String,最后要以对象的形式输出,所以需要自定义转换器

     nuc.sw.action--->AddressConverter.java

 1 package nuc.sw.action;
 2 
 3 import java.util.Map;
 4 
 5 import org.apache.struts2.util.StrutsTypeConverter;
 6 import nuc.sw.vo.Address;
 7 public class AddressConverter extends StrutsTypeConverter {
 8 
 9     /*
10      *1:转换器必须继承StrutsTypeConverter
11      *2:自带方法  字符串转为value(address)
12      *3:自带方法  value转为字符串 
13      */
14     @Override
15     public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
16         
17         //此处可以简略代码,但是必须在Address.java写构造方法
18         Address address=new Address(arg1[0],arg1[1],arg1[2],arg1[3]);
19 
20         //String[]中放Address中的4个值
21 //        address.setProvince(arg1[0]);
22 //        address.setCity(arg1[1]);
23 //        address.setCounty(arg1[2]);
24 //        address.setStreet(arg1[3]);
25         return address;
26     }
27 
28     
29     @Override
30     public String convertToString(Map arg0, Object arg1) {
31         //ag1是父类,Address是子类,需要强制转换。
32         if(arg1 instanceof Address){
33             Address a=(Address)arg1;
34             return a.getProvince()+"省"+a.getCity()+"市"+a.getCounty()+"区"+a.getStreet();
35         }
36         else
37           return null;
38     }
39 
40 }

 

 

七:局部转换器 ---写action中属性名建的配置文件---和action位于同级目录下

address=nuc.sw.action.AddressConverter

 

八:对用户名进行校验     nuc/sw/action--->UserDetailsAction-validation.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2   <!DOCTYPE validators PUBLIC 
 3           "-//Apache Struts//XWork Validator 1.0.2//EN"
 4           "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
 5  <validators>
 6    <field name="username">
 7        <field-validator type="requiredstring">
 8          <param name="trim">true</param>
 9          <message>用户名不能为空</message>
10        </field-validator>
11        <field-validator type="regex">
12          <param name="regexExpression">^\w{4,25}$</param>
13          <message>用户名必须为4-25位 </message>
14        </field-validator>
15    </field>
16  </validators>

 

九:项目结构:

 

技术分享

 

 

十:运行结果

技术分享

 

技术分享

 技术分享

 

 

struts2-15-用户名校验