Pada artikel ini merupakan lanjutan dari artikel : Membuat CRUD Menggunakan Spring Hibernate - I [ Read ]
Berikut adalah cara membuat CRUD menggunakan Spring Hibernate :
1. Tambahkan Method Save pada class BukuDAO.java
public void SaveBuku(Buku buku);
2. Tambahkan method save() pada BukuDAOImpl.java
@Override
public void SaveBuku(Buku buku) {
hibernateTemplate.save(buku);
}
3. Buat sebuah Controller, gunakan template dari spring framework. Klik kanan package “Controller” --> “New” --> “Other” -->”Spring Framework” -->”Simple Form Controller” lalu klik Next
4. Isikan “Class Name” kemudian pilih package “Controller” lalu klik Finish
5. Edit file SaveController.java
package Controller;
import Dao.BukuDAO;
import Model.Buku;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
public class SaveController extends SimpleFormController {
private BukuDAO bukuDAO;
public void setBukuDAO(BukuDAO bukuDAO){
this.bukuDAO = bukuDAO;
}
public SaveController() {
//Initialize controller properties here or
//in the Web Application Context
setCommandClass(Buku.class);
setCommandName("save");
//setSuccessView("read");
//setFormView("save");
}
/*@Override
protected void doSubmitAction(Object command) throws Exception {
throw new UnsupportedOperationException("Not yet implemented");
}*/
//Use onSubmit instead of doSubmitAction
//when you need access to the Request, Response, or BindException objects
@Override protected ModelAndView onSubmit( HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)throws Exception {
Buku buku = (Buku) command;
bukuDAO.SaveBuku(buku);
ModelMap map = new ModelMap();
map.addAttribute("allbuku", bukuDAO.getBuku());
return new ModelAndView("read", map);
}
}
6. Kemudian buat file .jsp, klik kanan forlder jsp (/Web Pages/WEB-INF/jsp) “New”-->”JSP” ubah bagian “file name” dengan “Save” lalu klik Finish.
Isikan file Save.jsp
<pre>
<%--
Document : save
Created on : 03 Des 12, 19:49:32
Author : FAHMI
--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form:form method="POST" commandName="save">
<table>
<tr>
<td>ID</td>
<td>:</td>
<td><input type="text" name="id" value="">
</td>
</tr>
<tr>
<td>Nama</td>
<td>:</td>
<td><input type="text" name="nama" value=""></td>
</tr>
<tr>
<td>Harga</td>
<td>:</td>
<td><input type="text" name="harga" value=""></td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="simpan" value="Simpan"></td>
</tr>
</table>
</form:form>
</body>
</html>
</pre>
7. Tambahkan pada file dispatcher-servlet.xml
<pre>
<bean name="/save.htm" class="Controller.SaveController">
<property name="bukuDAO" ref="BukuDAO"/>
<property name="formView" value="save"/>
<property name="successView" value="read"/>
</bean>
</pre>
8. Jalankan klik kanan project pilih “Clean” kemudian “run” lalu ubah url menjadi http://localhost:8084/SpringHibernateCrud/save.htm