- Overview / Setting Up the Database / Creating the Web Project
- Configuring Spring-MVC
- Configuring Hibernate
- Creating the Table View, Interface, and Implementation Classes
- Creating the Table Controller / Creating the Controller View
Creating the Table Controller
The ideal convention is to create a controller for each table. This design keeps everything as a one-to-one ratio between the controller and table class, which makes debugging and maintaining the application much easier. Create a new package called com.springMVCHibrernate.controllers and add the following Registration controller class:
package com.springMVCHibernate.controllers; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.springMVCHibernate.dao.RegistrationDAO; import com.springMVCHibernate.dt.RegistrationDT; @Controller public class RegistrationController { @RequestMapping("/registration/") public String Home() { return "/registration/home"; } @RequestMapping("/registration/add/") public String addRegistration(HttpServletRequest request) { RegistrationDT registrationDT = new RegistrationDT(); registrationDT.setName(request.getParameter("name")); registrationDT.setAddress(request.getParameter("address")); registrationDT.setPhone(request.getParameter("phone")); registrationDT.setLogin(request.getParameter("login")); registrationDT.setPassword(request.getParameter("pass")); RegistrationDAO registrationDAO = new RegistrationDAO(); registrationDAO.addRegistration(registrationDT); return "/registration/addConfirm"; } }
The RequestMapping annotation tells Spring that the following path will be mapped to a page called home.jsp in the views/registration folder:
http://localhost:8080/springMVCHibernate/registration/
The home.jsp page will contain our registration form. The addRegistration method is executed when the /registration/add/ path is requested (posted by our registration form). A new DT object is created, and the properties are populated from the registration form. After that, the addRegistration method of the DAO is called and the record is added to the Registration table.
Creating the Controller View
The last part to getting a small yet complete working example is to create the registration form. Create a folder called views\registration in your project's WEB-INF folder. By this convention, each table would have a folder with the JSP pages for handling the table's CRUD operations. Normally a folder's home.jsp would list the records for that table. But because we're using only one operation, using the home.jsp page seemed like an easy place to put the registration form. Add the home.jsp file to the views\registration folder:
<%@ 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> <link href="<%=request.getContextPath()%>/resources/css/default.css'" rel="Stylesheet" type="text/css"> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Registration Home</title> </head> <body> <form method="post" action="/springMVCHibernate/registration/add/"> <table id="sample" bgcolor="#FFFFFF"> <tr class="topwrow"><td colspan="2"><b>Registration Form</b></td></tr> <tr><td colspan="2"> </td></tr> <tr><td>Name:</td><td><input type="text" name="name"></td></tr> <tr><td>Address:</td><td><input type="text" name="address"></td></tr> <tr><td>Phone:</td><td><input type="text" name="phone"></td></tr> <tr><td colspan="2"> </td></tr> <tr><td>New Login:</td><td><input type="text" name="login"></td></tr> <tr><td>New Pass:</td><td><input type="text" name="pass"></td></tr> <tr><td colspan="2"> </td></tr> <tr><td colspan="2"align="center"><input type="submit" name="submit"></td></tr> </table> </form> </body> </html>
The form's action parameter calls the registration/add/ path, which invokes the addRegistration method of the Registration controller to add the record.
You should now be able to test the Registration page. If you run the application, fill in the registration form, and submit it, you should see a confirmation page registration/addConfirm/ (if you added one). If you check your database and open the Registration table, you should see the new record.
Remember: We didn't create a Registration table. Hibernate did that for us! So how did Hibernate know to create the table? The easy answer is because the table didn't exist, although we had a table class for the table. Hibernate will keep dropping and re-creating the table (even if the table already exists), unless we change the following line in the hibernate.cfg.xml file:
<property name="hbm2ddl.auto">create</property>
Changing the create value to update instructs Hibernate not to perform automatic table generation.
Conclusion
This simple example should get you off to a good start in using Spring MVC with Hibernate. You'll wonder how you ever got along without it!