1

问题是当我尝试通过 struts 验证来验证字段时。它不起作用....我可以在tomcat上看到错误消息。

我在这里发布我的代码,

注册.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(动作类)

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>

例外

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".
4

1 回答 1

3

不缩进代码和 XML 是一个可怕的想法;这是不可能的考虑。

错误信息:

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

所以。第 30 行附近有什么<param>元素吗?

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

哦,看,那个元素没有name属性。<param>

此外,您的execute()方法是可疑的,不清楚您的操作属性是否有公共设置器。

于 2013-08-17T18:27:45.410 回答