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 Buku getBukuById(int id);
2. Tambahkan method save() pada BukuDAOImpl.java
@Override
public Buku getBukuById(int id) {
return hibernateTemplate.get(Buku.class, id);
}
3. Buat sebuah Controller dari “Java Class” : ByIdController.java
package Controller;
import Dao.BukuDAO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class ByIdController implements Controller{
private BukuDAO bukuDAO;
public void setBukuDAO(BukuDAO bukuDAO){
this.bukuDAO = bukuDAO;
}
@Override
public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
int id = Integer.parseInt(hsr.getParameter("id"));
ModelMap map = new ModelMap();
map.addAttribute("buku", bukuDAO.getBukuById(id));
return new ModelAndView("update", map);
}
}
4. Tambahkan pada file “dispatcher-servlet.xml”
<bean name="/byid.htm" class="Controller.ByIdController">
<property name="bukuDAO" ref="BukuDAO"/>
</bean>
5. Edit file BukuDAO.java
public void updateBuku(Buku buku);
6. Implementasikan method tadi pada BukuDAOImpl.java
@Override
public void updateBuku(Buku buku) {
hibernateTemplate.merge(buku);
}
7. Buat sebuah controller dari “Simple Form Controller”, klik kanan package Controller --> Other --> Spring Framework --> Simple Form Controller --> lalu klik Next, Beri nama pada bagian “Class Name” kemudian klik Finish.
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 UpdateController extends SimpleFormController {
private BukuDAO bukuDAO;
public void setBukuDao(BukuDAO bukuDAO) {
this.bukuDAO = bukuDAO;
}
public UpdateController() {
//Initialize controller properties here or
//in the Web Application Context
setCommandClass(Buku.class);
setCommandName("update");
//setSuccessView("successView");
//setFormView("formView");
}
@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.updateBuku(buku);
ModelMap map = new ModelMap();
map.addAttribute("allbuku", bukuDAO.getBuku());
return new ModelAndView("read", map);
}
}
8. Buat file JSP, disini saya menamakannya update.jsp
<%@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" action="/SpringHibernateCrud/update.htm" commandName="update">
<table>
<tr>
<td>ID</td>
<td>:</td>
<td>
<input type="text" value="${buku.id}" disabled>
<input type="hidden" name="id" value="${buku.id}">
</td>
</tr>
<tr>
<td>Nama</td>
<td>:</td>
<td><input type="text" name="nama" value="${buku.nama}"></td>
</tr>
<tr>
<td>Harga</td>
<td>:</td>
<td><input type="text" name="harga" value="${buku.harga}"></td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="simpan" value="Simpan"></td>
</tr>
</table>
</form:form>
</body>
</html>
9. Tambahkan pada file “dispatcher-servlet.xml”
<bean name="/update.htm" class="Controller.UpdateController">
<property name="bukuDao" ref="BukuDAO"/>
<property name="formView" value="update"/>
<property name="successView" value="read"/>
</bean>
10. Tambahkan link pada halaman read file jsp
<
a
href
=
"/SpringHibernateCrud/byid.htm?id=${buku.id}"
>Update</
a
>
11. Clean project kemudian Run