struts2的验证

struts2的验证和struts1的验证相对来说简单很多,注意的是在Action类的同一级目录下加一个

Action名-validation.xml文件就即可。下面是例题:



1.user.java

Java代码
package com.lxitedu.validate;  
 
public class User {  
    private String name;  
    private String password;  
    private int age;  
    private String email;  
 
    public String getName() {  
        return name;  
    }  
 
    public void setName(String name) {  
        this.name = name;  
    }  
 
    public String getPassword() {  
        return password;  
    }  
 
    public void setPassword(String password) {  
        this.password = password;  
    }  
 
    public int getAge() {  
        return age;  
    }  
 
    public void setAge(int age) {  
        this.age = age;  
    }  
 
    public String getEmail() {  
        return email;  
    }  
 
    public void setEmail(String email) {  
        this.email = email;  
    }  
 
    @Override 
    public String toString() {  
        return "User [age=" + age + ", email=" + email + ", name=" + name  
                + ", password=" + password + ", sex=" +  "]";  
    }  


package com.lxitedu.validate;

public class User {
private String name;
private String password;
private int age;
private String email;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return "User [age=" + age + ", email=" + email + ", name=" + name
+ ", password=" + password + ", sex=" +  "]";
}
}
2.Action.java



Java代码
package com.lxitedu.validate;  
 
import com.opensymphony.xwork2.ActionSupport;  
import com.opensymphony.xwork2.ModelDriven;  
 
public class Action extends ActionSupport implements ModelDriven<User> {  
  private static final long serialVersionUID = 1L;  
  private User user = new User();  
 
  @Override 
  public String execute() throws Exception {  
    return SUCCESS;  
  }  
 
  @Override 
  public User getModel() {  
    return user;  
  }  
    
  public String run() throws Exception {  
    return INPUT;  
  }  


package com.lxitedu.validate;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class Action extends ActionSupport implements ModelDriven<User> {
  private static final long serialVersionUID = 1L;
  private User user = new User();

  @Override
  public String execute() throws Exception {
    return SUCCESS;
  }

  @Override
  public User getModel() {
    return user;
  }
 
  public String run() throws Exception {
    return INPUT;
  }
}
3.在与Action.java的同一级目录下定义一个Action-validation.xml文件



Xml代码
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"  
    "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> 
 
<validators> 
    <field name="name"> 
        <field-validator type="requiredstring"> 
            <param name="trim">true</param> 
            <message>必须输入名字</message> 
        </field-validator> 
        <field-validator type="regex"> 
            <param name="expression"><![CDATA[(\w{4,25})]]></param> 
            <message>您输入的用户名只能是字母和数组,且长度必须在4到25之间</message> 
        </field-validator> 
    </field> 
    <field name="password"> 
        <field-validator type="requiredstring"> 
            <param name="trim">true</param> 
            <message>必须输入密码</message> 
        </field-validator> 
        <field-validator type="regex"> 
            <param name="expression"><![CDATA[(\w{4,25})]]></param> 
            <message>您输入的密码只能是字母和数组,且长度必须在4到25之间</message> 
        </field-validator> 
    </field> 
    <field name="age"> 
        <field-validator type="int"> 
            <param name="min">1</param> 
            <param name="max">150</param> 
            <message>年龄必须在1到150之间</message> 
        </field-validator> 
    </field> 
      
    <field name="email"> 
        <field-validator type="email"> 
            <message>输入正确的email地址</message> 
        </field-validator> 
          
        <field-validator type="requiredstring"> 
            <param name="trim">true</param> 
            <message>必须输入邮箱</message> 
        </field-validator> 
    </field> 
</validators> 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
<field name="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>必须输入名字</message>
</field-validator>
<field-validator type="regex">
<param name="expression"><![CDATA[(\w{4,25})]]></param>
<message>您输入的用户名只能是字母和数组,且长度必须在4到25之间</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>必须输入密码</message>
</field-validator>
<field-validator type="regex">
<param name="expression"><![CDATA[(\w{4,25})]]></param>
<message>您输入的密码只能是字母和数组,且长度必须在4到25之间</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>年龄必须在1到150之间</message>
</field-validator>
    </field>

<field name="email">
<field-validator type="email">
<message>输入正确的email地址</message>
</field-validator>

<field-validator type="requiredstring">
<param name="trim">true</param>
<message>必须输入邮箱</message>
</field-validator>
</field>
</validators>
4.struts.xml



Xml代码
<action name="validate" class="com.lxitedu.validate.Action"> 
            <result name="input">/validate.jsp</result> 
            <result name="success">/validate.jsp</result> 
</action> 

<action name="validate" class="com.lxitedu.validate.Action">
            <result name="input">/validate.jsp</result>
            <result name="success">/validate.jsp</result>
</action> 5.validate.jsp



Html代码
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>Insert title here</title> 
</head> 
<body> 
    <s:form action="abc/validate.action" > 
        <s:textfield name="name" label="姓名"  /> 
        <s:textfield name="password" label="密码"/>   
        <s:textfield name="age" label="年龄"/>   
        <s:textfield name="email" label="邮箱"/>   
        <s:submit/> 
    </s:form>   
</body> 
</html> 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
    <s:form action="abc/validate.action" >
        <s:textfield name="name" label="姓名"  />
        <s:textfield name="password" label="密码"/>
        <s:textfield name="age" label="年龄"/>
        <s:textfield name="email" label="邮箱"/>
        <s:submit/>
    </s:form>
</body>
</html>

OK.这就是struts2的一个最最简单的验证过程。。。

你可能感兴趣的:(html,jsp,xml,struts)