Wednesday, January 1, 2014

Spring MVC with transactions using Data Source Transaction manager

studentReg.jsp
----------------
<%--
    Document   : studentReg
    Created on : Jan 1, 2014, 1:07:25 PM
    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>
        <form action="studentReg.spring" method="post">
           <table>
               <tr>
                   <td>StudentName:</td>
                   <td><input type="text" name="sname"/></td>
               </tr>
               <tr>
                   <td>City</td>
                   <td><input type="text" name="scity"/></td>
               </tr>
               <tr>
                   <td colspan="2"><input type="submit"/></td>
               </tr>
           </table>
        </form>
    </body>
</html>

StudentController.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.controllers;

import org.jl.services.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 *
 * @author Rishitha
 */
@Controller
public class StudentController {
    @Autowired
    StudentService regService;
    @Autowired
    DataSourceTransactionManager ptm;
    @RequestMapping("/studentReg.spring")
   
        String saveStudentRegDetails(StudentTo st){
        TransactionStatus td = ptm.getTransaction(new DefaultTransactionDefinition());
       
        try{
            //This calls dao and tries
            //to insert student details in to 2 tables
            //student
            regService.registerStudent(st);
        }catch(Exception e ){
            ptm.rollback(td);
            return "failure";
        }
        ptm.commit(td);
        return "success";
       
    }
   
}


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.controllers;

/**
 *
 * @author Rishitha
 */
public class StudentTo {
    private String sname;
    private String scity;
   
    public StudentTo(){
       
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getScity() {
        return scity;
    }

    public void setScity(String scity) {
        this.scity = scity;
    }
   
   
   
}


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.controllers.StudentTo;

/**
 *
 * @author Rishitha
 */
public interface StudentDAOI {
   
    public void saveStudentDet(StudentTo st );
    public void saveStudentDetCopy(StudentTo st );
   
}

StudentJDBCDAO.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.controllers.StudentTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Rishitha
 */
@Repository
public class StudentJDBCDAO implements StudentDAOI{
    @Autowired
    private JdbcTemplate jt;
    public void saveStudentDet(StudentTo st) {
        jt.execute("INSERT INTO studentdet values('"+st.getSname()+"','"+st.getScity()+"')");
       
    }

    public void saveStudentDetCopy(StudentTo st) {
         jt.execute("INSERT INTO studentdetcop values('"+st.getSname()+"','"+st.getScity()+"')");
    }

 
   
}


StudentService.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.services;

import org.jl.controllers.StudentTo;
import org.jl.dao.StudentDAOI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Rishitha
 */
@Service
public class StudentService {
    @Autowired
    StudentDAOI sdao;
    public void registerStudent(StudentTo st ){
        sdao.saveStudentDet(st);
        sdao.saveStudentDetCopy(st);
       
       
       
    }
    public StudentService(){
       
    }
    public StudentDAOI getSdao() {
        return sdao;
    }

    public void setSdao(StudentDAOI sdao) {
        this.sdao = sdao;
    }
   
   
   
}

dispatcher-servlet.xml in WEB-INF

<?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="ds" 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 name="jt" class="org.springframework.jdbc.core.JdbcTemplate" c:_0-ref="ds">
     </bean>
  
     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
     <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/"
          p:suffix=".jsp" />
     <bean id="ptm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
            c:_0-ref="ds">
         
     </bean>
         
    <context:component-scan base-package="org.jl"/>

</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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <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>

No comments:

Post a Comment