Friday, January 3, 2014

SPRING MVC Hibernate Integration Login Example

dispatcher-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:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

" >

  

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
   
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        p:driverClassName="oracle.jdbc.driver.OracleDriver"
        p:url="jdbc:oracle:thin:@localhost:1521:XE"
        p:username="hr" p:password="hr" />
   
 
      <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
      <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
     
      <bean id="sf" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
          <property name="dataSource" ref="dataSource"/>
          <property name="annotatedClasses">
            <list>
               <value>org.jl.vo.StudentTo</value>
            </list>
           </property>
         <property name="hibernateProperties">
              <props>
                 <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                 <prop key="hibernate.show_sql">true</prop>
                 
                   <prop key="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</prop>
                   <prop key="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</prop>
                   <prop key="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</prop>
                   <prop key="hibernate.connection.username">hr</prop>
                      <prop key="hibernate.connection.password">hr</prop>  
                 
             </props>
           </property>
         
      </bean>
      <bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate"
          c:_0-ref="sf"/>

      <bean class="org.jl.dao.StudentHibDAO" p:hibernateTemplate-ref="ht"/>
     
     

<context:component-scan base-package="org.jl"></context:component-scan>
</beans>

web.xml
----------

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       
         <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
         </init-param>
         <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

failure.jsp

<%--
    Document   : failure
    Created on : Jan 3, 2014, 9:07:37 AM
    Author     : Rishitha
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Failure</h1>
    </body>
</html>

loginForm.jsp

<%--
    Document   : loginForm
    Created on : Jan 2, 2014, 9:44:08 PM
    Author     : Rishitha
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
     
        <script src="jquery-1.8.0.min.js"></script>
        <script>
           
            $(document).ready(function(){
                $("#submit").click(function(){
                     var uname = $("#uname").val();
                     var password = $("#password").val();
                     if(uname == null || password == null ){
                         alert("Username/Password is missing ");
                         return false;
                     }else{
                         document.forms.form[0].submit();
                     }
                   
                });
            });
           
        </script>
        <script type="text/javascript" language="javascript">// <![CDATA[
function checkAll()
{
  var checkboxes = new Array();
  checkboxes = document.getElementsByTagName('input');
  alert(checkboxes);
  for (var i=0; i<checkboxes.length; i++)  {
    if (checkboxes[i].type == 'checkbox')   {
      checkboxes[i].checked = true;
    }
  }
}
</script>
       
    </head>
    <body>
       
        <form name="checkform" action="login.spring">
           
            <table>
               
                <tr>
                    <td>Username:</td>
                    <td><input type="text" name="uname" id="uname"/>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" id="password" name="password"/>
                </tr>
                <tr>
                    <td colspan="1"><input type="submit" id="submit"/></td>
                </tr>
               
               
               
            </table>
           
           
           
           
        </form>
       
       
       
       
    </body>
</html>


success.jsp

<%--
    Document   : success
    Created on : Jan 4, 2014, 12:22:52 AM
    Author     : Rishitha
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World Success SPRING MVC Plus Hibernate</h1>
    </body>
</html>

StudentDAOI.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jl.dao;

import org.jl.vo.StudentTo;

/**
 *
 * @author Rishitha
 */
public interface StudentDAOI {
   
    StudentTo getStudent(StudentTo st );
       
   
   
}

StudentHibDAO.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jl.dao;

import java.util.List;
import org.jl.vo.StudentTo;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Rishitha
 */

public class StudentHibDAO extends HibernateDaoSupport implements StudentDAOI{

   
    public StudentTo getStudent(StudentTo st) {
       
       List stList = getHibernateTemplate().find("FROM StudentTo st WHERE st.uname=? AND "+
                "st.password=?",new Object[]{st.getUname(),st.getPassword()});
        //getHibernateTemplate().f
           if(stList != null & stList.size() > 0)    
      return (StudentTo)stList.get(0);
           else
               return null;
   
    }
   
}

LoginController.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jl.spring.controllers;

import org.jl.dao.StudentDAOI;
import org.jl.vo.StudentTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Rishitha
 */
@Controller
public class LoginController {
   
    @Autowired
    StudentDAOI stdao;
    @RequestMapping("/login.spring")
    public ModelAndView getStudent(StudentTo st ){
        System.out.println("IN controller ");
        ModelAndView mv = new ModelAndView("success");
        StudentTo stdb = stdao.getStudent(st);
        if(stdb !=null && stdb.getFname() != null ){
            mv.addObject("student",stdb);
            return mv;
        }
       
        return new ModelAndView("failure");
    }
   
   
}


StudentTo.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jl.vo;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 *
 * @author Rishitha
 */
@Entity
@Table(name="jlearners")
public class StudentTo {
   @Id
   private Long id;
   private String uname;
   private String password;
   private String fname;
   private String lname;
  
   public StudentTo(){
      
   }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }
  
  
  
}

Create a table jlearners with the following columns

ID,FNAME,LNAME,PASSWORD,UNAME



No comments:

Post a Comment