In Struts2, can we have both validation xml (ActionClass-method-validation.xml) and validate method implemented in Action class.
I was asking as this validation xml don'开发者_Go百科t get invoked in my situation.
hope you have created proper rule file actionname-validation.xml (note- actionname is case sensitive) and kept it in same package as of Action class.
Also, if you do not have default interceptor configured then please add validation interceptor in Struts.xml within that particular action tag.
<action name="welcome" class="com.abc.web.WelcomeAction">
<interceptor-ref name="validation" />
<result name="success" >hello.jsp</result>
</action>
Refer for more details about Validation framework at http://struts.apache.org/2.0.14/docs/validation.html and http://java.dzone.com/articles/struts2-tutorial-part-37
<?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 = "firstname">
<field-validator type = "stringlength">
<param name = "minlength">3</param>
<param name ="maxlength">18</param>
<message>Please Enter Your First Name</message>
</field-validator>
</field>
<field name = "lastname">
<field-validator type = "required">
<message>Please Enter Your Last Name</message>
</field-validator>
</field>
<field name = "mobilenumber">
<field-validator type = "int">
<message>Please Enter Your Mobile Number</message>
</field-validator>
</field>
<field name = "dob">
<field-validator type = "date">
<message>Please Enter Your Date Of Birth</message>
</field-validator>
</field>
<field name = "emailid">
<field-validator type = "email">
<message>Please Enter Email ID Correctly</message>
</field-validator>
</field>
<field name = "hometown">
<field-validator type = "required">
<message>Please Enter Your Home Town</message>
</field-validator>
</field>
<field name = "currentcity">
<field-validator type = "required">
<message>Please Enter Your Current City</message>
</field-validator>
</field>
<field name = "username">
<field-validator type = "stringlength">
<param name = "minlength">3</param>
<param name ="maxlength">18</param>
<message>Please Enter Your User Name</message>
</field-validator>
</field>
<field name = "password">
<field-validator type = "stringlength">
<param name = "minlength">8</param>
<message>Password must Contain minimum of 8 Characters</message>
</field-validator>
</field>
<validator type = "expression">
<param name = "expression">password.equals(password1)</param>
<meaasage>Passwords Does Not Match</message>
</validator>
</validators>
Just go through it and check whether its correct
精彩评论