0

我有一个带有几个文本框的表单。使用操作将文本框值初始化为默认值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>

测试动作.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>

客户.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>
4

2 回答 2

1

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

于 2013-10-25T08:08:33.923 回答
0

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

参考

class MyAction extends ActionSupport{

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

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

}
于 2013-10-27T20:06:42.230 回答