Struts2 验证数组

2022-01-16 00:00:00 validation arrays collections java 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 个文本区域供用户输入数字.我希望所有字段都是必需的.

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 个具有相同名称数字"的文本区域,所以我将在此类中获得的数字"将是一个长度为 10 的整数数组.当我不使用下面的验证时,我可以轻松获得用户输入的数字(即 number[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.

谢谢

推荐答案

你不太可能重用这个验证器,所以只需在操作中使用 validate:

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.");
           }
        }
    }
}

然后使用 <s:actionerror/> 在 jsp 中显示字段错误,或者将上面的内容重写为专门命名字段(带索引),在这种情况下您可以使用 ,您将使用 addFieldError 方法.有关这些标签的详细信息,请参阅 http://struts.apache.org/2.3.1.2/docs/tag-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

相关文章