Followers

spring application

web.xml
======

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <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>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

   
    <context:component-scan base-package="myspring" />
   
<import resource="services.xml" />

</beans>

services.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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
   
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" /> 
        <property name="url" value="jdbc:derby://localhost:1527/batch" /> 
        <property name="username" value="app" /> 
        <property name="password" value="app" />
   
    </bean>

    <bean id="dao" class="myspring.TransactionDaoImpl"> 
    <property name="dataSource" ref="dataSource"></property> 
    </bean> 
    
   
</beans>

/admin/index.jsp
===========

<a href="getReport.htm">Click here to get the Report</a><br />
<a href="admin/login.htm">Home Page</a>

index.jsp
======

<body>
    <a href="admin/getReport.htm">Click here to get the Report</a> <br /><a href="send.htm"><img src="Myshop.JPG" width="1000" height="400"</a> <br/>
    <a href="login.htm">Home Page</a>
   
</body>

report.jsp
=======

<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="myspring.Transaction"%>
<body>
   
    <table border="1" >
        <tr><td>ID</td><td>Sender</td><td>SenderAddress</td><td>SenderCity</td><td>Receiver</td><td>ReceiverAddress</td><td>ReceiverCity</td><td>Status</td></tr>
            
            <% 
            List<Transaction> myList = (List<Transaction>)request.getAttribute("transaction");
            Iterator<Transaction> it = myList.iterator();
            while(it.hasNext()){
            Transaction tr = it.next();
            out.println("<tr><td>"+tr.getId()+"</td><td>"+tr.getSender()+"</td><td>"+tr.getSenderAddress()+"</td><td>"+tr.getSenderCity()+"</td><td>"+tr.getReceiver()+"</td><td>"+tr.getReceiverAddress()+"</td><td>"+tr.getReceiverCity()+"</td><td>"+tr.getStatus()+"</td></tr>");
            }
            %>
                           
    </table>
   
   
</body>

send.jsp
======

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>


    <body>
        <h1>Spring MVC </h1> <a href="login.htm">Home Page</a> <br />
        <form:form method ="post" commandName="transaction" action="send.htm" >
            <table border="0" >
                <tr>
                    <td> Sender </td>
                    <td> Receiver </td>
                </tr>
                <tr>
                    <td> <form:input path="sender" maxlength="20" /></td>
                    <td> <form:input path="receiver" maxlength="20" /></td>
                </tr>
                <tr>
                    <td> <form:textarea path="senderAddress" maxlength="20" /></td>
                    <td> <form:textarea path="receiverAddress" maxlength="20" /></td>
                </tr>
                 <tr>
                    <td>
                 <form:select path="senderCity" >
                     <form:option value="bangalore" >bangalore</form:option>
                     <form:option value="chennai" >chennai</form:option>
                     <form:option value="delhi" >delhi</form:option>
                 </form:select>
                 </td>
                 <td>
                 <form:select path="receiverCity" >
                     <form:option value="bangalore" >bangalore</form:option>
                     <form:option value="chennai" >chennai</form:option>
                     <form:option value="delhi" >delhi</form:option>
                 </form:select>
                 </td>
                </tr>  
            </table>
           <input type="submit"  value="ACCEPT" />
</form:form>
    </body>


success.jsp
=======

<body>
    Transaction is successful please  <a href="send.htm">click</a> to make a transaction.<br />
    <a href="login.htm">Home Page</a>  
   
</body>


CourierController.java
==============

package myspring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/send.htm")
public class CourierController {
    @Autowired
    TransactionDao dao;

    public TransactionDao getDao() {
        return dao;
    }

    public void setDao(TransactionDao dao) {
        this.dao = dao;
    }
    @RequestMapping(method = RequestMethod.GET)   
    public String showForm(ModelMap m){
    Transaction t = new Transaction();
    m.addAttribute("transaction", t); 
   
    return "/send.jsp";
    //return "/newjsp.jsp";
    }
   
    @RequestMapping(method = RequestMethod.POST)   
    public String processForm(@ModelAttribute("transaction") Transaction t){
   
    dao.insert(t);
   
    return "/success.jsp";
    }   
}




LoginController.java
=============

package myspring;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {
   
    @RequestMapping({"/login.htm","/admin/login.htm"})
    public String homePage(){
   
    return "/index.jsp";
    }   
}

MyRowMapper.java
============

package myspring;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;

public class MyRowMapper implements RowMapper<Transaction>{

    @Override
    public Transaction mapRow(ResultSet rs, int i) throws SQLException {
    Transaction tr = new Transaction();
    tr.setId(rs.getInt(1));
    tr.setSender(rs.getString(2));
    tr.setSenderAddress(rs.getString(3));
    tr.setSenderCity(rs.getString(4));
    tr.setReceiver(rs.getString(5));
    tr.setReceiverAddress(rs.getString(6));
    tr.setReceiverCity(rs.getString(7));
    tr.setStatus(rs.getString(8));
   
    return tr;
    }   
}


ReportController.java
==============

package myspring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/admin/getReport.htm")
public class ReportController {
    @Autowired
    TransactionDao dao;
   
    @RequestMapping(method = RequestMethod.GET)
    public String getReport(ModelMap m){
        m.addAttribute("transaction",dao.getRecords());
   
    return "/report.jsp";
    }
   
}

Transaction.java
===========

package myspring;

public class Transaction {
    private int id;
    private String sender;
    private String senderAddress;
    private String senderCity;
    private String receiver;
    private String receiverAddress;
    private String receiverCity;
    private String status;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getSenderAddress() {
        return senderAddress;
    }

    public void setSenderAddress(String senderAddress) {
        this.senderAddress = senderAddress;
    }

    public String getSenderCity() {
        return senderCity;
    }

    public void setSenderCity(String senderCity) {
        this.senderCity = senderCity;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getReceiverAddress() {
        return receiverAddress;
    }

    public void setReceiverAddress(String receiverAddress) {
        this.receiverAddress = receiverAddress;
    }

    public String getReceiverCity() {
        return receiverCity;
    }

    public void setReceiverCity(String receiverCity) {
        this.receiverCity = receiverCity;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

TransactionDao.java
=============
package myspring;

import java.util.List;

public interface TransactionDao {
    public void insert(Transaction t);
    public List<Transaction> getRecords();
   
}

TransactionDaoImpl.java
================

package myspring;

import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class TransactionDaoImpl implements TransactionDao{
    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
   

    @Override
    public void insert(Transaction t) {
       
    JdbcTemplate template = new JdbcTemplate(dataSource);
    int rowCount = template.queryForInt("select max(ID) from TRX");
    String sender = t.getSender();
    String senderAddress = t.getSenderAddress();
    String senderCity = t.getSenderCity();
    String receiver = t.getReceiver();
    String receiverAddress = t.getReceiverAddress();
    String receiverCity = t.getReceiverCity();
   
    String query = "insert into TRX values("+(rowCount+1)+",'"+sender+"','"+senderAddress+"','"+senderCity+"','"+receiver+"','"+receiverAddress+"','"+receiverCity+"','Not-Dispatched')";
    template.execute(query);
   
    }

    @Override
    public List<Transaction> getRecords() {
    List<Transaction> myList = new ArrayList<Transaction>();
        JdbcTemplate template = new JdbcTemplate(dataSource);
        String sql = "select * from TRX";
        myList =  template.query(sql, new MyRowMapper());       
        return myList;
   
    }   

}






No comments:

Post a Comment