简体   繁体   English

使用带有验证的struts2错误预填充表单元素

[英]Pre populate form element using struts2 error with validation

I have a form with few text boxes. 我有一个很少有文本框的表格。 The text box values are initialized to default values using customerSer action. 使用customerSer操作将文本框值初始化为默认值。

I can change the default values and submit the form using customer action. 我可以更改默认值,并使用customer操作提交表单。 Both the action belongs to the same class with different methods used for each action. 两个动作都属于同一类,每个动作使用不同的方法。

This works fine as long as i do not have any validation for the form. 只要我对表单没有任何验证,这就可以正常工作。 Once form validation is applied, the first action does not work and gives error and looks for result type input . 一旦应用了表单验证,第一个操作将不起作用,并给出错误并寻找结果类型input

I need to do struts2 validation for the form elements. 我需要对表单元素进行struts2验证。 Is there any other approch can be taken for form population of data or validation? 是否可以采用其他方法来填充数据或进行验证?

Struts.xml 在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 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 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 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>

Link to pre populate data customer.jsp 链接到预填充数据customer.jsp

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

When your validation xml file is named like this TestAction-validation.xml that means that validation process will happen for all actions in TestAction class. 当您的验证xml文件这样命名为TestAction-validation.xml ,这意味着将对TestAction类中的所有操作进行验证过程。 So your validation is configured per action class, in order to configure it per action name as it is given in the struts.xml file you need to name your validation file like that TestAction-customer-validation.xml . 因此,将根据操作类对验证进行配置,为了根据struts.xml文件中给出的操作名称对验证进行配置,您需要将验证文件命名为TestAction-customer-validation.xml

You can also use @SkipValidation annotation, on the method you don't want, the validation to apply. 您也可以在不需要的方法上使用@SkipValidation批注,以应用验证。

Reference 参考

class MyAction extends ActionSupport{

 @SkipValidation
 public String view(){       //validation will NOT be applied here
   ...
 }

 public String save(){           //validation will be applied here
  ...
 }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM