GTU Advance Java Practical- 8
Write an application to keep record and retrieve record of student. The record includes student id, enrollment number, semester, SPI. Use MVC arechitecture.
Index.jsp
				
					<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>INDEX PAGE</title>
</head>
<body>
<a href="load.html">INSERT</a>
<br />
<a href="search.html">SEARCH</a>
<br />
</body>
</html>

				
			
spring-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"/><!-- base package name which contains con,dao,vo packages -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" /><!--exclude index page all other jsp pages are
include in WEB-INF folder-->
<property name="suffix" value=".jsp" /><!--extension of those pages-->
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/prac10"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.model.RegVO</value><!--vo file destination-->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>spring</servlet-name><!--servlet name should be same as per file name-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name><!--servlet name should be same as above-->
<url-pattern>*.html</url-pattern><!--we can add mutliple url pattern-->
</servlet-mapping>
</web-app>

				
			
Registration.jsp
				
					<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>REGISTRATION PAGE</title>
</head>
<%@taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
<body>
<f:form action="insert.html" post="Post" modelAttribute="RegVO">
<table border="1px solid black">
<tr>
<td>STUDENT_ID:</td>
<td><f:input path="studentID" /></td>
</tr>
<tr>
<td>ENROLLMENT_NUMBER:</td>
<td> <f:input path="enrollment" /></td>
</tr>
<tr>
<td>SEMESTER:</td>
<td><f:input path="semester" /></td>
</tr>
<tr>
<td>SPI:</td>
<td> <f:input path="SPI" /></td>
</tr>
<tr>
<td> <f:hidden path="id" /></td>
</tr>
<tr>
<td> <input type="submit" value="Submit"></td>
</tr>
</table>
</f:form>
</body>
</html>
Search.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<body>
<table border="1px solid black">
<tr>
<th>Id</th>
<th>STUDENT_ID</th>
<th>ENROLLMENT_NUMBER</th>
<th>SEMESTER</th>
<th>SPI</th>
<th>ACTION</th>
</tr>
<c:forEach items="${SearchList}" var="p">
<tr>
<td>${p.id}</td>
<td>${p.studentID}</td>
<td>${p.enrollment}</td>
<td>${p.semester}</td>
<td>${p.SPI}</td>
<td colspan="2"><a href="delete.html?id=${p.id}">Delete</a>&nbsp;&nbsp;<a
href="edit.html?id=${p.id}">Edit</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
				
			
RegController.java
				
					package com.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.dao.RegDAO;
import com.model.RegVO;
@Controller
public class RegController {
  @Autowired
  RegDAO regDAO;
  @RequestMapping(value = "load.html", method = RequestMethod.GET)
  public ModelAndView Load() {
    return new ModelAndView("Registration", "RegVO", new RegVO());
  }
  @RequestMapping(value = "insert.html", method = RequestMethod.POST)
  public ModelAndView Insert(@ModelAttribute RegVO regVo) {
    regDAO.insert(regVo);
    return new ModelAndView("redirect:/search.html");
  }
  @RequestMapping(value = "search.html", method = RequestMethod.GET)
  public ModelAndView Search() {
    List searchList = regDAO.search();
    return new ModelAndView("Search", "SearchList", searchList);
  }
  @RequestMapping(value = "delete.html", method = RequestMethod.GET)
  public ModelAndView Delete(HttpServletRequest request, @ModelAttribute RegVO regVO,
    @RequestParam int id) {
    regVO.setId(id);
    regDAO.delete(regVO);
    return new ModelAndView("redirect:/search.html");
  }
  @RequestMapping(value = "edit.html", method = RequestMethod.GET)
  public ModelAndView Edit(HttpServletRequest request, @ModelAttribute RegVO regVO,
    @RequestParam int id) {
    regVO.setId(id);
    List editList = regDAO.edit(regVO);
    return new ModelAndView("Registration", "RegVO", editList.get(0));
  }
}
				
			
RegDAO.java
				
					package com.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.model.RegVO;
@Repository
public class RegDAO {
  @Autowired
  SessionFactory sessionFactory;
  public void insert(RegVO regVO) {
    try {
      Session session = sessionFactory.openSession();
      Transaction transaction = session.beginTransaction();
      session.saveOrUpdate(regVO);
      transaction.commit();
      session.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public List search() {
    Session session = sessionFactory.openSession();
    Query q = session.createQuery("from RegVO");
    List ls = q.list();
    session.close();
    return ls;
  }
  public void delete(RegVO regVO) {
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    session.delete(regVO);
    transaction.commit();
    session.close();
  }
  public List edit(RegVO regVO) {
    Session session = sessionFactory.openSession();
    Query q = session.createQuery("from RegVO where id=" + regVO.getId());
    List ls = q.list();
    session.close();
    return ls;
  }
}
				
			
RegVO.java
				
					package com.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Spring")
public class RegVO {
  @Id
  @Column(name = "Id")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int id;
  @Column(name = "StudentID")
  private String studentID;
  @Column(name = "Enrollment")
  private String enrollment;
  @Column(name = "Semester")
  private String semester;
  @Column(name = "SPI")
  private String spi;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getStudentID() {
    return studentID;
  }
  public void setStudentID(String studentID) {
    this.studentID = studentID;
  }
  public String getEnrollment() {
    return enrollment;
  }
  public void setEnrollment(String enrollment) {
    this.enrollment = enrollment;
  }
  public String getSemester() {
    return semester;
  }
  public void setSemester(String semester) {
    this.semester = semester;
  }
  public String getSPI() {
    return spi;
  }
  public void setSPI(String spi) {
    this.spi = spi;
  }
}