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".
[如何在struts 2中使用超鏈接傳遞動作名稱?](http://stackoverflow.com/questions/18268758/how-to-pass-an-action-name-using-hyperlink-in -struts-2) –
可能有insertdata表單的字段名稱可能與InsertAction-validation.xml中的字段名稱不同 – xrcwrn