2013-08-17 46 views
1

問題是當我嘗試通過struts驗證驗證字段時。它dosent工作....我可以看到在tomcat上的錯誤消息。驗證在struts2中無法正常工作

我張貼在這裏我的代碼,

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類)

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 [email protected]</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". 
+0

[如何在struts 2中使用超鏈接傳遞動作名稱?](http://stackoverflow.com/questions/18268758/how-to-pass-an-action-name-using-hyperlink-in -struts-2) –

+0

可能有insertdata表單的字段名稱可能與InsertAction-validation.xml中的字段名稱不同 – xrcwrn

回答

3

不縮進代碼和XML是一種可怕的想法;這是不可能想到的。

錯誤消息:

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

大二你是否有圍繞第30行的任何<param>元素?

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

哦,看,還有對於<param>元素沒有name屬性。

此外,您的​​方法是可疑的,並且不清楚您的操作屬性是否有公共setter。

+0

「另外,你的execute()方法是可疑的」:我大聲笑出來:D –