1

Problem is when I try to validate fields through struts validation. It dosent work .... I can see error message on tomcat.

I post my code here,

Register.jsp

  <s:form action="insertdata">
  <s:textfield name="username" key="label.uname" required="true"/>
  <s:password name="password" key="label.pass" required="true"/>
  <s:password name="cpassword" key="label.cpass" required="true"/>
  <s:textfield name="emailid" key="label.mail" required="true"/>
  <s:textfield name="mobileno" key="label.mobileno" required="true"/>
  <s:datetimepicker name="dob" key="label.dob" displayFormat="MM/dd/yy" required="true"/>
  <s:submit value="Register"/>
</s:form>

struts.xml

<action name="insertdata" class="com.curd.action.InsertAction">
  <result name="success">RegisterSuccessful.jsp</result>
  <result name="error">NotRegister.jsp</result>
</action>

InsertAction.java(Action Class)

public class InsertAction {
    private String username,password,cpassword,emailid;
    private int mobileno;
    private Date dob;   
    DAO dao=new DAO();
    public String execute(){
        return "success";
    else
        return "error";
    }

InsertAction-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators
  PUBLIC '-//OpenSymphony Group//XWork Validator 1.0.2//EN'
  'http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd'>
<validators>
  <field name="username">
    <field-validator type="requiredstring">
      <message>username is required</message>
    </field-validator>
  </field>

  <field name="password">
    <field-validator type="requiredstring">
      <message>Password is required</message>
    </field-validator>
  </field>

  <field name="cpassword">
    <field-validator type="requiredstring">
      <message>confirm password is required</message>
    </field-validator>
  </field>

  <field name="emailid">
    <field-validator type="requiredstring">
      <message>Email ID is required</message>
    </field-validator>
    <field-validator type="mail">
      <message>Enter Valid Email id eg.john@gmail.com</message>
    </field-validator>
  </field>

  <field name="mobileno">
    <field-validator type="stringLength">
      <param minLength="10"/>
      <message>Mobile No Must be ${minLength} Digit..</message>
    </field-validator>
  </field>

  <field name="dob">
    <field-validator type="date">
      <param name="min">2000/01/1</param>
      <param name="max">2013/09/1</param>
      <message>Year should be With in ${min} and ${max}</message>
    </field-validator>
  </field>
</validators>

Exception

SEVERE: Validation error for username:username is required Aug 17, 2013 10:43:38 PM com.opensymphony.xwork2.validator.DelegatingValidatorContext$LoggingValidationAware addFieldError
SEVERE: Validation error for password:Password is required Aug 17, 2013 10:43:38 PM com.opensymphony.xwork2.validator.DelegatingValidatorContext$LoggingValidationAware addFieldError
SEVERE: Validation error for cpassword:confirm password is required Aug 17, 2013 10:48:29 PM com.opensymphony.xwork2.util.DomHelper$StartHandler error SEVERE: Attribute "name" is required and must be specified for element type "param".at (null:30:25)org.xml.sax.SAXParseException;systemId:file:///E:/Language%20SW/KRISHSOFT/eclipse/com/curd/action/InsertAction-validation.xml; lineNumber: 30; columnNumber: 25;Attribute "name" is required and must be specified for element type "param".
Krish
  • 304
  • 3
  • 5
  • 19
  • possible duplicate of [How to pass an action name using hyperlink in struts 2?](http://stackoverflow.com/questions/18268758/how-to-pass-an-action-name-using-hyperlink-in-struts-2) – Roman C Aug 17 '13 at 18:49
  • There may be insertdata form's field name may not be same on InsertAction-validation.xml – xrcwrn Aug 18 '13 at 03:23

1 Answers1

3

Not indenting code and XML is a horrible idea; it's impossible to think about.

The error message:

SEVERE: Attribute "name" is required and must be specified for element type "param".

So. Do you have any <param> elements around line 30?

<param minLength="10"></param>

Oh look, there's no name attribute for that <param> element.

Also, your execute() method is suspicious, and it's not clear if you have public setters for your action properties.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302