使用Spring Web MVC验证器类进行服务器端验证

时间:2009-11-03 07:20:10

标签: java spring spring-mvc

我正在使用Spring Web MVC开发应用程序,Hibernate ..

现在,我的登录页面配置如下:

<bean name="/uservalidate.htm" class="UserValidateFormController">
        <property name="sessionForm" value="true"/>
        <property name="commandName" value="User"/>
        <property name="commandClass" value="User"/>
        <property name="formView" value="login"/>
        <property name="userSecurityProcessor" ref="IUserSecurityProcessor"/>
         <property name="validator">
            <bean class="LoginValidator"/>
        </property>
        <property name="successView" value="layout.jsp"/>

现在,在从登录页面控制提交后转到Validator Class,即LoginValidator ..

但是,在那个班级我没有得到我的userSecurityProcessor的引用,它给我服务器端数据..

有人可以建议,我如何在Validator类中查看用户名和密码..?

提前致谢..

3 个答案:

答案 0 :(得分:2)

将LoginValidator的实例创建为单独的bean。在LoginValidator中,添加userSecurityProcessor属性。然后在您的配置文件中注入对IUserSecurityProcessor实例的引用,就像您上面一样。所以像这样:

<bean name="loginValidator" class="LoginValidator">
<property name="userSecurityProcessor" ref="IUserSecurityProcessor"/>
</bean>

然后改变

<bean name="/uservalidate.htm" class="UserValidateFormController">
    <property name="sessionForm" value="true"/>
    <property name="commandName" value="User"/>
    <property name="commandClass" value="User"/>
    <property name="formView" value="login"/>
    <property name="userSecurityProcessor" ref="IUserSecurityProcessor"/>
     <property name="validator">
        <bean class="LoginValidator"/>
    </property>
    <property name="successView" value="layout.jsp"/>

<bean name="/uservalidate.htm" class="UserValidateFormController">
    <property name="sessionForm" value="true"/>
    <property name="commandName" value="User"/>
    <property name="commandClass" value="User"/>
    <property name="formView" value="login"/>
    <property name="userSecurityProcessor" ref="IUserSecurityProcessor"/>
     <property name="validator" ref = "loginValidator"/>
    <property name="successView" value="layout.jsp"/>

答案 1 :(得分:1)

您可以在Validator中获取userSecurityProcessor。您将获得的唯一对象是commandObject。在您的情况下,它是User

但是如果您需要,那么您必须将它绑定在Validator

<bean id="myValidator" class="LoginValidator">
    <property name="userSecurityProcessor" ref="IUserSecurityProcessor"/>
</bean>

现在,将此验证器与您的表单绑定。

答案 2 :(得分:-1)

public void validate(对象目标,错误错误){      用户用户=(用户)目标;      String username = user.getUsername();      String password = user.getPassword();      ..........     }