Blog Archive

This Day in History

Saturday, January 10, 2009

Login App with Spring Framework


Greetings and Warm Welcome to the World of Spring !!!

I generally get enthused only when I see something works right the first time. My intention was to create a simple “Login App” before getting deeper into Spring. I should thank so many hundreds of people who have posted so much material on Spring – without which I would not have been able to write this up. Thanks to Google and all those Spring Authors. 

Now lets start our simple project by defining the requirements. 

Requirements:

  1. A Login page for a user to provide Username and Password
  2. Receive the provided User Credentials
  3. Validate the provided User Credentials
  4. Show Error Message in the Login Screen if the validation fails
  5. Show the Main Page if Validation is successful 

First let us see “what” needs to be created. You can find the “why” for each of these by googling or in books. 

I used Netbeans 6.1 and I hope you too have the same. Eclipse should work fine too – but I not verified that. 

Step-1: under WEB-INF/jsp/ directory Create login.htm – with a form – form field names being “username” and “password

Step-2: Create the Databean for this form. Call it Login.java. Define getter and setter methods for the fields “username” and “password” – these variables are case-sensitive and should be ditto as defined in login.jsp

Step-3: Create Controller for this Form – LoginController.java

Step-4: Create LoginValidator.java

Step-5: Edit web.xml

Step-6: Edit gspringapp-servlet.xml

a.       Define bean = Login

b.      Define bean id = validator

c.        Define bean id = messageSource [For Error Message to be shown in the login page]

Step-7: Create message.properties under /WEB-INF/classes and make an entry - “key-value” pair. The key is the “name” given in “LoginValidator” for RejectValue

Step-8: Save spring.tld under /WEB-INF/jsp directory

Step-9: Create main.jsp and save under WEB-INF/jsp/ directory

###SOURCE FILES###    

###################

Login.java

package com.springapp;

/**
 * This is the Databean POJO
 * This needs to have the getter and setter methods
 * for the form login.htm
 * All the variables should be same as defined in the html form
 * Beware - it is case-sensitive
 * 
 */
public class Login 
{    
    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;
    }
}
###################

LoginFormController.java

package com.springapp;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class LoginFormController extends SimpleFormController
{
    public LoginFormController()
    {
        setCommandClass(Login.class);
        setCommandName("login");
        setFormView("login");
        setSuccessView("main");        
        //setSupportedMethods(String[] {"POST", "GET"}); 
    }
    
    @Override
    public ModelAndView onSubmit(Object command)
    {
        return new ModelAndView(getSuccessView());
    }

}
###################

LoginValidator.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.springapp;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 *
 * @author nrganeshbabu
 */
public class LoginValidator implements Validator {

    public boolean supports(Class dummy) {
        return dummy.equals(Login.class);
    }

    public void validate(Object command, Errors errors) {
        
        Login loggy = (Login)command;
        if((loggy.getUsername()).equals(loggy.getPassword()))
        {
            errors.reject("invalidLogin");
        }       
    }
}

###################

Message.properties

invalidLogin.login=Invalid Credentials-Username And Password Cannot Be Same

###################

Ref: http://book.javanb.com

Hope with the above source, you were able to create a simple Login Application.

Cheers and Good Luck...

Ganesh Babu N R

0 comments:

Post a Comment