Struts2验证

时间:2010-09-27 16:55:30

标签: struts2 validation xml-validation

在Struts2中,我们是否可以同时拥有验证xml(ActionClass-method-validation.xml)和在Action类中实现的验证方法。

我问的是,在我的情况下,这个验证xml不会被调用。

2 个答案:

答案 0 :(得分:1)

希望您已创建正确的规则文件actionname-validation.xml(注意 - actionname区分大小写)并将其保存在与Action类相同的包中。

此外,如果您没有配置默认拦截器,请在该特定操作标记内的Struts.xml中添加验证拦截器。

<action name="welcome"  class="com.abc.web.WelcomeAction">
<interceptor-ref name="validation" />
<result name="success" >hello.jsp</result>
</action>

请参阅有关验证框架的更多详细信息 http://struts.apache.org/2.0.14/docs/validation.htmlhttp://java.dzone.com/articles/struts2-tutorial-part-37

答案 1 :(得分:0)

<?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>

只需检查一下是否正确