سؤال

I have a form with few text boxes. The text box values are initialized to default values using customerSer action.

I can change the default values and submit the form using customer action. 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.

I need to do struts2 validation for the form elements. Is there any other approch can be taken for form population of data or validation?

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>

Link to pre populate data 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. 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.

نصائح أخرى

You can also use @SkipValidation annotation, on the method you don't want, the validation to apply.

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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top