Struts2的校验一个数组 [英] Struts2 Validation for an array

查看:149
本文介绍了Struts2的校验一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:如何验证一个数组中的元素

Question: how to validate the elements of an array?

我想编写一个简单的应用程序,它要求用户使用struts2的输入10个号码。

I want to write a simple application that asks a user to enter 10 numbers using struts2.

enter.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>    

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Enter 10 numbers!</title>
</head>
<body>
<h3>Please enter 10 numbers</h3>
<s:form action="next.action" method="post" validate="true">
    <s:iterator var="i" begin="0" end="9">
        <s:label value="Number %{#i+1}"/>
        <s:textfield name="number" key="label.number" size="20"/>   
    </s:iterator>
    <s:submit method="execute" key="label.next" align="center" />
</s:form>
</body>
</html>

我用了一个迭代器生成10 textarea的为用户输入数字。我想需要的所有字段。

I used a iterator to generate 10 textarea for the user to enter the numbers. And I want all the fields to be required.

NextAction.java

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;

public class NextAction extends ActionSupport{



    private Integer[] number;

    public Integer[] getNumber() {
        return number;
    }
    public void setNumber(Integer[] number) {
        this.number = number;
    }


    public String execute(){
        return "success";
    }

}

本类有唯一的属性是数字。需要注意的是,因为我生成10 textarea的具有相同的名称号码,号码我会在这个类得到的将是长度为10的整数数组,如果我不使用下面的验证,我可以很容易地得到数字用户输入(即数[I]),并且在另一个JSP后显示出来。

The only property this class has is number. Note that because I generated 10 textarea with the same name "number", the "number" I'll get in this class will be an Integer array of length 10. And when I am not using the validation below, I can easily get the numbers the user entered (i.e. number[i]), and display them after in another jsp.

的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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Number</display-name>
  <welcome-file-list>
    <welcome-file>enter.jsp</welcome-file>
  </welcome-file-list>

    <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

的struts.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>

 <constant name="struts.custom.i18n.resources" value="ApplicationResources" />

    <package name="default" extends="struts-default" namespace="/">
        <action name="forward" class="NextAction">
            <result name="success">success.jsp</result>
            result name="input">enter.jsp</result>

        </action>
    </package>
</struts>

NextAction-validation.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="number">   <!-- The field 'number' here is actually holding an array -->
    <field-validator type="required">
        <message key="errors.required"/>
    </field-validator>
    <field-validator type="int">
        <param name="min">1</param>
    <param name="max">100</param>
        <message key="errors.number"/>
    </field-validator>
</field>

</validators>

但是,当我加入这个验证,因为该领域数字是数组,那么此验证将无法工作。(如果只有一个文本区域命名为数字有,此验证会一直很好。但我们有10个)

But when I added this validation, because the field "number" is array, then this validation will not work.(if there were only one textarea named 'number', this validation would have been fine. But we have 10 )

我的问题是如何验证数组的每个元素,这是我们从提交的表单得到什么?希望我的问题是清楚的。

My question is how to validate each element of the array, which we get from the submitted form? Hope my question is clear.

感谢您

推荐答案

这是不可能的,你要重用这个验证程序,所以只用动作中验证:

It is unlikely you are going to reuse this validator, so just use validate within the action:

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;

public class NextAction extends ActionSupport{
    private Integer[] number;

    public Integer[] getNumber() {
        return number;
    }
    public void setNumber(Integer[] number) {
        this.number = number;
    }
    //Following is default behaviour so it is not worth writing
    //public String execute(){
    //    return "success";
    //}

    //add validation in action (_not tested_)
    public void validate(){
        if (number.length > 10){
          this.addActionError("Error: More than ten numbers supplied.");
        }else if (number.length < 10){
          this.addActionError("Error: Less than ten numbers supplied.");
        }
        for (int i = 0; i < number.length; i++){
           if(number[i] < 0){
             this.addActionError("Error: Number " + (i + 1) + " is less than zero.");
           }else if(number[i] > 100){
             this.addActionError("Error: Number " + (i + 1) + " is greater than 100.");
           }
        }
    }
}

然后用&LT显示在jsp现场错误; S:actionerror /&GT; 或者把上面的具体命名字段(索引)在这种情况下,你可以使用,你会使用addFieldError方法。有关这些标记的详细信息,请参见 http://struts.apache.org/2.3 1.2 /文档/标签的reference.html

Then display the field errors in the jsp with <s:actionerror /> or rewrite the above to specifically name fields (with indexes) in which case you can use , and you'll use the addFieldError method. For details on these tags see http://struts.apache.org/2.3.1.2/docs/tag-reference.html

这篇关于Struts2的校验一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆