Spring3 MVC Hibernate Demo
Project Name: "SpringMVCHibernate"
WEB-INF\web.xml
====================
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>Spring Example</display-name>
<servlet>
<servlet-name>basicspring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>basicspring</servlet-name>
<url-pattern>*.obj</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
WEB-INF\basicspring-servlet.xml
===============================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-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.xyz.controller" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>messages</value>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/qa_db" />
<property name="username" value="root" />
<property name="password" value="root1" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="itemService" class="com.xyz.service.ItemServiceImpl">
<property name="itemDao"><ref local="itemDao" /></property>
</bean>
<bean id="itemDao" class="com.xyz.dao.ItemDaoImpl">
<property name="sessionFactory"><ref local="sessionFactory"/> </property>
</bean>
</beans>
src\hibernate.cfg.xml
======================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/qa_db</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root1</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource = "Item.hbm.xml"/>
</session-factory>
</hibernate-configuration>
src\Item.hbm.xml
=================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.xyz.dto">
<class name="Item" table="Item_800890">
<meta attribute="class-description">
This class contains the item details.
</meta>
<id name="itemCode" type="int" column="item_code">
<generator class="assigned"/>
</id>
<property name="itemName" type="string" column="item_name" not-null="true" />
</class>
</hibernate-mapping>
src\com\xyz\controller\ItemController.java
===============================
package com.xyz.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.xyz.dto.Item;
import com.xyz.service.ItemService;
/**
* @author Pedababu M
*
*/
@Controller
@RequestMapping("item")
public class ItemController {
@Autowired
private ItemService itemService;
/**
* @return the itemService
*/
public ItemService getItemService() {
return itemService;
}
/**
* @param itemService the itemService to set
*/
public void setItemService(ItemService itemService) {
this.itemService = itemService;
}
@RequestMapping("/itemhomepage")
public ModelAndView itemHomePage(){
Map myModel = new HashMap();
return new ModelAndView("itemhomepage");
}
@RequestMapping("/searchitempage")
public ModelAndView searchItemPage(){
Map myModel = new HashMap();
return new ModelAndView("searchitempage");
}
@RequestMapping("/searchitem")
public ModelAndView searchItem(@RequestParam("itemCode") String itemCode){
System.out.println("itemCode" + itemCode);
Map myModel = new HashMap();
Item item = getItemService().getItem(Integer.parseInt(itemCode));
if (item == null) {
myModel.put("itemCode", itemCode);
return new ModelAndView("invaliditem","model",myModel);
} else {
myModel.put("item", item);
return new ModelAndView("item","model",myModel);
}
}
@RequestMapping("/additempage")
public ModelAndView addItemPage(){
Map myModel = new HashMap();
return new ModelAndView("additempage");
}
@RequestMapping("/additem")
public ModelAndView addItem(@RequestParam("itemCode") String itemCode,@RequestParam("itemName") String itemName){
System.out.println("itemCode:::" + itemCode + " itemName:::" + itemName);
Map myModel = new HashMap();
Item item = new Item();
item.setItemCode(Integer.parseInt(itemCode));
item.setItemName(itemName);
int insertedItemCode = getItemService().additeam(item);
myModel.put("insertedItemCode", insertedItemCode);
return new ModelAndView("insertitem","model",myModel);
}
@RequestMapping("/deleteitempage")
public ModelAndView deleteItemPage(){
Map myModel = new HashMap();
return new ModelAndView("deleteitempage");
}
@RequestMapping("/deleteitem")
public ModelAndView deleteItem(@RequestParam("itemCode") String itemCode){
System.out.println("itemCode:::" + itemCode);
Map myModel = new HashMap();
getItemService().deleteItem(Integer.parseInt(itemCode));
myModel.put("deletedItemCode", itemCode);
return new ModelAndView("deleteitem","model",myModel);
}
}
src\com\xyz\service\ItemService.java
===================================
package com.xyz.service;
import com.xyz.dto.Item;
/**
* @author Pedababu M
*
*/
public interface ItemService {
int additeam(Item item);
Item getItem(int itemCode);
Item loadItem(int itemCode);
void deleteItem(int itemCode);
}
src\com\xyz\service\ItemServiceImpl.java
===================================
package com.xyz.service;
import com.xyz.dao.ItemDao;
import com.xyz.dto.Item;
/**
* @author Pedababu M
*
*/
public class ItemServiceImpl implements ItemService {
private ItemDao itemDao;
/**
* @return the itemDao
*/
public ItemDao getItemDao() {
return itemDao;
}
/**
* @param itemDao the itemDao to set
*/
public void setItemDao(ItemDao itemDao) {
this.itemDao = itemDao;
System.out.println("setItemDao22222222222"+this.itemDao);
}
/**
* @param item
* @return int
*/
public int additeam(Item item) {
int itemCode = itemDao.additeam(item);
return itemCode;
}
/**
* @param itemCode
* @return
*/
public Item getItem(int itemCode) {
return itemDao.getItem(itemCode);
}
/**
* @param itemCode
* @return
*/
public Item loadItem(int itemCode) {
return itemDao.getItem(itemCode);
}
public void deleteItem(int itemCode) {
itemDao.deleteItem(itemCode);
}
}
src\com\xyz\dao\ItemDao.java
=============================
package com.xyz.dao;
import com.xyz.dto.Item;
/**
* @author Pedababu M
*
*/
public interface ItemDao {
int additeam(Item item);
Item getItem(int itemCode);
Item loadItem(int itemCode);
void deleteItem(int itemCode);
}
src\com\xyz\dao\ItemDaoImpl.java
==================================
package com.xyz.dao;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import com.xyz.dto.Item;
/**
* @author Pedababu M
*
*/
public class ItemDaoImpl implements ItemDao {
private Session session = null;
Transaction transaction = null;
private SessionFactory sessionFactory = null;
/**
* @return the sessionFactory
*/
public SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* @param sessionFactory the sessionFactory to set
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
System.out.println("sessionFactory7777777777777777777777777777777777777777777777777777777777 :" + this.sessionFactory);
}
Item item = null;
/**
* @param item
* @return
*/
public int additeam(Item item) {
Integer itemCode = 0;
try {
System.out.println("Before addItem - Dao start");
session = getSessionFactory().openSession();
transaction = session.beginTransaction();
System.out.println("Before addItem - Dao");
itemCode = (Integer) session.save(item);
//item.setItemName("itemName");
transaction.commit();
} catch (HibernateException hibernateException) {
transaction.rollback();
System.out.println(hibernateException.getMessage());
} finally {
//session.close();
}
return itemCode;
}
public Item getItem(int itemCode) {
try {
session = getSessionFactory().openSession();
transaction = session.beginTransaction();
item = (Item) session.get(Item.class,itemCode);
transaction.commit();
} catch (HibernateException hibernateException) {
transaction.rollback();
System.out.println(hibernateException.getMessage());
} finally {
session.close();
}
return item;
}
public Item loadItem(int itemCode) {
Item item = null;
try {
session = getSessionFactory().openSession();
transaction = session.beginTransaction();
item = (Item) session.load(Item.class,itemCode);
transaction.commit();
} catch (HibernateException hibernateException) {
transaction.rollback();
System.out.println(hibernateException.getMessage());
} finally {
session.close();
}
return item;
}
public void deleteItem(int itemCode) {
try {
session = getSessionFactory().openSession();
transaction = session.beginTransaction();
Item item = new Item();
//item.setItemCode(itemCode);
item = (Item) session.load(Item.class,itemCode);
session.delete(item);
transaction.commit();
} catch (HibernateException hibernateException) {
transaction.rollback();
System.out.println(hibernateException.getMessage());
} finally {
session.close();
}
}
}
src\com\xyz\dto\Item.java
===========================
package com.xyz.dto;
import java.io.Serializable;
import org.hibernate.CallbackException;
import org.hibernate.Session;
import org.hibernate.classic.Lifecycle;
import org.hibernate.classic.Validatable;
import org.hibernate.classic.ValidationFailure;
/**
* @author Pedababu M
*
*/
public class Item implements Lifecycle, Validatable{
private int itemCode;
private String itemName;
/**
* @return the itemCode
*/
public int getItemCode() {
return itemCode;
}
/**
* @param itemCode the itemCode to set
*/
public void setItemCode(int itemCode) {
this.itemCode = itemCode;
}
/**
* @return the itemName
*/
public String getItemName() {
return itemName;
}
/**
* @param itemName the itemName to set
*/
public void setItemName(String itemName) {
this.itemName = itemName;
}
public Item() {
}
public boolean onDelete(Session arg0) throws CallbackException {
return false;
}
@Override
public void onLoad(Session arg0, Serializable arg1) {
}
@Override
public boolean onSave(Session arg0) throws CallbackException {
System.out.println("Item data is saved");
return false;
}
@Override
public boolean onUpdate(Session arg0) throws CallbackException {
// TODO Auto-generated method stub
return false;
}
public void validate() throws ValidationFailure {
// TODO Auto-generated method stub
System.out.println(itemName + " " + itemName.length());
if (itemName.length()<=0){
throw new ValidationFailure("length must be greater than 0");
}
}
}
additempage.jsp
===============
<%@ include file="include.jsp" %>
<%@ taglib uri="/WEB-INF/spring.tld" prefix="spring" %>
<html>
<body>
<h3>Add Item :</h3>
<form method="post" action="/SpringMVCHibernate/item/additem.obj">
<br>Enter Item Code:
<input type="text" name="itemCode">
<br>Enter Item Code:
<input type="text" name="itemName">
<input type="submit" value="Add Item">
</form>
Click Here to <a href="/SpringMVCHibernate/item/additempage.obj">Add Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/searchitempage.obj">Search Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/deleteitempage.obj">Delete Item</a><br/>
</body>
</html>
deleteitem.jsp
=============
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page session="false" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<!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>
<body>
Deleted Item Code: <c:out value="${model.deletedItemCode}" /><br/>
Click Here to <a href="/SpringMVCHibernate/item/additempage.obj">Add Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/searchitempage.obj">Search Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/deleteitempage.obj">Delete Item</a><br/>
</body>
</html>
deleteitempage.jsp
======================
<%@ include file="include.jsp" %>
<%@ taglib uri="/WEB-INF/spring.tld" prefix="spring" %>
<html>
<body>
<h3>Delete Item:</h3>
<form method="post" action="/SpringMVCHibernate/item/deleteitem.obj">
<br>Enter Item Code:
<input type="text" name="itemCode">
<input type="submit" value="Delete Item">
</form>
Click Here to <a href="/SpringMVCHibernate/item/additempage.obj">Add Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/searchitempage.obj">Search Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/deleteitempage.obj">Delete Item</a><br/>
</body>
</html>
include.jsp
====================
<%@ page session="false" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/fmt.tld" prefix="fmt" %>
index.jsp
=============
<%@ include file="include.jsp" %>
<c:redirect url="/item/itemhomepage.obj" />
insertitem.jsp
==============================
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page session="false" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<!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>
<body>
Inserted Item Code: <c:out value="${model.insertedItemCode}" /><br/>
Click Here to <a href="/SpringMVCHibernate/item/additempage.obj">Add Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/searchitempage.obj">Search Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/deleteitempage.obj">Delete Item</a><br/>
</body>
</html>
invaliditem.jsp
===============================
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page session="false" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<!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>
<body>
Invalid Item Code: <c:out value="${model.itemCode}" /><br/>
Click Here to <a href="/SpringMVCHibernate/item/additempage.obj">Add Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/searchitempage.obj">Search Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/deleteitempage.obj">Delete Item</a><br/>
</body>
</html>
item.jsp
==========================
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page session="false" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<!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>
<body>
<h2>Item Details</h2>
Item Code: <c:out value="${model.item.itemCode}" /><br/>
Item Name: <c:out value="${model.item.itemName}" /><br/>
Click Here to <a href="/SpringMVCHibernate/item/additempage.obj">Add Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/searchitempage.obj">Search Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/deleteitempage.obj">Delete Item</a><br/>
</body>
</html>
itemhomepage.jsp
=============================
<%@ page session="false" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/fmt.tld" prefix="fmt" %>
<html>
<head>
<title>Item Home Page</title>
</head>
<body>
<h3>Item Operations</h3>
Click Here to <a href="/SpringMVCHibernate/item/additempage.obj">Add Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/searchitempage.obj">Search Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/deleteitempage.obj">Delete Item</a><br/>
</body>
</html>
searchitempage.jsp
==========================
<%@ include file="include.jsp" %>
<%@ taglib uri="/WEB-INF/spring.tld" prefix="spring" %>
<html>
<body>
<h3>Item Search:</h3>
<form method="post" action="/SpringMVCHibernate/item/searchitem.obj">
<br>Enter Item Code:
<input type="text" name="itemCode">
<input type="submit" value="Search Item">
</form>
Click Here to <a href="/SpringMVCHibernate/item/additempage.obj">Add Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/searchitempage.obj">Search Item</a><br/>
Click Here to <a href="/SpringMVCHibernate/item/deleteitempage.obj">Delete Item</a><br/>
</body>
</html>
No comments:
Post a Comment