Run Spring 3 with hibernate CRUD Application
In this tutorial you will learn about how to "Run Spring 3 wtih hibernate CRUD Application".
When you run your application then first of all execute the index.htm and ineternally serving the index.jsp according to view resolver.
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>Spring 3.0 Application</title>
</head>
<body>
<h1>Welcome To <a href="www.developerhelpway.com">
<b>Developerhelpway.com</b></a></h1>
<h3>Spring 3 CRUD Application With Hibernate Integration</h3>
<a href="addNewStudent.htm"><b>Add new student record</b></a>
<br><br>
<a href="studentList.htm"><b>Student List</b></a>
</body>
</html> |
Run our Spring 3 CRUD Application :
After running the Spring 3 CRUD Application open your web browser and type in address bar "http://localhost:9090/spring-CRUD-Application-With-Hibernate". Then you will the following:

Clicked on "Add new student record" then it searches the url which is maintain in this url like: "http://localhost:9090/spring-CRUD-Application-With-Hibernate/addNewStudent.htm" in your controlller url mapping.
@RequestMapping(value="addNewStudent.htm",
method=RequestMethod.GET)
public String getStudent(Model model){
model.addAttribute("student", new Student());
return "student/studentAddEdit";
}
View resolveer resolves the url and serve the follwoinig jsp file enternally. But, in url will display ".htm".
studentAddEdit.jsp
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="form"
uri="http://www.springframework.org/tags/form" %>
<!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>Add new student</title>
</head>
<body>
<h1>Welcome To <a href="www.developerhelpway.com">
<b>Developerhelpway.com</b></a></h1>
<h3>Spring 3 CRUD Application With Hibernate Integration</h3>
<form:form action="doSaveEdit.htm" modelAttribute="student">
<form:hidden path="id"/>
FirstName <form:input path="firstName"/><br><br>
LastName <form:input path="lastName"/><br><br>
Course <form:input path="course"/><br><br>
Roll <form:input path="roll"/><br><br>
<input type="submit" value="Submit" />
</form:form>
</body>
</html> |
After serving jsp open the following screen. But all input fields are empty. Fill the following records and clicked on "Submit" command button.

Clicked on "Submit" button, it goes to given form action "doSaveEdit.htm" url. Then again find this url in your controller. Which method contains "doSaveEdit.htm" url with method type "POST". It will invoke that method as following:
@RequestMapping(value="doSaveEdit",
method=RequestMethod.POST)
public String postStudent(@ModelAttribute Student student,
Model model){
try {
studentServie.save(student);
model.addAttribute("student", student);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:studentList.htm";
}
The above method calles the service (StudentService)method with save()method with take a Student type parameter. And serve the studentList.jsp.
studentList.jsp
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Student List</title>
</head>
<body>
<h1>Welcome To <a href="www.developerhelpway.com">
<b>Developerhelpway.com</b></a></h1>
<h3>Spring 3 CRUD Application With Hibernate Integration</h3>
<h2><a href="<c:url value="addNewStudent.htm" />">
Add new student</a></h2>
<table width="600">
<tr>
<td width="50">Serial</td>
<td width="200">First Name</td>
<td width="200">Last Name</td>
<td width="100">Course</td>
<td width="50">Roll</td>
<td width="100">Actions</td>
</tr>
<c:forEach items="${students}" var="student" varStatus="loops">
<tr>
<td width="50">${loops.index + 1}</td>
<td width="200">${student.firstName}</td>
<td width="200">${student.lastName}</td>
<td width="100">${student.course}</td>
<td width="50">${student.roll}</td>
<td width="100">
<a href="editStudent.htm?id=${student.id}">Edit</a>
/
<a href="deleteStudent.htm?id=${student.id}">Delete</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
|
After serving the above jsp gives the following output:

Clicked on "Edit" link then open edit url (http://localhost:9090/spring3-CRUD-Application-With-Hibernate/editStudent.htm?id=1). It finds the url in your controller with method "GET". Following mapping will execute in controller:
@RequestMapping(value="editStudent.htm",
method=RequestMethod.GET)
public String getForEditStudent(@RequestParam Integer id,
Model model){
Student student=null;
try {
student = studentServie.getStudent(id);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("student", student);
return "student/studentAddEdit";
}
After executing the above method it will serve the "studentAddEdit.jsp" page and finally you will find the following output on the screen:

Clicked on "Submit" button then again goes to new request to the server with "POST" method. It goes to postStudent() method. If we save with id then update your record of given id.

Clicked on "Delete" link then execute "http://localhost:9090/spring3-CRUD-Application-With-Hibernate/deleteStudent.htm" url. This url find in controller RequestMapping with "get" method.
@RequestMapping(value="deleteStudent.htm",
method=RequestMethod.GET)
public String deleteStudent(@RequestParam Integer id,
Model model){
try {
studentServie.deleteStudent(id);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:studentList.htm";
}
Download Spring 3 CRUD Application