2013-10-25 39 views
0

我有一个几乎没有文本框的表单。将文本框值初始化为默认值,使用customerSer操作。使用带验证的struts2错误填充表单元素

我可以更改默认值并提交表格使用customer操作。这两个操作都属于同一个类,每个操作使用不同的方法。

这工作正常,只要我没有任何形式的验证。一旦应用了表单验证,第一个操作不起作用,并给出错误并寻找结果类型input

我需要为表单元素进行struts2验证。有没有其他方法可以用于形成数据或验证数据?

struts.xml中

<action name="customerSer" 
      class="net.test.struts2.action.TestAction" method="initialize"> 
      <result name="none">Customer.jsp</result> 
</action>   

<action name="customer" 
      class="net.test.struts2.action.TestAction"> 
      <result name="success">SuccessCustomer.jsp</result> 
      <result name="input">Customer.jsp</result> 
</action> 

TestAction.java

package net.viralpatel.struts2.action; 

import com.opensymphony.xwork2.ActionSupport; 

    public class TestAction extends ActionSupport { 

     /** 
     * 
     */ 
     private static final long serialVersionUID = 7154564763591606533L; 
     private String name; 
     private Integer age; 
     private String email; 
     private String telephone; 

     public String execute() { 
      return SUCCESS; 
     } 

     public String initialize() { 
      System.out.println("Aditya"); 
      this.name="Aditya"; 
      this.age=28; 
      return NONE; 
     } 

     public String getName() { 
      return name; 
     } 

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

     public Integer getAge() { 
      return age; 
     } 

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

     public String getEmail() { 
      return email; 
     } 

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

     public String getTelephone() { 
      return telephone; 
     } 

     public void setTelephone(String telephone) { 
      this.telephone = telephone; 
     } 
    } 

TestAction-validation.xml中

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" 
    "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> 
<validators> 
    <field name="name"> 
     <field-validator type="requiredstring"> 
      <param name="trim">true</param> 
      <message key="errors.required" /> 
     </field-validator> 
    </field> 
    <field name="age"> 
     <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.range"/> 
     </field-validator> 
    </field> 
    <field name="email"> 
     <field-validator type="requiredstring"> 
      <message key="errors.required" /> 
     </field-validator> 
     <field-validator type="email"> 
      <message key="errors.invalid" /> 
     </field-validator> 
    </field> 
    <field name="telephone"> 
     <field-validator type="requiredstring"> 
      <message key="errors.required" /> 
     </field-validator> 
    </field> 
</validators> 

Customer.jsp

<%@ page contentType="text/html; charset=UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<html> 
<head> 
<title>Customer Form - Struts2 Demo | ViralPatel.net</title> 
</head> 

<body> 
<h2>Customer Form</h2> 

<s:form action="customer.html" method="post" validate="true"> 
    <s:textfield name="name" key="name" size="20" /> 
    <s:textfield name="age" key="age" size="20" /> 
    <s:textfield name="email" key="email" size="20" /> 
    <s:textfield name="telephone" key="telephone" size="20" /> 
    <s:submit key="label.add.customer" align="center" /> 
</s:form> 
</body> 
</html> 

链接到预先当您的验证XML文件被命名为这样TestAction-validation.xml这意味着验证过程中会发生的所有操作填充数据customer.jsp

<body> 
    <h2>Howdy, <s:property value="username" />...!</h2> 
    <s:a href="customerSer.html">Add Customer</s:a> 
</body> 
+1

更改验证文件名'TestAction-客户validation.xml'。 –

+0

它的工作。你能告诉我这个修改后它有什么不同吗? – UserAdi

回答

1

TestAction类。因此,您的验证是针对每个操作类别配置的,为了按照操作名称配置它,如struts.xml文件中给出的那样,您需要命名验证文件,如TestAction-customer-validation.xml

0

您还可以使用@SkipValidation注释,在您不想要的方法上应用验证。

Reference

​​