membuat crud sederhana spring mvc

Create Read Update Delete menggunakan spring mvc, spring data jpa, hibernate entity manager, tiles, mysql

Bismillah

q> Melanjutkan tulisan sebelumnya :
https://saifiahmada.wordpress.com/2014/12/13/menambah-menu-stok/

q> Langkah-langkah praktek :
1. Buat bean entity Customer
2. Buat interface CustomerRepositroy
3. Buat class CustomerService
4. Tambahkan definition pada general.xml
5. Tambahkan menu pada classic.jsp
6. Buat file customer.jsp pada /WEB-INF/jsp/
7. Tambahkan class controller CustomerController

1. Buat bean entity Customer

package com.saifiahmada.maven.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Customer {

	@Id
	@GeneratedValue
	private Integer id;

	private String nama;

	private String alamat;

	private String noHp;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNama() {
		return nama;
	}

	public void setNama(String nama) {
		this.nama = nama;
	}

	public String getAlamat() {
		return alamat;
	}

	public void setAlamat(String alamat) {
		this.alamat = alamat;
	}

	public String getNoHp() {
		return noHp;
	}

	public void setNoHp(String noHp) {
		this.noHp = noHp;
	}

}

2. Buat interface CustomerRepositroy

package com.saifiahmada.maven.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.saifiahmada.maven.entity.Customer;

public interface CustomerRepository extends JpaRepository<Customer, Integer> {

}

3. Buat class CustomerService

package com.saifiahmada.maven.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.saifiahmada.maven.entity.Customer;
import com.saifiahmada.maven.repository.CustomerRepository;

@Service
public class CustomerService {

	@Autowired
	private CustomerRepository customerRepository;

	public void save(Customer customer) {
		customerRepository.save(customer);
	}

	public List<Customer> findAll() {
		return customerRepository.findAll();
	}

}

4. Tambahkan definition pada general.xml

	<definition name="customer" extends="common">
		<put-attribute name="title" value="Customer" />
		<put-attribute name="body" value="/WEB-INF/jsp/customer.jsp" />
		<put-attribute name="current" value="customer" />
	</definition>

5. Tambahkan menu pada classic.jsp

<li class="${current == 'customers' ? 'active' : '' }">
<a href="/customer/awal.html">Customer</a></li>

6. Buat file customer.jsp pada /WEB-INF/jsp/

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ include file="../layout/taglib.jsp"%>

<form:form commandName="customer" action="/customer/add.html"
	cssClass="form-horizontal">

	<c:if test="${ ! empty customer.nama }">
		<div class="form-group">
			<label for="id" class="col-sm-2 control-label">Id</label>
			<div class="col-sm-10">
				<form:input path="id" cssClass="form-control" readonly="true" disabled="true" />
				<form:hidden path="id"/>
			</div>
		</div>

	</c:if>

	<div class="form-group">
		<label for="nama" class="col-sm-2 control-label">Nama</label>
		<div class="col-sm-10">
			<form:input path="nama" cssClass="form-control" />
		</div>
	</div>

	<div class="form-group">
		<label for="alamat" class="col-sm-2 control-label">Alamat</label>
		<div class="col-sm-10">
			<form:input path="alamat" cssClass="form-control" />
		</div>
	</div>

	<div class="form-group">
		<label for="noHp" class="col-sm-2 control-label">No HP</label>
		<div class="col-sm-10">
			<form:input path="noHp" cssClass="form-control" />
		</div>
	</div>

	<input type="submit" value="Simpan" class="btn btn-success" />
	<a href="/customer/awal.html" class="btn btn-success">Reset</a>

</form:form>

<br />

<table class="table table-bordered table-hover table-striped">
	<thead>
		<tr>
			<th>Nama</th>
			<th>Alamat</th>
			<th>No. HP</th>
			<th>Edit</th>
			<th>Delete</th>
		</tr>
	</thead>
	<tbody>
		<c:forEach items="${customers}" var="c">
			<tr>
				<td>${c.nama }</td>
				<td>${c.alamat }</td>
				<td>${c.noHp }</td>
				<td><a href="/customer/edit/${c.id}.html">Edit</a></td>
				<td><a href="/customer/delete/${c.id}.html">Delete</a></td>
			</tr>
		</c:forEach>
	</tbody>
</table>

7. Tambahkan class controller CustomerController

package com.saifiahmada.maven.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.saifiahmada.maven.entity.Customer;
import com.saifiahmada.maven.service.CustomerService;

@Controller
@RequestMapping(value = "/customer")
public class CustomerController {

	@Autowired
	private CustomerService customerService;

	@ModelAttribute("customer")
	public Customer getCustomer(){
		return new Customer();
	}

	@RequestMapping(value="/awal")
	public String awal(Model model){
		model.addAttribute("customer", new Customer());
		model.addAttribute("customers", customerService.findAll());
		return "customer";
	}

	@RequestMapping(value = "/add", method=RequestMethod.POST)
	public String add(@ModelAttribute("customer") Customer customer){
		customerService.save(customer);
		return "redirect:/customer/awal.html";
	}

	@RequestMapping(value = "/edit/{id}")
	public String edit(@PathVariable("id") Integer id, Model model){
		Customer customer = customerService.findOne(id);
		model.addAttribute("customer", customer);
		model.addAttribute("customers", customerService.findAll());
		return "customer";
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id, Model model){
		customerService.delete(id);
		model.addAttribute("customers", customerService.findAll());
		return "customer";
	}

}

q> Jalankan jetty untuk melihat hasilnya

q> Awal menu Customer di-klik
Selection_632

q> Edit salah satu data, kemudian tekan tombol simpan

Selection_633

q> Masukkan data baru

Selection_634

q> Data tersimpan

Selection_635

Alhamdulillah

Published by

saifiahmada

Begin a big Process with a bit trying

One thought on “membuat crud sederhana spring mvc”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s