开发者

Server Side Validation using Spring Web MVC validator class

I am developing app using Spring Web MVC, Hibernate..

Now, i have my login page configuration like :

<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"/>

Now, after submitting from login page control goes to Validator Clas开发者_Python百科s i.e. LoginValidator..

But, at that class i am not getting reference of my userSecurityProcessor which gives me server side data..

Can anybody please suggest, how can i check Username and Password in my Validator class..?

Thanks in advance..


Create an instance of LoginValidator as a separate bean. In your LoginValidator, add a userSecurityProcessor property. Then in your config file inject a reference to an instance of a IUserSecurityProcessor just as you do above. So something like this:

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

Then change

<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"/>

to

<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"/>


You can' get userSecurityProcessor in Validator. The only object you will get is, the commandObject. In your case it is User

But in case you need that, then you will have to bind it in Validator

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

Now, bind this validator with your Form.


public void validate(Object target, Errors errors) { User user = (User) target; String username = user.getUsername(); String password = user.getPassword(); .......... }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜