Login application, using Spring MVC

diggFurldel.icio.usFacebook

This article builds on ??. We will be using Spring MVC to create a simple form. The data filled in the form will be sent to the model. The model ??. We will not do form validation.

The spring tag library.
  • Get the spring.tld from spring download and put it in \WEB-INF\tlds.
  • Update the web.xml to use this tld

\WEB-INF\web.xml

...
    <taglib>
        <taglib-uri>/spring</taglib-uri>
        <taglib-location>/WEB-INF/spring.tld</taglib-location>
    </taglib>
...
The view.
Command object.
  • The command object is bound to the input tag in the form on the front end.
  • The command object is passed to the validator.
  • If the validator passes the command object it is passed to the controller.
  • If the validator does not pass the command object, ??.
package com.check.spring.mvc;
 
public class LoginCredentials {
    private String Username;
    private String Password;
    public String getPassword() {
        return Password;
    }
    public void setPassword(String password) {
        Password = password;
    }
    public String getUsername() {
        return Username;
    }
    public void setUsername(String username) {
        Username = username;
    } 
}
Validator object.
  • Implements the Validator object of spring framework.
  • Validator class gets control after the submit on the form.
  • A validate() method of the object is called with the command object and errors object as parameter.
  • What does this supports() do ??
package com.check.spring.mvc;
 
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
 
public class LoginCredentialsValidator implements Validator {
 
    LoginCredentials loginCredentials;
 
    @Override
    public boolean supports(Class clazz) {
        return LoginCredentials.class.isAssignableFrom(clazz);
    }
 
    @Override
    public void validate(Object commandObject, Errors errors) {
 
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName",
                "Field is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
                "Field is required.");
 
        loginCredentials = (LoginCredentials) commandObject;
 
        if ((loginCredentials.getPassword() != "password")&&
            (loginCredentials.getUsername() != "partha")){
            errors.reject("Credentials provided are not correct.");
        }
    }
}

\WEB-INF\dispatcher-servlet.xml

...
    <bean id="LoginCredentialsValidator"
        class="com.check.spring.mvc.LoginCredentialsValidator" />
...

LoginFormController

  • Extends the SimpleFormController class.
  • The onSubmit method gets control.
  • What is the formBackingObject?? doing?

Step1 : Getting a useless Login form shown in the webpage.

Create the basic JSP.

  • It does not do anything.
  • There is no action associated with it.
  • CheckSpringMVC101\WEB-INF\jsp\loginForm.jsp
<html>
<head><title>Login please.</title></head>
<body>
<table>
<tr><td>Username:</td><td><input type="text" name="username" /></td></tr>
<tr><td>Password:</td><td><input type="password" name="password" /></td></tr>
<tr><td colspan=2><input type="submit" /></td></tr>
</table>
</body>
</html>

Create an almost empty SimpleFormController.

  • CheckSpringMVC101\WEB-INF\src\com\check\spring\mvc\login\LoginFormController.java
package com.check.spring.mvc.login;
 
import org.springframework.web.servlet.mvc.SimpleFormController;
 
public class LoginFormController extends SimpleFormController {
}

Create a POJO command object.

  • There is simply nothing here.
  • However the command object is required for the SimpleFormController.
  • CheckSpringMVC101\WEB-INF\src\com\check\spring\mvc\login\LoginCredentials.java
package com.check.spring.mvc.login;
 
public class LoginCredentials {
}

Tie the view, controller and the command object.

  • CheckSpringMVC101\WEB-INF\dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd">
 
<beans>
 
    <bean id="Login"
        class="com.check.spring.mvc.login.LoginFormController">
        <property name="sessionForm">
            <value>true</value>
        </property>
        <property name="commandName">
            <value>credentials</value>
        </property>
        <property name="commandClass">
            <value>com.check.spring.mvc.login.LoginCredentials</value>
        </property>
        <property name="formView">
            <value>/WEB-INF/jsp/loginForm.jsp</value>
        </property>
    </bean>
 
    <bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/login.do">
                    <ref bean="Login" />
                </entry>
            </map>
        </property>
    </bean>
 
</beans>

Run the application.

Step 2: Create command object, bind that with the view and receive data in controller.

Get the spring tld.

  • Fetch the spring.tld from Spring download and put it in CheckSpringMVC101\WEB-INF\tlds.
  • Update the web.xml to use the tld.
  • CheckSpringMVC101\WEB-INF\web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
 'http://java.sun.com/dtd/web-app_2_3.dtd'>
 
<web-app>
 
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
 
    <taglib>
        <taglib-uri>/spring</taglib-uri>
        <taglib-location>/WEB-INF/tlds/spring.tld</taglib-location>
    </taglib>
 
</web-app>

Tie the view with command object.

  • Import the spring taglib.
  • Create a form in the JSP with method post.
  • Use the <spring:bind> tag to bind input elements inside form to the command object properties.
  • CheckSpringMVC101\WEB-INF\jsp\loginForm.jsp
<%@ taglib prefix="spring" uri="/spring" %>
 
<html>
 
<head><title>Login please.</title></head>
<body>
<form method="post">
<table>
<tr><td>Username:</td>
<spring:bind path="credentials.username">
<td><input type="text" name="username" /></td>
</spring:bind>
</tr>
<tr><td>Password:</td>
<spring:bind path="credentials.password">
<td><input type="password" name="password" /></td>
</spring:bind>
</tr>
<tr><td colspan=2><input type="submit" /></td></tr>
</table>
</form>
</body>
</html>

Update the command object to be able to handle form data.

  • Create the POJO like methods and variables.
  • CheckSpringMVC101\WEB-INF\src\com\check\spring\mvc\login\LoginCredentials.java
package com.check.spring.mvc.login;
 
public class LoginCredentials {
    private String username; 
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    } 
}

Update the controller to read data from the command object.

  • Override the onSubmit method to get access to the command object.
  • At this point we are simply reading the attributes of the command object and not doing anything with them.
  • CheckSpringMVC101\WEB-INF\src\com\check\spring\mvc\login\LoginFormController.java
package com.check.spring.mvc.login;
 
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
 
public class LoginFormController extends SimpleFormController {
 
    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
        LoginCredentials credentials = (LoginCredentials) command;
        System.out.println("Username: " + credentials.getUsername());
        System.out.println("Password: " + credentials.getPassword());
        return super.onSubmit(command);
    }
}

Run the application.

Step 3: Create a model using the command object and then surface the data in view.

Create a model in controller.

Tie together the view and the model.

Create a view which can fetch data from the controller.

Run the application.

diggFurldel.icio.usFacebook

Page tags: spring
page_revision: 20, last_edited: 1197043515|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License