使用struts2错误预填充表单元素并进行验证

时间:2013-10-25 07:21:12

标签: java jsp validation struts2 struts2-jquery

我的表单中包含很少的文本框。使用 customerSer 操作将文本框值初始化为默认值。

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

只要我没有对表单进行任何验证,这都可以正常工作。应用表单验证后,第一个操作不起作用并提供错误并查找结果类型input

我需要对表单元素进行struts2验证。是否有任何其他approch可用于表格数据或验证?

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>

2 个答案:

答案 0 :(得分:1)

当您的验证xml文件命名为此TestAction-validation.xml时,表示将对TestAction类中的所有操作进行验证过程。因此,您的验证是按操作类配置的,以便根据struts.xml文件中的操作名称对其进行配置,您需要将验证文件命名为TestAction-customer-validation.xml

答案 1 :(得分:0)

您还可以对您不想要的方法使用@SkipValidation批注,并应用验证。

Reference

class MyAction extends ActionSupport{

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

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

}