【问题标题】:Pre populate form element using struts2 error with validation使用带有验证的 struts2 错误预填充表单元素
【发布时间】:2024-04-22 08:55:01
【问题描述】:

我有一个带有几个文本框的表单。使用 customerSer 操作将文本框值初始化为默认值。

我可以更改默认值并使用 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>

预填充数据 customer.jsp 的链接

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

【问题讨论】:

  • 将验证文件名更改为TestAction-customer-validation.xml
  • 它的工作。你能告诉我这次修改后有什么不同吗?

标签: java jsp validation struts2 struts2-jquery


【解决方案1】:

当您的验证 xml 文件命名为 TestAction-validation.xml 时,这意味着验证过程将对 TestAction 类中的所有操作进行。因此,您的验证是针对每个操作类进行配置的,为了按照struts.xml 文件中给出的操作名称对其进行配置,您需要将验证文件命名为TestAction-customer-validation.xml

【讨论】:

    【解决方案2】:

    你也可以使用@SkipValidation注解,在你不想要的方法上,应用验证。

    Reference

    class MyAction extends ActionSupport{
    
     @SkipValidation
     public String view(){       //validation will NOT be applied here
       ...
     }
    
     public String save(){           //validation will be applied here
      ...
     }
    
    }
    

    【讨论】:

      最近更新 更多