Friday, June 10, 2011

Demo Login example in Spring 3

Sample of Login application in Spring 3 with Annotation, Spring Annotation Example
Spring 3 support very good approach of annotation, you don't need to define each and every controller in xml. We can directly define name of the controller in the controller. Annotation has been used to define url in the controller.
Here is the sample of  login application in spring 3.
Application Name: Spring 3
For login Sample, we required following files:
index.jsp
Welcome to Main Page
<a href="LoginForm.bp">Login</a> 

Spring3-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan
        base-package="com.bp.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>


LoginForm.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

Login Form

User Name:
Password:

LoginController.java
package com.bp.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.bp.controller.domain.User;
 
@Controller
@RequestMapping("LoginForm.bp")//this mean all  url must be */User...
public class HelloWordController {
 
 
  @RequestMapping(method = RequestMethod.GET)
     public String showForm(Map model) 
  {
             User loginForm = new User();
             loginForm.setUsername("Biren");             
             model.put("loginForm", loginForm);
             return "LoginForm";
     }
  
  @RequestMapping(method = RequestMethod.POST)
     public String processForm(User loginForm, BindingResult result,Map model) 
  {
             String userName = "a";
             String password = "a";
             if (result.hasErrors()) {
                     return "LoginForm";
             }
             System.out.println(loginForm.getUsername());
           //  loginForm = (User) model.get("loginForm");
             System.out.println(loginForm.getUsername());
             if (!loginForm.getUsername().equals(userName)
                             || !loginForm.getPassword().equals(password)) {
              
             
              model.put("loginForm", loginForm);
                     return "LoginForm";
             }
             model.put("loginForm", loginForm);
             return "LoginSuccess";
     }
 
}

User.java
package com.bp.controller.domain;


public class User {

 String username;
 String password;
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 
}

No comments:

Post a Comment