• <bdo id='GtayX'></bdo><ul id='GtayX'></ul>
  • <tfoot id='GtayX'></tfoot>
        <i id='GtayX'><tr id='GtayX'><dt id='GtayX'><q id='GtayX'><span id='GtayX'><b id='GtayX'><form id='GtayX'><ins id='GtayX'></ins><ul id='GtayX'></ul><sub id='GtayX'></sub></form><legend id='GtayX'></legend><bdo id='GtayX'><pre id='GtayX'><center id='GtayX'></center></pre></bdo></b><th id='GtayX'></th></span></q></dt></tr></i><div id='GtayX'><tfoot id='GtayX'></tfoot><dl id='GtayX'><fieldset id='GtayX'></fieldset></dl></div>

        <legend id='GtayX'><style id='GtayX'><dir id='GtayX'><q id='GtayX'></q></dir></style></legend>

        <small id='GtayX'></small><noframes id='GtayX'>

        Struts2 验证数组

        Struts2 Validation for an array(Struts2 验证数组)
      1. <tfoot id='VM6ib'></tfoot>

          1. <i id='VM6ib'><tr id='VM6ib'><dt id='VM6ib'><q id='VM6ib'><span id='VM6ib'><b id='VM6ib'><form id='VM6ib'><ins id='VM6ib'></ins><ul id='VM6ib'></ul><sub id='VM6ib'></sub></form><legend id='VM6ib'></legend><bdo id='VM6ib'><pre id='VM6ib'><center id='VM6ib'></center></pre></bdo></b><th id='VM6ib'></th></span></q></dt></tr></i><div id='VM6ib'><tfoot id='VM6ib'></tfoot><dl id='VM6ib'><fieldset id='VM6ib'></fieldset></dl></div>

            <small id='VM6ib'></small><noframes id='VM6ib'>

            • <bdo id='VM6ib'></bdo><ul id='VM6ib'></ul>

                  <legend id='VM6ib'><style id='VM6ib'><dir id='VM6ib'><q id='VM6ib'></q></dir></style></legend>
                    <tbody id='VM6ib'></tbody>
                • 本文介绍了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";
                      /
                                  
                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                  Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                  About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                  What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                  Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                  Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)

                    <tbody id='R5i7B'></tbody>

                  <small id='R5i7B'></small><noframes id='R5i7B'>

                  <legend id='R5i7B'><style id='R5i7B'><dir id='R5i7B'><q id='R5i7B'></q></dir></style></legend>

                      • <bdo id='R5i7B'></bdo><ul id='R5i7B'></ul>
                          <tfoot id='R5i7B'></tfoot>
                          <i id='R5i7B'><tr id='R5i7B'><dt id='R5i7B'><q id='R5i7B'><span id='R5i7B'><b id='R5i7B'><form id='R5i7B'><ins id='R5i7B'></ins><ul id='R5i7B'></ul><sub id='R5i7B'></sub></form><legend id='R5i7B'></legend><bdo id='R5i7B'><pre id='R5i7B'><center id='R5i7B'></center></pre></bdo></b><th id='R5i7B'></th></span></q></dt></tr></i><div id='R5i7B'><tfoot id='R5i7B'></tfoot><dl id='R5i7B'><fieldset id='R5i7B'></fieldset></dl></div>