Spring MVC : Login App Part 2

Now let's try some validation for our dummy project. Use back the same project from the previous post. Remember the validation that we used for our Struts before? (Hope u do :P) Its somewhat same as this one.

First of all, get the spring-form.tld file from the spring.jar file. Extract it and you should be able to see that file residing inside the META-INF folder. Create a folder under WEB-INF in your project, name it as tld. Copy the tld file and paste it inside the tld folder. If you want to know more about the file, visit this. So your directory should be somewhat like below.


Let's update our web.xml file. Add the following code :

<taglib>

<taglib-uri>/spring</taglib-uri>

<taglib-location>/WEB-INF/tld/spring-form.tld</taglib-location>

</taglib>


Next we'll update our bean file and our controller and we'll also add a new file for vaildation. Let's start off with our LoginFormBean.java. This is our bean file, as you all know bean file consists of getters and setters. Since this is a login application, we'll add getters and setters for username and password. So create some functions as below :


public class LoginFormBean {

String name;

String password;
public String getName() {

return name;

}
public void setName(String name) {

this.name = name;

}
public String getPassword() {

return password;

}
public void setPassword(String password) {

this.password = password;

}

}



Now add these lines to LoginFormController.java under onSubmit function. System.out.println is optional :P

LoginFormBean loginBean = (LoginFormBean) command;

System.out.println("Username: " + loginBean.getName());

System.out.println("Password: " + loginBean.getPassword());

return new ModelAndView("success"); //success here is the success.jsp

Now for our validator file. Create LoginFormValidator.java under Validator package. Add this code. Make sure you implement Validator. (LoginFormValidator implements Validator)

LoginFormBean loginBean;
@Override

public boolean supports(Class clazz) {

return clazz.equals(LoginFormBean.class);

}
@Override

public void validate(Object commandObject, Errors errors) {
loginBean = (LoginFormBean) commandObject;

ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name", "Name is required.");

ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password", "Password is required.");

if (!errors.hasFieldErrors("name") && !errors.hasFieldErrors("password")) {

if ((loginBean.getPassword().equals("password") == false) && (loginBean.getName().equals("admin") == false)) {

errors.rejectValue("name", "name", "Incorrect Login Details!");

}

}

}

The validator class has 2 functions, supports() and validate() method. supports() is used to check whether the validator supports the class and validate() is used to validate the object of the supported class. rejectValue() takes in few arguments, in this example, it takes 3 arguments. First argument is for the bean property, second is the key value of the error message that exists in a resource file (here Ive ommited the resource file) so for this just keep it the same as the bean property, and third argument is the default error message in case the resource file is not found. (Resource file is the *.properties file)

Next we're going to update our index.jsp page. Add this line right before the <html> tag. This is to use the tags from spring-form.tld.

<%@ taglib prefix="spring" uri="/spring" %>

The uri "spring" is the same as the one in <taglib> inside web.xml. Make sure the name is same :P

Start using the tags we have imported in the index.jsp. Its as follows :

<spring:form method="post" action="" commandName="login">

<p>

User name :

<spring:input path="name"/>

</p>

<p>

Password :

<spring:password path="password" />

</p>

<p>

<spring:errors cssClass="error" path="*"/><br>

</p>

<p>

<p>
<spring:errors cssClass="error" path="*"/><br>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</spring:form>

and under the head tag we add the following css :


<style>
.error { color: red; }
</style>

cssClass="error" is referring to that css above. For path="*" , the * is a wildcard. Its referring to all the path (bean property) tag from the jsp . You can even specify one by one like this :

<spring:errors cssClass="error" path="name"/>

<spring:errors cssClass="error" path="password"/>

commandName="login" here is the same as the commandName specified in the dispatcher-servlet.xml.

Next we'll update our dispatcher-servlet.xml file. Since we have created a validator class, we'll make the validator being referred as well. Under the bean id = "loginForm" add the following line :

<property name="validator"><ref bean="loginValidator"/></property>

Create a new bean id for the loginValidator ref bean above.

<bean id="loginValidator" class="Validator.LoginFormValidator" />

Now try to run the application. You should be able to get the errors if its entered wrongly or submitting without entering anything.

0 comments: