`

SpringMVC3 Hibernate CRUD Sample Application

阅读更多

To learn any web framework starting with a HelloWorld application is a good idea. Once you have some familiarity with the framework configuration, it's better to do a CRUD(Create,Read,Update,Delete) application which covers various aspects of a web framework like Validation, Request URL Mappings, Request Parameter Binding, Pre-populating forms etc.

Now I am going to explain how to write a Simple CRUD application using SpringMVC3, Hibernate and MySQL.
Our Application is ContactsManagement where you can view or search contacts, create new contacts, edit or delete existing contacts.

Step#1: Create the CONTACTS Table

01.CREATE TABLE  CONTACTS
02.(
03.id int(10) unsigned NOT NULL AUTO_INCREMENT,
04.name varchar(45) NOT NULL,
05.address varchar(45) DEFAULT NULL,
06.gender char(1) DEFAULT 'M',
07.dob datetime DEFAULT NULL,
08.email varchar(45) DEFAULT NULL,
09.mobile varchar(15) DEFAULT NULL,
10.phone varchar(15) DEFAULT NULL,
11.PRIMARY KEY (id)
12.);

Step#2: Copy the SpringMVC, Hibernate and their dependent jars into WEB-INF/lib folder.
If you are using Maven you can mention the following dependencies.

001.<dependencies>
002.<dependency>
003.<groupId>junit</groupId>
004.<artifactId>junit</artifactId>
005.<version>4.8.1</version>
006.<type>jar</type>
007.<scope>compile</scope>
008.</dependency>
009.<dependency>
010.<groupId>org.springframework</groupId>
011.<artifactId>spring-web</artifactId>
012.<version>3.0.5.RELEASE</version>
013.<type>jar</type>
014.<scope>compile</scope>
015.</dependency>
016.<dependency>
017.<groupId>org.springframework</groupId>
018.<artifactId>spring-core</artifactId>
019.<version>3.0.5.RELEASE</version>
020.<type>jar</type>
021.<scope>compile</scope>
022.<exclusions>
023.<exclusion>
024.<artifactId>commons-logging</artifactId>
025.<groupId>commons-logging</groupId>
026.</exclusion>
027.</exclusions>
028.</dependency>
029.<dependency>
030.<groupId>log4j</groupId>
031.<artifactId>log4j</artifactId>
032.<version>1.2.14</version>
033.<type>jar</type>
034.<scope>compile</scope>
035.</dependency>
036.<dependency>
037.<groupId>org.springframework</groupId>
038.<artifactId>spring-tx</artifactId>
039.<version>3.0.5.RELEASE</version>
040.<type>jar</type>
041.<scope>compile</scope>
042.</dependency>
043.<dependency>
044.<groupId>jstl</groupId>
045.<artifactId>jstl</artifactId>
046.<version>1.1.2</version>
047.<type>jar</type>
048.<scope>compile</scope>
049.</dependency>
050.<dependency>
051.<groupId>taglibs</groupId>
052.<artifactId>standard</artifactId>
053.<version>1.1.2</version>
054.<type>jar</type>
055.<scope>compile</scope>
056.</dependency>
057.<dependency>
058.<groupId>org.springframework</groupId>
059.<artifactId>spring-webmvc</artifactId>
060.<version>3.0.5.RELEASE</version>
061.<type>jar</type>
062.<scope>compile</scope>
063.</dependency>
064.<dependency>
065.<groupId>org.springframework</groupId>
066.<artifactId>spring-aop</artifactId>
067.<version>3.0.5.RELEASE</version>
068.<type>jar</type>
069.<scope>compile</scope>
070.</dependency>
071.<dependency>
072.<groupId>commons-digester</groupId>
073.<artifactId>commons-digester</artifactId>
074.<version>2.1</version>
075.<type>jar</type>
076.<scope>compile</scope>
077.</dependency>
078.<dependency>
079.<groupId>commons-collections</groupId>
080.<artifactId>commons-collections</artifactId>
081.<version>3.2.1</version>
082.<type>jar</type>
083.<scope>compile</scope>
084.</dependency>
085.<dependency>
086.<groupId>org.hibernate</groupId>
087.<artifactId>hibernate-core</artifactId>
088.<version>3.3.2.GA</version>
089.<type>jar</type>
090.<scope>compile</scope>
091.</dependency>
092.<dependency>
093.<groupId>javax.persistence</groupId>
094.<artifactId>persistence-api</artifactId>
095.<version>1.0</version>
096.<type>jar</type>
097.<scope>compile</scope>
098.</dependency>
099.<dependency>
100.<groupId>c3p0</groupId>
101.<artifactId>c3p0</artifactId>
102.<version>0.9.1.2</version>
103.<type>jar</type>
104.<scope>compile</scope>
105.</dependency>
106.<dependency>
107.<groupId>org.springframework</groupId>
108.<artifactId>spring-orm</artifactId>
109.<version>3.0.5.RELEASE</version>
110.<type>jar</type>
111.<scope>compile</scope>
112.</dependency>
113.<dependency>
114.<groupId>org.slf4j</groupId>
115.<artifactId>slf4j-api</artifactId>
116.<version>1.6.1</version>
117.<type>jar</type>
118.<scope>compile</scope>
119.</dependency>
120.<dependency>
121.<groupId>org.slf4j</groupId>
122.<artifactId>slf4j-log4j12</artifactId>
123.<version>1.6.1</version>
124.<type>jar</type>
125.<scope>compile</scope>
126.</dependency>
127.<dependency>
128.<groupId>cglib</groupId>
129.<artifactId>cglib-nodep</artifactId>
130.<version>2.2</version>
131.<type>jar</type>
132.<scope>compile</scope>
133.</dependency>
134.<dependency>
135.<groupId>org.hibernate</groupId>
136.<artifactId>hibernate-annotations</artifactId>
137.<version>3.4.0.GA</version>
138.<type>jar</type>
139.<scope>compile</scope>
140.</dependency>
141.<dependency>
142.<groupId>jboss</groupId>
143.<artifactId>javassist</artifactId>
144.<version>3.7.ga</version>
145.<type>jar</type>
146.<scope>compile</scope>
147.</dependency>
148.<dependency>
149.<groupId>mysql</groupId>
150.<artifactId>mysql-connector-java</artifactId>
151.<version>5.1.14</version>
152.<type>jar</type>
153.<scope>compile</scope>
154.</dependency>
155.</dependencies>


  
Step#3: Configure SpringMVC
  
 a) Configure DispatcherServlet in web.xml
    

01.<servlet>
02.<servlet-name>dispatcher</servlet-name>
03.<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
04.<load-on-startup>1</load-on-startup>
05.</servlet>
06. 
07.<servlet-mapping>
08.<servlet-name>dispatcher</servlet-name>
09.<url-pattern>*.do</url-pattern>
10.</servlet-mapping>
11. 
12.<listener>
13.<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
14.</listener>
15.<context-param>
16.<param-name>contextConfigLocation</param-name>
17.<param-value>classpath:applicationContext.xml</param-value>
18.</context-param>

   b) Configure View Resolver in WEB-INF/dispatcher-servlet.xml

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

  
c) Configure Annotation support, PropertyPlaceHolderConfigurer, ResourceBundleMessageSource in WEB-INF/classes/applicationContext.xml 
  

01.<context:annotation-config></context:annotation-config>
02. 
03.<context:component-scan base-package="com.sivalabs"></context:component-scan>
04. 
05.<mvc:annotation-driven> </mvc:annotation-driven >
06. 
07.<context:property-placeholder location="classpath:config.properties"></context:property-placeholder>
08. 
09.<bean     id="messageSource"
10.class="org.springframework.context.support.ResourceBundleMessageSource"
11.p:basename="Messages">
12.</bean>

Step#4: Configure JDBC connection parameters and Hibernate properties in config.properties

01.################### JDBC Configuration ##########################
02.jdbc.driverClassName=com.mysql.jdbc.Driver
03.jdbc.url=jdbc:mysql://localhost:3306/sivalabs
04.jdbc.username=root
05.jdbc.password=admin
06. 
07.################### Hibernate Configuration ##########################
08.hibernate.dialect=org.hibernate.dialect.MySQLDialect
09.hibernate.show_sql=true
10.#hibernate.hbm2ddl.auto=update
11.hibernate.generate_statistics=true


    

Step#5: Configure DataSource, SessionFactory, TransactionManagement support in WEB-INF/classes/applicationContext.xml 

01.<bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"
02.p:driverClassName="${jdbc.driverClassName}"
03.p:url="${jdbc.url}"
04.p:username="${jdbc.username}"
05.p:password="${jdbc.password}">
06.</bean>       
07. 
08.<bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
09.<property name="dataSource" ref="dataSource"></property>
10.<property name="hibernateProperties">
11.<props>       
12.<prop key="hibernate.dialect">${hibernate.dialect}</prop>         
13.<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
14.</props>
15.</property>
16.<property name="packagesToScan" value="com.sivalabs"></property>
17.</bean>
18. 
19. 
20.<bean     id="transactionManager"
21.class="org.springframework.orm.hibernate3.HibernateTransactionManager"
22.p:sessionFactory-ref="sessionFactory">
23.</bean>
24. 
25.<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
26. 

  Step#6: Configure the Labels, error messages in WEB-INF/classes/Messages.properties

1.App.Title=SivaLabs
2.typeMismatch.java.util.Date={0} is Invalid Date.
3.dob=DOB

Step#7: Create the Entity class Contact.java

01.package com.sivalabs.contacts;
02. 
03.import java.util.Date;
04. 
05.import javax.persistence.Column;
06.import javax.persistence.Entity;
07.import javax.persistence.GeneratedValue;
08.import javax.persistence.GenerationType;
09.import javax.persistence.Id;
10.import javax.persistence.Table;
11. 
12.import org.apache.commons.lang.builder.ToStringBuilder;
13. 
14.@Entity
15.@Table(name="CONTACTS")
16.public class Contact
17.{
18.@Id
19.@GeneratedValue(strategy = GenerationType.AUTO)
20.private int id;
21.@Column    private String name;
22.@Column private String address;
23.@Column private String gender;
24.@Column private Date dob;
25.@Column private String email;
26.@Column private String mobile;
27.@Column private String phone;
28. 
29.@Override
30.public String toString()
31.{
32.return ToStringBuilder.reflectionToString(this);
33.}
34.//setters & getters   
35.}

Step#8: Create the ContactsDAO.java which performs CRUD operations on CONTACTS table.

01.package com.sivalabs.contacts;
02. 
03.import java.util.List;
04. 
05.import org.hibernate.Criteria;
06.import org.hibernate.SessionFactory;
07.import org.hibernate.criterion.Restrictions;
08.import org.springframework.beans.factory.annotation.Autowired;
09.import org.springframework.stereotype.Repository;
10.import org.springframework.transaction.annotation.Transactional;
11. 
12.@Repository
13.@Transactional
14.public class ContactsDAO
15.{
16.@Autowired
17.private SessionFactory sessionFactory;
18. 
19.public Contact getById(int id)
20.{
21.return (Contact) sessionFactory.getCurrentSession().get(Contact.class, id);
22.}
23. 
24.@SuppressWarnings("unchecked")
25.public List<Contact> searchContacts(String name)
26.{
27.Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Contact.class);
28.criteria.add(Restrictions.ilike("name", name+"%"));
29.return criteria.list();
30.}
31. 
32.@SuppressWarnings("unchecked")
33.public List<Contact> getAllContacts()
34.{
35.Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Contact.class);
36.return criteria.list();
37.}
38. 
39.public int save(Contact contact)
40.{
41.return (Integer) sessionFactory.getCurrentSession().save(contact);
42.}
43. 
44.public void update(Contact contact)
45.{
46.sessionFactory.getCurrentSession().merge(contact);
47.}
48. 
49.public void delete(int id)
50.{
51.Contact c = getById(id);
52.sessionFactory.getCurrentSession().delete(c);
53.}
54.}


Step#9: Create ContactFormValidator.java which performs the validations on saving/updating a contact.

01.package com.sivalabs.contacts;
02. 
03.import org.springframework.stereotype.Component;
04.import org.springframework.validation.Errors;
05.import org.springframework.validation.ValidationUtils;
06.import org.springframework.validation.Validator;
07. 
08.@Component("contactFormValidator")
09.public class ContactFormValidator implements Validator
10.{
11.@SuppressWarnings("unchecked")
12.@Override
13.public boolean supports(Class clazz)
14.{
15.return Contact.class.isAssignableFrom(clazz);
16.}
17. 
18.@Override
19.public void validate(Object model, Errors errors)
20.{
21.ValidationUtils.rejectIfEmptyOrWhitespace(errors,"name","required.name""Name is required.");
22.}
23.}

Step#10: Create ContactsControllers.java which processes all the CRUD requests.

001.package com.sivalabs.contacts;
002. 
003.import java.text.SimpleDateFormat;
004.import java.util.Date;
005.import java.util.List;
006. 
007.import org.springframework.beans.factory.annotation.Autowired;
008.import org.springframework.beans.propertyeditors.CustomDateEditor;
009.import org.springframework.stereotype.Controller;
010.import org.springframework.validation.BindingResult;
011.import org.springframework.web.bind.WebDataBinder;
012.import org.springframework.web.bind.annotation.InitBinder;
013.import org.springframework.web.bind.annotation.ModelAttribute;
014.import org.springframework.web.bind.annotation.RequestMapping;
015.import org.springframework.web.bind.annotation.RequestMethod;
016.import org.springframework.web.bind.annotation.RequestParam;
017.import org.springframework.web.bind.support.SessionStatus;
018.import org.springframework.web.servlet.ModelAndView;
019. 
020.@Controller
021.public class ContactsControllers
022.{
023.@Autowired
024.private ContactsDAO contactsDAO;
025. 
026.@Autowired
027.private ContactFormValidator validator;
028. 
029.@InitBinder
030.public void initBinder(WebDataBinder binder)
031.{
032.SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
033.dateFormat.setLenient(false);
034.binder.registerCustomEditor(Date.classnewCustomDateEditor(dateFormat, true));
035.}
036. 
037.@RequestMapping("/searchContacts")
038.public ModelAndView searchContacts(@RequestParam(required= false, defaultValue="") String name)
039.{
040.ModelAndView mav = new ModelAndView("showContacts");
041.List<Contact> contacts = contactsDAO.searchContacts(name.trim());
042.mav.addObject("SEARCH_CONTACTS_RESULTS_KEY", contacts);
043.return mav;
044.}
045. 
046.@RequestMapping("/viewAllContacts")
047.public ModelAndView getAllContacts()
048.{
049.ModelAndView mav = new ModelAndView("showContacts");
050.List<Contact> contacts = contactsDAO.getAllContacts();
051.mav.addObject("SEARCH_CONTACTS_RESULTS_KEY", contacts);
052.return mav;
053.}
054. 
055.@RequestMapping(value="/saveContact", method=RequestMethod.GET)
056.public ModelAndView newuserForm()
057.{
058.ModelAndView mav = new ModelAndView("newContact");
059.Contact contact = new Contact();
060.mav.getModelMap().put("newContact", contact);
061.return mav;
062.}
063. 
064.@RequestMapping(value="/saveContact", method=RequestMethod.POST)
065.public String create(@ModelAttribute("newContact")Contact contact, BindingResult result, SessionStatus status)
066.{
067.validator.validate(contact, result);
068.if (result.hasErrors())
069.{               
070.return "newContact";
071.}
072.contactsDAO.save(contact);
073.status.setComplete();
074.return "redirect:viewAllContacts.do";
075.}
076. 
077.@RequestMapping(value="/updateContact", method=RequestMethod.GET)
078.public ModelAndView edit(@RequestParam("id")Integer id)
079.{
080.ModelAndView mav = new ModelAndView("editContact");
081.Contact contact = contactsDAO.getById(id);
082.mav.addObject("editContact", contact);
083.return mav;
084.}
085. 
086.@RequestMapping(value="/updateContact", method=RequestMethod.POST)
087.public String update(@ModelAttribute("editContact") Contact contact, BindingResult result, SessionStatus status)
088.{
089.validator.validate(contact, result);
090.if (result.hasErrors()) {
091.return "editContact";
092.}
093.contactsDAO.update(contact);
094.status.setComplete();
095.return "redirect:viewAllContacts.do";
096.}
097. 
098.@RequestMapping("deleteContact")
099.public ModelAndView delete(@RequestParam("id")Integer id)
100.{
101.ModelAndView mav = new ModelAndView("redirect:viewAllContacts.do");
102.contactsDAO.delete(id);
103.return mav;
104.}   
105.}


Step#11: Instead of writing the JSTL tag library declerations in all the JSPs, declare them in one JSP and include that JSP in other JSPs.

 


taglib_includes.jsp

1.<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
2.<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
3. 
4.<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
5.<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>


Step#12: Create the JSPs.

a)showContacts.jsp

01.<%@include file="taglib_includes.jsp" %>
02. 
03.<html>
04.<head>
05. 
06.<title><spring:message code="App.Title"></spring:message> </title>
07.<script type="text/javascript" src="js/contacts.js"></script>
08.</head>
09.<body style="font-family: Arial; font-size:smaller;">
10.<center>
11.<form action="searchContacts.do" method="post">       
12.<table style="border-collapse: collapse;" border="0"bordercolor="#006699" width="500">
13.<tr>
14.<td>Enter Contact Name</td>
15.<td><input type="text" name="name"/>
16.<input type="submit" value="Search"/>
17.<input type="button" value="New Contact"onclick="javascript:go('saveContact.do');"/>
18.</td></tr>
19.</table>
20.</form>
21. 
22.<table style="border-collapse: collapse;" border="1"bordercolor="#006699" width="500">
23.<tr bgcolor="lightblue">
24.<th>Id</th>
25.<th>Name</th>           
26.<th>Address</th>   
27.<th>Mobile</th>
28.<th></th>
29.</tr>
30.<c:if test="${empty SEARCH_CONTACTS_RESULTS_KEY}">
31.<tr>
32.<td colspan="4">No Results found</td>
33.</tr>
34.</c:if>
35.<c:if test="${! empty SEARCH_CONTACTS_RESULTS_KEY}">
36.<c:forEach var="contact"items="${SEARCH_CONTACTS_RESULTS_KEY}">       
37.<tr>
38.<td><c:out value="${contact.id}"></c:out></td>
39.<td><c:out value="${contact.name}"></c:out></td>
40.<td><c:out value="${contact.address}"></c:out> </td>
41.<td><c:out value="${contact.mobile}"></c:out></td>
42.<td>
43.<a href="updateContact.do?id=${contact.id}">Edit</a>
44.<a href="javascript:deleteContact('deleteContact.do?id=${contact.id}');">Delete</a>
45.</td>
46.</tr>
47.</c:forEach>
48.</c:if>               
49.</table>   
50.</center>
51. 
52.</body>
53.</html>


b)newContact.jsp

01.<%@include file="taglib_includes.jsp" %>
02. 
03.<html>
04.<head>
05.<script type="text/javascript" src="js/contacts.js"></script>
06.<title><spring:message code="App.Title"></spring:message> </title>
07.</head>
08.<body style="font-family: Arial; font-size:smaller;">
09. 
10.<table  bgcolor="lightblue" width="750" height="500" align="center"style="border-collapse: collapse;" border="1" bordercolor="#006699" >
11.<tr>
12.<td align="center"><h3>Edit Contact Form</h3></td>
13.</tr>
14.<tr valign="top" align="center">
15.<td align="center">
16.<form:form action="saveContact.do" method="post"commandName="newContact">
17. 
18.<table width="500" style="border-collapse: collapse;"border="0" bordercolor="#006699" cellspacing="2"cellpadding="2">   
19.<tr>
20.<td width="100" align="right">Name</td>
21.<td width="150">
22.<form:input path="name"/></td>
23.<td align="left">
24.<form:errors path="name" cssStyle="color:red"></form:errors>
25.</td>
26.</tr>
27. 
28.<tr>
29.<td width="100" align="right">DOB</td>
30.<td><form:input path="dob"/></td>
31.<td align="left"><form:errors path="dob"cssStyle="color:red"></form:errors>  </td>
32.</tr>
33.<tr>
34.<td width="100" align="right">Gender</td>
35.<td>                       
36.<form:select path="gender">
37.<form:option value="M" label="Male"/>
38.<form:option value="F" label="Female"/>
39.</form:select>                       
40.</td>
41.<td>
42.</td>                       
43.</tr>
44.<tr>
45.<td width="100" align="right">Address</td>
46.<td><form:input path="address"/></td>
47.<td align="left">
48.<form:errors path="address" cssStyle="color:red"></form:errors>  </td>
49.</tr>
50.<tr>
51.<td width="100" align="right">Email</td>
52.<td><form:input path="email"/></td>
53.<td align="left"><form:errors path="email"cssStyle="color:red"></form:errors>  </td>
54.</tr>
55.<tr>
56.<td width="100" align="right">Mobile</td>
57.<td><form:input path="mobile"/></td>
58.<td align="left">
59.<form:errors path="mobile" cssStyle="color:red"></form:errors>  </td>
60.</tr>
61.<tr>
62.<td colspan="3" align="center">
63.<input type="submit" name="" value="Save">
64. 
65.<input type="reset" name="" value="Reset">
66. 
67.<input type="button"  value="Back"onclick="javascript:go('viewAllContacts.do');">
68.</td>
69.</tr>                   
70.</table>           
71.</form:form>
72.</td>   
73.</tr>
74.</table>
75.</body>
76.</html>


c)editContact.jsp

01.<%@include file="taglib_includes.jsp" %>
02. 
03.<html>
04.<head>
05.<script type="text/javascript" src="js/contacts.js"></script>
06.<title><spring:message code="App.Title"></spring:message> </title>
07.</head>
08.<body style="font-family: Arial; font-size:smaller;">
09. 
10.<table  bgcolor="lightblue" width="750" height="500" align="center"style="border-collapse: collapse;" border="1" bordercolor="#006699" >
11.<tr>
12.<td align="center"><h3>Edit Contact Form</h3></td>
13.</tr>
14.<tr valign="top" align="center">
15.<td align="center">
16.<form:form action="updateContact.do" method="post"commandName="editContact">
17.<table width="500" style="border-collapse: collapse;"border="0" bordercolor="#006699" cellspacing="2"cellpadding="2">                   
18.<tr>
19.<td width="100" align="right">Id</td>
20.<td width="150">
21.<form:input path="id" readonly="true"/></td>
22.<td align="left">
23.<form:errors path="id" cssStyle="color:red"></form:errors>  </td>
24.</tr>
25.<tr>
26.<td width="100" align="right">Name</td>
27.<td>
28.<form:input path="name"/></td>
29.<td align="left">
30.<form:errors path="name" cssStyle="color:red"></form:errors>
31.</td>
32.</tr>
33. 
34.<tr>
35.<td width="100" align="right">DOB</td>
36.<td><form:input path="dob"/></td>
37.<td align="left"><form:errors path="dob"cssStyle="color:red"></form:errors>  </td>
38.</tr>
39.<tr>
40.<td width="100" align="right">Gender</td>
41.<td>                       
42.<form:select path="gender">
43.<form:option value="M" label="Male"/>
44.<form:option value="F" label="Female"/>
45.</form:select>                       
46.</td>
47.<td>
48.</td>                       
49.</tr>
50.<tr>
51.<td width="100" align="right">Address</td>
52.<td><form:input path="address"/></td>
53.<td align="left">
54.<form:errors path="address" cssStyle="color:red"></form:errors>  </td>
55.</tr>
56.<tr>
57.<td width="100" align="right">Email</td>
58.<td><form:input path="email"/></td>
59.<td align="left"><form:errors path="email"cssStyle="color:red"></form:errors>  </td>
60.</tr>
61.<tr>
62.<td width="100" align="right">Mobile</td>
63.<td><form:input path="mobile"/></td>
64.<td align="left">
65.<form:errors path="mobile" cssStyle="color:red"></form:errors>  </td>
66.</tr>
67.<tr valign="bottom">
68.<td colspan="3" align="center">
69.<input type="button"  value="Delete"onclick="javascript:deleteContact('deleteContact.do?id=${editContact.id}');">
70. 
71.<input type="submit" name=""value="Save">                       
72. 
73.<input type="button"  value="Back"onclick="javascript:go('viewAllContacts.do');">
74.</td>
75.</tr>
76. 
77.</table>               
78.</form:form>
79.</td>   
80.</tr>
81.</table>
82. 
83. 
84.</body>
85.</html>


Step#13: Write the javascript file js/contacts.js containing the utility methods

01.function go(url)
02.{
03.window.location = url;
04.}
05. 
06.function deleteContact(url)
07.{
08.var isOK = confirm("Are you sure to delete?");
09.if(isOK)
10.{
11.go(url);
12.}
13.}


Step#14: The welcome file index.jsp

1.<%
2.response.sendRedirect("viewAllContacts.do");
3.%>

Step#15: Start the server and point your browser URL to http://localhost:8080/SpringMVCHibernate

 

http://java.dzone.com/articles/springmvc3-hibernate-crud

分享到:
评论

相关推荐

    Spring+SpringMVC+Hibernate框架集成详解

    Spring+SpringMVC+Hibernate 框架集成详解 本文档旨在详细介绍 Spring、SpringMVC 和 Hibernate 框架的集成,旨在帮助开发人员快速了解这三个框架的集成过程。 Spring 框架 Spring 框架是一个 Java 语言的开源...

    SpringMVC+Hibernate +MySql+ EasyUI实现CRUD

    在"SpringMVC+Hibernate +MySql+ EasyUI实现CRUD"的项目中,开发流程大致如下: 1. **数据库设计**:首先,你需要使用MySql创建数据库和对应的表结构,定义好各字段类型和约束。 2. **实体类创建**:基于数据库表...

    SpringMVC+Hibernate+Spring整合实例

    SpringMVC、Hibernate和Spring是Java开发中三大核心框架,它们各自负责应用程序的不同层面:SpringMVC用于处理HTTP请求和响应,Hibernate则是持久层框架,负责数据库操作,而Spring作为全能容器,提供依赖注入和面向...

    SpringMVC+Hibernate整合的登录Demo 导入即可直接运行适合初次接触整合

    3. **整合步骤**:SpringMVC与Hibernate的整合主要包括以下步骤: - 配置SpringMVC的 DispatcherServlet,设置ContextLoaderListener来加载Spring的配置文件。 - 在Spring配置文件中,定义Hibernate的...

    基于springMvc+hibernate的web application的构建

    5. **DAO层**:编写DAO接口及其实现,使用Hibernate的Session工厂(SessionFactory)和Session对象进行CRUD操作。 6. **Service层**:创建Service接口和实现,包含业务逻辑,通过@Autowired注解注入DAO实现。 7. **...

    SpringMVC+hibernate实例

    3. SpringMVC中使用Hibernate的整合,如声明事务管理器、配置DataSource、注入SessionFactory等。 4. 控制器(Controller)的编写,包括RequestMapping注解的使用、模型数据的绑定与传递。 5. Service层的设计,如何...

    SpringMVC+Hibernate实现增删改

    SpringMVC和Hibernate是Java开发中的两个重要框架,它们在企业级应用开发中扮演着核心角色。SpringMVC是Spring框架的一部分,主要处理Web应用程序的请求-响应模型,而Hibernate则是一个对象关系映射(ORM)框架,...

    SpringMVC与Hibernate整合所需jar

    在Java Web开发中,SpringMVC和Hibernate是两个非常重要的框架。SpringMVC负责处理HTTP请求,实现业务逻辑,而Hibernate则专注于对象关系映射(ORM),简化数据库操作。将这两个框架整合在一起可以构建出高效、灵活...

    基于Spring+SpringMVC+Hibernate的图书管理系统

    系统采用技术:Spring+SpringMVC+Hibernate+jQuery+Ajax+面向接口编程。 简介:该图书管理项目实现了三种角色的功能,分别是超级管理员,图书管理员和读者。在系统中,实现了图书管理,统计管理,系统管理和系统监控...

    SpringMVC+HIbernate登录程序并进行增删改查

    SpringMVC+Hibernate登录程序并进行增删改查是一个经典的Web开发示例,它整合了两个重要的Java技术框架——SpringMVC和Hibernate,用于构建基于Java的动态网站应用。SpringMVC是Spring框架的一部分,主要负责处理...

    基于SpringMVC+Hibernate实现的在线购物商城.zip

    基于springmvc+Hibernate实现的在线购物商城.zip基于springmvc+Hibernate实现的在线购物商城.zip基于springmvc+Hibernate实现的在线购物商城.zip基于springmvc+Hibernate实现的在线购物商城.zip基于springmvc+...

    springMvc+hibernate Demo

    这个 "springMvc+hibernate Demo" 压缩包文件很可能是为了展示如何将这两个框架整合在一起,创建一个完整的数据驱动的 Web 应用。 **Spring MVC 框架** Spring MVC 是 Spring 框架的一部分,设计用于构建可维护、松...

    springmvc+hibernate+oracle基础框架

    Hibernate支持实体类与数据库表之间的映射,提供CRUD操作的API,以及事务管理和缓存机制。在SpringMVC中,Hibernate可以作为数据访问层,负责数据的持久化。 **Oracle** Oracle数据库系统是全球广泛使用的商业关系...

    idea工具创建的Spring+SpringMVC+Hibernate+maven项目

    标题中的"idea工具创建的Spring+SpringMVC+Hibernate+maven项目"指的是使用IntelliJ IDEA这个集成开发环境(IDE)构建的一个Java Web项目,该项目整合了四个关键的技术框架:Spring、SpringMVC、Hibernate以及Maven...

    springmvc+hibernate完整项目

    SpringMVC和Hibernate是Java开发中的两个重要框架,SpringMVC负责处理Web应用的请求-响应,而Hibernate则是一个强大的对象关系映射(ORM)工具,用于简化数据库操作。这个"springmvc+hibernate完整项目"是一个整合了...

    Spring,SpringMvc,Hibernate

    标题中的"Spring,SpringMvc,Hibernate"涉及到的是Java开发中常用的三个开源框架,它们在企业级Web应用开发中起着核心作用。以下是这三个框架的详细解释: **Spring框架**是Java开发中的一个全面的轻量级容器,它...

    springmvc整合hibernate4

    5. DAO层:使用Hibernate的`Session`接口进行CRUD操作。例如,`SessionFactory`获取`Session`,然后使用`Session`的`save()`, `update()`, `delete()`, `load()`或`get()`方法操作数据。 整合步骤通常包括以下部分...

    springmvc+hibernate+easyui

    总结来说,"springmvc+hibernate+easyui"是一个使用SpringMVC作为MVC框架,Hibernate作为ORM工具,EasyUI作为前端组件库的典型企业级Web应用架构。它实现了菜单和数据列表的动态显示,使得系统具备了灵活的权限控制...

    SpringMVC-Hibernate-Crud:简单的SpringMVC和Hibernate Crud示例

    在这个"SpringMVC-Hibernate-Crud"项目中,我们将深入探讨如何整合这两个框架,实现一个基本的CRUD(创建、读取、更新和删除)功能。 首先,SpringMVC是Spring框架的一部分,它提供了一个轻量级的控制层,使得...

    SpringMVC+Hibernate+Spring

    Hibernate通过实体类、配置文件和Session接口实现了数据的CRUD操作,极大地提高了开发效率。在整合Spring时,我们可以使用Spring的HibernateTemplate或JPA来进一步简化数据访问代码。 然后,Spring作为核心框架,...

Global site tag (gtag.js) - Google Analytics