struts2中使用xml进行validation验证

在struts中,根据配置的validation.xml文件进行页面输入项目的验证已经众所周知,本文介绍在struts2中两种validation.xml的配置方式。可以根据不同的需要进行不同的配置。


Demo download


以下以login页面输入firstname,lastname,和age为例进行说明。

struts.xml中,成功的话转向成功页面。不成功的话转回到原页面。

struts2中使用xml进行validation验证_第1张图片

LoginAction.java

package struts2.login;


import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import struts2.login.bean.UserBean;


import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction  extends ActionSupport{
    private  UserBean userBean;
    
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        return SUCCESS;
    }

    public UserBean getUserBean() {
        return userBean;
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    @Override
    public void validate() {
        // TODO Auto-generated method stub
        Map map = this.getFieldErrors();
        Set set = map.keySet();

        for (Iterator iter = set.iterator(); iter.hasNext();)
        {
            System.out.println(map.get(iter.next()));
        }
    }
  
    /*http://blog.csdn.net/happyjiahan/article/details/6535793
*/    
}

LoginAction-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">

<validators>

    <field name="userBean.firstname">
        <field-validator type="requiredstring">
            <message>request firstname</message>
        </field-validator>
    </field>
    
    <field name="userBean.lastname">
        <field-validator type="requiredstring">
            <message>request lastname</message>
        </field-validator>
    </field>
    
    <field name="userBean.age">
        <field-validator type="required">
            <message>request integer</message>
        </field-validator>
        <field-validator type="int">
            <param name="min">1</param>
            <param name="max">150</param>
            <message>should between ${min} and ${max}</message>
        </field-validator>
    </field>
</validators> 
UserBean.java

package struts2.login.bean;


public class UserBean {
    private String firstname;
    private String lastname;
    private Integer age;
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Integer getAge() {
        return age;
    }
}
struts2.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="default" extends="struts-default">
        <action name="login" class="struts2.login.LoginAction">
            <result name="success">/success.jsp</result>
            <result name="input">/login.jsp</result>
        </action>
    </package>
    
</struts>
login.jsp

<%@ page contentType="text/html; charset=gb2312" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<s:form action="login" method="post">
<s:textfield name="userBean.firstname" label="firstname"/>
<s:textfield name="userBean.lastname" label="lastname"/>
<s:textfield name="userBean.age" label="age"/>
<s:submit/>
</s:form>
</html>

success.jsp

<%@ page contentType="text/html; charset=gb2312" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
LOGIN SUCCESS
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>vvvvv</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
			org.apache.struts2.dispatcher.FilterDispatcher
		</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

struts2中使用xml进行validation验证_第2张图片


你可能感兴趣的:(struts2中使用xml进行validation验证)