Persiapan:
- Sudah membaca artikel Spring Web MVC – Create Log File (Episode 3). Jika belum, silahkan baca artikel ini.
- Membaca doa sebelum memulai mengikuti artikel ini.
Note: Untuk postingan ini sudah mulai agak advance sedikit, saya coba menjelaskan secara perlahan, dan kalau anda bingung.
File-file yang dibutuhkan:
- jtds-1.2.4.jar — silahkan download disini
- ibatis-sqlmap-2.3.4.726.jar — silahkan download disini
- c3p0-0.9.1.2.jar — silahkan download disini
Pada artikel ini saya akan coba memberikan contoh membuat sebuah aplikasi Simple CRUD dengan menggunakan iBatis. Untuk membuat sebuah aplikasi yang mengandung CRUD ada 2 pendekatan yang bisa biasanya saya gunakan. Yang pertama adalah membangun model Dao nya terlebih dahulu baru membangun view jsp nya, cara kedua adalah membangun flow pada view nya terlebih dahulu, setelah itu baru membangun model Dao nya. Pada contoh kali ini saya akan menggunakan cara yang kedua.
PART 1 – Membangun view
Anggap kita akan membuat data mahasiswa yang terdiri dari NIM, NAMA, dan UMUR (3 saja dulu ya, jangan terlalu banyak. :p). Lalu pada aplikasi ini kita akan membuat fasilitas untuk:
- Add New
- Edit
- Search
- Show All
Silahkan ikuti langkah2 berikut:
- pada package com.firstProgram.model buat class Mahasiswa.java
package com.firstProgram.model; public class Mahasiswa { private String nim; private String nama; private Integer umur; public String getNim() { return nim; } public void setNim(String nim) { this.nim = nim; } public String getNama() { return nama; } public void setNama(String nama) { this.nama = nama; } public Integer getUmur() { return umur; } public void setUmur(Integer umur) { this.umur = umur; } }
- pada folder firstProgram/WebContent/WEB-INF/pages buat file mahasiswa_show.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Episode 5</title> <link href="<c:url value="/css/style.css"/>" rel="stylesheet" type="text/css" /> </head> <body> <a href="<c:url value="/mahasiswa/all.asik" />">Show All</a> --- <a href="<c:url value="/mahasiswa/add.asik" />">Add New</a> --- <a href="<c:url value="/mahasiswa/search.asik" />">Search</a> <br /> <br /> <br /> <table width="500" border="1" cellpadding="2" cellspacing="2"> <tr> <th width="30%">NIM</th> <th width="50%">Nama</th> <th width="10%">Umur</th> <th width="20%" colspan="2"> </th> </tr> <tr> <td>nim</td> <td>nama</td> <td>umur</td> <td> <form:form action="edit.asik"> <input type="hidden" id="nim" name="nim" value="nim"/> <input type="hidden" id="nama" name="nama" value="nama"/> <input type="hidden" id="umur" name="umur" value="umur"/> <input type="submit" value="Edit" style="width:60px;"/> </form:form> </td> <td> <form:form action="delete.asik"> <input type="hidden" id="nim" name="nim" value="nim"/> <input type="submit" value="Delete" style="width:60px;"/> </form:form> </td> </tr> </table> </body> </html>
- pada folder firstProgram/WebContent/WEB-INF/pages buat file mahasiswa_new.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Episode 5</title> <link href="<c:url value="/css/style.css"/>" rel="stylesheet" type="text/css" /> </head> <body> <a href="<c:url value="/mahasiswa/all.asik" />">Show All</a> --- <a href="<c:url value="/mahasiswa/add.asik" />">Add New</a> --- <a href="<c:url value="/mahasiswa/search.asik" />">Search</a> <h1>Please Input:</h1> <form:form> <div> <label for="nim">NIM</label> <input type="text" id="nim" name="nim" value="nim"/> </div> <div> <label for="nama">Nama</label> <input type="text" id="nama" name="nama" value="nama"/> </div> <div> <label for="umur">Age</label> <input type="text" id="umur" name="umur" value="umur"/> </div> <div> <label> </label> <input type="submit" value="Save"/> <input type="reset" value="Cancel"/> </div> </form:form> <br /> <br /> <br /> <table width="500" border="1" cellpadding="2" cellspacing="2"> <tr> <th width="30%">NIM</th> <th width="50%">Nama</th> <th width="10%">Umur</th> <th width="20%" colspan="2"> </th> </tr> <tr> <td>nim</td> <td>nama</td> <td>umur</td> <td> <form:form action="edit.asik"> <input type="hidden" id="nim" name="nim" value="nim"/> <input type="hidden" id="nama" name="nama" value="nama"/> <input type="hidden" id="umur" name="umur" value="umur"/> <input type="submit" value="Edit" style="width:60px;"/> </form:form> </td> <td> <form:form action="delete.asik"> <input type="hidden" id="nim" name="nim" value="nim"/> <input type="submit" value="Delete" style="width:60px;"/> </form:form> </td> </tr> </table> </body> </html>
- pada folder firstProgram/WebContent/WEB-INF/pages buat file mahasiswa_edit.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Episode 5</title> <link href="<c:url value="/css/style.css"/>" rel="stylesheet" type="text/css" /> </head> <body> <a href="<c:url value="/mahasiswa/all.asik" />">Show All</a> --- <a href="<c:url value="/mahasiswa/add.asik" />">Add New</a> --- <a href="<c:url value="/mahasiswa/search.asik" />">Search</a> <h1>Please Input:</h1> <form:form> <div> <label for="nim">NIM</label> <input type="text" id="nim" name="nim" value="nim" READONLY="TRUE"/> </div> <div> <label for="nama">Nama</label> <input type="text" id="nama" name="nama" value="nama"/> </div> <div> <label for="umur">Umur</label> <input type="text" id="umur" name="umur" value="umur"/> </div> <div> <label> </label> <input type="submit" value="Update"/> <input type="reset" value="Cancel"/> </div> </form:form> <br /> <br /> <br /> <table width="500" border="1" cellpadding="2" cellspacing="2"> <tr> <th width="30%">NIM</th> <th width="50%">Nama</th> <th width="10%">Umur</th> <th width="20%" colspan="2"> </th> </tr> <tr> <td>nim</td> <td>nama</td> <td>umur</td> <td> <form:form action="edit.asik"> <input type="hidden" id="nim" name="nim" value="nim"/> <input type="hidden" id="nama" name="nama" value="nama"/> <input type="hidden" id="umur" name="umur" value="umur"/> <input type="submit" value="Edit" style="width:60px;"/> </form:form> </td> <td> <form:form action="delete.asik"> <input type="hidden" id="nim" name="nim" value="nim"/> <input type="submit" value="Delete" style="width:60px;"/> </form:form> </td> </tr> </table> </body> </html>
- pada folder firstProgram/WebContent/WEB-INF/pages buat file mahasiswa_search.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Episode 5</title> <link href="<c:url value="/css/style.css"/>" rel="stylesheet" type="text/css" /> </head> <body> <a href="<c:url value="/mahasiswa/all.asik" />">Show All</a> --- <a href="<c:url value="/mahasiswa/add.asik" />">Add New</a> --- <a href="<c:url value="/mahasiswa/search.asik" />">Search</a> <h1>Please Input:</h1> <form:form> <div> <label for="nim">NIM</label> <input type="text" id="nim" name="nim" value="${mahasiswa.nim}"/> </div> <div> <label> </label> <input type="submit" value="Search"/> <input type="reset" value="Cancel"/> </div> </form:form> <br /> <br /> <br /> <table width="500" border="1" cellpadding="2" cellspacing="2"> <tr> <th width="30%">NIM</th> <th width="50%">Nama</th> <th width="10%">Umur</th> <th width="20%" colspan="2"> </th> </tr> <tr> <td>nim</td> <td>nama</td> <td>umur</td> <td> <form:form action="edit.asik"> <input type="hidden" id="nim" name="nim" value="nim"/> <input type="hidden" id="nama" name="nama" value="nama"/> <input type="hidden" id="umur" name="umur" value="umur"/> <input type="submit" value="Edit" style="width:60px;"/> </form:form> </td> <td> <form:form action="delete.asik"> <input type="hidden" id="nim" name="nim" value="nim"/> <input type="submit" value="Delete" style="width:60px;"/> </form:form> </td> </tr> </table> </body> </html>
- Pada package com.firstProgram.web buat Class MahasiswaAllController.java
package com.firstProgram.web; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.firstProgram.dao.SimpleDao; @Controller public class MahasiswaAllController { private SimpleDao simpleDao; @Autowired public void setSimpleDao(SimpleDao simpleDao) { this.simpleDao = simpleDao; } @RequestMapping (value="/mahasiswa/all.asik", method=RequestMethod.GET) public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map model = new HashMap(); return new ModelAndView("mahasiswa/all", model); } }
- Pada package com.firstProgram.web buat Class MahasiswaAddController.java
package com.firstProgram.web; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.firstProgram.dao.SimpleDao; import com.firstProgram.model.Mahasiswa; @Controller public class MahasiswaAddController { private SimpleDao simpleDao; @Autowired public void setSimpleDao(SimpleDao simpleDao) { this.simpleDao = simpleDao; } @RequestMapping (value="/mahasiswa/add.asik", method=RequestMethod.GET) public ModelAndView addNew(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map model = new HashMap(); return new ModelAndView("mahasiswa/add", model); } @RequestMapping (value="/mahasiswa/add.asik", method=RequestMethod.POST) public ModelAndView saveNew(@RequestParam("nim")String nim ,@RequestParam("nama")String nama ,@RequestParam("umur")Integer umur ,HttpServletRequest request ,HttpServletResponse response) throws ServletException, IOException { Map model = new HashMap(); Mahasiswa mahasiswa = new Mahasiswa(); mahasiswa.setNama(nama); mahasiswa.setNim(nim); mahasiswa.setUmur(umur); return new ModelAndView("mahasiswa/add", model); } }
- Pada package com.firstProgram.web buat Class MahasiswaEditController.java
package com.firstProgram.web; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.firstProgram.dao.SimpleDao; import com.firstProgram.model.Mahasiswa; @Controller public class MahasiswaEditController { private SimpleDao simpleDao; @Autowired public void setSimpleDao(SimpleDao simpleDao) { this.simpleDao = simpleDao; } @RequestMapping (value="/mahasiswa/edit.asik", method=RequestMethod.POST) public ModelAndView saveNew(@RequestParam(value="nim", required=false)String nim ,@RequestParam(value="nama", required=false)String nama ,@RequestParam(value="umur", required=false)Integer umur ,HttpServletRequest request ,HttpServletResponse response) throws ServletException, IOException { Map model = new HashMap(); if(nama == null){ nama = ""; } if(umur == null){ umur = 0; } Mahasiswa mahasiswa = new Mahasiswa(); mahasiswa.setNama(nama); mahasiswa.setNim(nim); mahasiswa.setUmur(umur); return new ModelAndView("mahasiswa/edit", model); } }
- Pada package com.firstProgram.web buat Class MahasiswaSearchController.java
package com.firstProgram.web; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.firstProgram.dao.SimpleDao; import com.firstProgram.model.Mahasiswa; @Controller public class MahasiswaSearchController { private SimpleDao simpleDao; @Autowired public void setSimpleDao(SimpleDao simpleDao) { this.simpleDao = simpleDao; } @RequestMapping (value="/mahasiswa/search.asik", method=RequestMethod.GET) public ModelAndView search(HttpServletRequest request ,HttpServletResponse response) throws ServletException, IOException { Map model = new HashMap(); return new ModelAndView("mahasiswa/search", model); } @RequestMapping (value="/mahasiswa/search.asik", method=RequestMethod.POST) public ModelAndView saveNew(@RequestParam(value="nim", required=false)String nim ,HttpServletRequest request ,HttpServletResponse response) throws ServletException, IOException { Map model = new HashMap(); return new ModelAndView("mahasiswa/search", model); } }
- Edit file index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Redirected because we can't set the welcome page to a virtual URL. --%> <c:redirect url="/mahasiswa/all.asik"/>
- Edit file view.properties
pages/hello.url = /WEB-INF/pages/hello.jsp pages/hello.class= org.springframework.web.servlet.view.JstlView pages/excell.url = /WEB-INF/pages/excell.jsp pages/excell.class= org.springframework.web.servlet.view.JstlView pages/excellReport.class = com.firstProgram.report.FirstProgramExcell mahasiswa/all.url = /WEB-INF/pages/mahasiswa_show.jsp mahasiswa/all.class = org.springframework.web.servlet.view.JstlView mahasiswa/add.url = /WEB-INF/pages/mahasiswa_new.jsp mahasiswa/add.class = org.springframework.web.servlet.view.JstlView mahasiswa/edit.url = /WEB-INF/pages/mahasiswa_edit.jsp mahasiswa/edit.class = org.springframework.web.servlet.view.JstlView mahasiswa/search.url = /WEB-INF/pages/mahasiswa_search.jsp mahasiswa/search.class = org.springframework.web.servlet.view.JstlView
- Coba jalankan aplikasi anda. http://localhost:8080/firstProgram . Pastikan flow data dari tiap jsp nya sudah benar (jika submit makan masuk ke dalam parameter yang dimaksud.
PART 2 – Membangun model Dao
Database yang saya gunakan adalah MS SQL Server 2000. Coba anda perhatikan 3 library yang dibutuhkan diatas:
- jtds-1.2.4.jar – ini adalah driver yang dibutuhkan untuk koneksi ke database MS SQL Server
- ibatis-sqlmap-2.3.4.726.jar – ini adalah library iBatis yang akan digunakan untuk mapping database ke class java.
- c3p0-0.9.1.2.jar – ini adalah library yang digunakan untuk connection pooling. Dengan menggunakan connection pooling artinya kita tidak perlu create connection ke database setiap kali transaksi. Pada aplikasi Web, umumnya koneksi ke database hanya di create pada saat server Start Up saja.
Anggap database yang saya gunakan:
- Server: localhost
- Database: testing
Pertama create table pada database:
CREATE TABLE [dbo].[MAHASISWA_2011] ( [NIM] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [NAMA] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [UMUR] [int] NULL ) ON [PRIMARY]
Silahkan ikuti langkah2 berikut:
- Download library-library yang saya sebutkan pada awal artikel lalu diinclude ke dalam project firstProgram
- Edit web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>firstProgram</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <filter> <filter-name>LoggerContextFilter</filter-name> <filter-class>ch.qos.logback.classic.selector.servlet.LoggerContextFilter</filter-class> </filter> <filter-mapping> <filter-name>LoggerContextFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>tjong</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>tjong</servlet-name> <url-pattern>*.asik</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>tjong</servlet-name> <url-pattern>*.xls</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
- Pada folder firstProgram/WebContent/WEB-INF create file applicationContext-ibatis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="exampleDataSource"/> </bean> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="classpath:sql-map-config.xml"/> </bean> <bean id="exampleDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClassName}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="simpleDao" class="com.firstProgram.dao.SimpleDaoImpl"> <property name="dataSource" ref="exampleDataSource"/> <property name="sqlMapClient" ref="sqlMapClient"/> </bean> </beans>
- Pada folder firstProgram/resources, create file sql-map-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <sqlMap resource="sqlmaps/simple.xml" /> </sqlMapConfig>
- Pada folder firstProgram/resources, create file jdbc.properties
jdbc.driverClassName=net.sourceforge.jtds.jdbc.Driver jdbc.url=jdbc:jtds:sqlserver://localhost/testing jdbc.username=admin jdbc.password=andreas
- Pada folder firstProgram/resources, create folder sqlmaps
- Pada folder firstProgram/resources/sqlmaps, create file simple.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="simple"> <typeAlias alias="Mahasiswa" type="com.firstProgram.model.Mahasiswa"/> <resultMap id="Mahasiswa"> <result property="nim" column="nim"/> <result property="nama" column="nama"/> <result property="umur" column="umur"/> </resultMap> <select id="getAllMahasiswa" resultMap="Mahasiswa"> SELECT nim, nama, umur FROM MAHASISWA_2011 ORDER BY nim </select> <select id="getMahasiswa" resultMap="Mahasiswa" parameterClass="java.util.HashMap"> SELECT nim, nama, umur FROM MAHASISWA_2011 WHERE NIM = #nim# ORDER BY nim </select> <delete id="deleteMahasiswa" parameterClass="java.util.HashMap"> DELETE FROM MAHASISWA_2011 WHERE NIM = #nim# </delete> <update id="updateMahasiswa" parameterClass="java.util.HashMap"> UPDATE MAHASISWA_2011 SET NIM = #nim# , NAMA = #nama# , UMUR = #umur# WHERE NIM = #nim# </update> <insert id="addMahasiswa" parameterClass="java.util.HashMap"> INSERT into MAHASISWA_2011(NIM, NAMA, UMUR) VALUES(#nim#,#nama#,#umur#) </insert> </sqlMap>
- Create package com.firstProgram.dao
- Pada package com.firstProgram.dao, create file SimpleDao.java
package com.firstProgram.dao; import java.util.List; import com.firstProgram.model.Mahasiswa; public interface SimpleDao { public Mahasiswa getMahasiswa(String nim); public List<Mahasiswa> getAllMahasiswa(); public void insertMahasiswa(Mahasiswa mahasiswa); public void updateMahasiswa(Mahasiswa mahasiswa); public void deleteMahasiswa(Mahasiswa mahasiswa); }
- Pada package com.firstProgram.dao, create file SimpleDaoImpl.java
package com.firstProgram.dao; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import com.firstProgram.model.Mahasiswa; public class SimpleDaoImpl extends SqlMapClientDaoSupport implements SimpleDao { public void deleteMahasiswa(Mahasiswa mahasiswa) { Map input = new HashMap(); input.put("nim", mahasiswa.getNim()); getSqlMapClientTemplate().delete("deleteMahasiswa", input); } public List<Mahasiswa> getAllMahasiswa() { return (List<Mahasiswa>) getSqlMapClientTemplate().queryForList("getAllMahasiswa"); } public Mahasiswa getMahasiswa(String nim) { Map input = new HashMap(); input.put("nim", nim); return (Mahasiswa) getSqlMapClientTemplate().queryForObject("getMahasiswa", input); } public void updateMahasiswa(Mahasiswa mahasiswa) { Map input = new HashMap(); input.put("nim", mahasiswa.getNim()); input.put("nama", mahasiswa.getNama()); input.put("umur", mahasiswa.getUmur()); getSqlMapClientTemplate().update("updateMahasiswa", input); } public void insertMahasiswa(Mahasiswa mahasiswa) { Map input = new HashMap(); input.put("nim", mahasiswa.getNim()); input.put("nama", mahasiswa.getNama()); input.put("umur", mahasiswa.getUmur()); getSqlMapClientTemplate().insert("addMahasiswa", input); } }
- Pada package com.firstProgram.web, edit file MahasiswaAddController.java
package com.firstProgram.web; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.firstProgram.dao.SimpleDao; import com.firstProgram.model.Mahasiswa; @Controller public class MahasiswaAddController { private SimpleDao simpleDao; @Autowired public void setSimpleDao(SimpleDao simpleDao) { this.simpleDao = simpleDao; } @RequestMapping (value="/mahasiswa/add.asik", method=RequestMethod.GET) public ModelAndView addNew(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map model = new HashMap(); return new ModelAndView("mahasiswa/add", model); } @RequestMapping (value="/mahasiswa/add.asik", method=RequestMethod.POST) public ModelAndView saveNew(@RequestParam("nim")String nim ,@RequestParam("nama")String nama ,@RequestParam("umur")Integer umur ,HttpServletRequest request ,HttpServletResponse response) throws ServletException, IOException { Map model = new HashMap(); Mahasiswa mahasiswa = new Mahasiswa(); mahasiswa.setNama(nama); mahasiswa.setNim(nim); mahasiswa.setUmur(umur); simpleDao.insertMahasiswa(mahasiswa); return new ModelAndView("mahasiswa/add", model); } }
- Pada package com.firstProgram.web, edit file MahasiswaAllController.java
package com.firstProgram.web; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.firstProgram.dao.SimpleDao; @Controller public class MahasiswaAllController { private SimpleDao simpleDao; @Autowired public void setSimpleDao(SimpleDao simpleDao) { this.simpleDao = simpleDao; } @RequestMapping (value="/mahasiswa/all.asik", method=RequestMethod.GET) public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map model = new HashMap(); model.put("mahasiswas", simpleDao.getAllMahasiswa()); return new ModelAndView("mahasiswa/all", model); } }
- Pada package com.firstProgram.web, edit file MahasiswaEditController.java
package com.firstProgram.web; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.firstProgram.dao.SimpleDao; import com.firstProgram.model.Mahasiswa; @Controller public class MahasiswaEditController { private SimpleDao simpleDao; @Autowired public void setSimpleDao(SimpleDao simpleDao) { this.simpleDao = simpleDao; } @RequestMapping (value="/mahasiswa/edit.asik", method=RequestMethod.POST) public ModelAndView saveNew(@RequestParam(value="nim", required=false)String nim ,@RequestParam(value="nama", required=false)String nama ,@RequestParam(value="umur", required=false)Integer umur ,HttpServletRequest request ,HttpServletResponse response) throws ServletException, IOException { Map model = new HashMap(); if(nama == null){ nama = ""; } if(umur == null){ umur = 0; } Mahasiswa mahasiswa = new Mahasiswa(); mahasiswa.setNama(nama); mahasiswa.setNim(nim); mahasiswa.setUmur(umur); simpleDao.updateMahasiswa(mahasiswa); mahasiswa = simpleDao.getMahasiswa(nim); model.put("mahasiswa", mahasiswa); model.put("mahasiswas", simpleDao.getAllMahasiswa()); return new ModelAndView("mahasiswa/edit", model); } }
- Pada package com.firstProgram.web, edit file MahasiswaSearchController.java
package com.firstProgram.web; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.firstProgram.dao.SimpleDao; import com.firstProgram.model.Mahasiswa; @Controller public class MahasiswaSearchController { private SimpleDao simpleDao; @Autowired public void setSimpleDao(SimpleDao simpleDao) { this.simpleDao = simpleDao; } @RequestMapping (value="/mahasiswa/search.asik", method=RequestMethod.GET) public ModelAndView search(HttpServletRequest request ,HttpServletResponse response) throws ServletException, IOException { Map model = new HashMap(); return new ModelAndView("mahasiswa/search", model); } @RequestMapping (value="/mahasiswa/search.asik", method=RequestMethod.POST) public ModelAndView saveNew(@RequestParam(value="nim", required=false)String nim ,HttpServletRequest request ,HttpServletResponse response) throws ServletException, IOException { Map model = new HashMap(); Mahasiswa mahasiswa = new Mahasiswa(); mahasiswa = simpleDao.getMahasiswa(nim); model.put("mahasiswa", mahasiswa); return new ModelAndView("mahasiswa/search", model); } }
- Edit file mahasiswa_edit.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Episode 5</title> <link href="<c:url value="/css/style.css"/>" rel="stylesheet" type="text/css" /> </head> <body> <a href="<c:url value="/mahasiswa/all.asik" />">Show All</a> --- <a href="<c:url value="/mahasiswa/add.asik" />">Add New</a> --- <a href="<c:url value="/mahasiswa/search.asik" />">Search</a> <h1>Please Input:</h1> <form:form> <div> <label for="nim">NIM</label> <input type="text" id="nim" name="nim" value="${mahasiswa.nim}" READONLY="TRUE"/> </div> <div> <label for="nama">Nama</label> <input type="text" id="nama" name="nama" value="${mahasiswa.nama}"/> </div> <div> <label for="umur">Umur</label> <input type="text" id="umur" name="umur" value="${mahasiswa.umur}"/> </div> <div> <label> </label> <input type="submit" value="Update"/> <input type="reset" value="Cancel"/> </div> </form:form> <br /> <br /> <br /> <table width="500" border="1" cellpadding="2" cellspacing="2"> <tr> <th width="30%">NIM</th> <th width="50%">Nama</th> <th width="10%">Umur</th> <th width="20%" colspan="2"> </th> </tr> <c:forEach items="${mahasiswas}" var="mahasiswa" varStatus="status"> <c:if test="${status.index % 2 == 0}"> <tr> </c:if> <c:if test="${status.index % 2 == 1}"> <tr> </c:if> <td>${mahasiswa.nim}</td> <td>${mahasiswa.nama}</td> <td>${mahasiswa.umur}</td> <td> <form:form action="edit.asik"> <input type="hidden" id="nim" name="nim" value="${mahasiswa.nim}"/> <input type="hidden" id="nama" name="nama" value="${mahasiswa.nama}"/> <input type="hidden" id="umur" name="umur" value="${mahasiswa.umur}"/> <input type="submit" value="Edit" style="width:60px;"/> </form:form> </td> <td> <form:form action="delete.asik"> <input type="hidden" id="nim" name="nim" value="${mahasiswa.nim}"/> <input type="submit" value="Delete" style="width:60px;"/> </form:form> </td> </tr> </c:forEach> </table> </body> </html>
- Edit file mahasiswa_new.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Episode 5</title> <link href="<c:url value="/css/style.css"/>" rel="stylesheet" type="text/css" /> </head> <body> <a href="<c:url value="/mahasiswa/all.asik" />">Show All</a> --- <a href="<c:url value="/mahasiswa/add.asik" />">Add New</a> --- <a href="<c:url value="/mahasiswa/search.asik" />">Search</a> <h1>Please Input:</h1> <form:form> <div> <label for="nim">NIM</label> <input type="text" id="nim" name="nim" value="${mahasiswa.nim}"/> </div> <div> <label for="nama">Nama</label> <input type="text" id="nama" name="nama" value="${mahasiswa.nama}"/> </div> <div> <label for="umur">Age</label> <input type="text" id="umur" name="umur" value="${mahasiswa.umur}"/> </div> <div> <label> </label> <input type="submit" value="Save"/> <input type="reset" value="Cancel"/> </div> </form:form> <br /> <br /> <br /> <table width="500" border="1" cellpadding="2" cellspacing="2"> <tr> <th width="30%">NIM</th> <th width="50%">Nama</th> <th width="10%">Umur</th> <th width="20%" colspan="2"> </th> </tr> <c:forEach items="${mahasiswas}" var="mahasiswa" varStatus="status"> <c:if test="${status.index % 2 == 0}"> <tr> </c:if> <c:if test="${status.index % 2 == 1}"> <tr> </c:if> <td>${mahasiswa.nim}</td> <td>${mahasiswa.nama}</td> <td>${mahasiswa.umur}</td> <td> <form:form action="edit.asik"> <input type="hidden" id="nim" name="nim" value="${mahasiswa.nim}"/> <input type="hidden" id="nama" name="nama" value="${mahasiswa.nama}"/> <input type="hidden" id="umur" name="umur" value="${mahasiswa.umur}"/> <input type="submit" value="Edit" style="width:60px;"/> </form:form> </td> <td> <form:form action="delete.asik"> <input type="hidden" id="nim" name="nim" value="${mahasiswa.nim}"/> <input type="submit" value="Delete" style="width:60px;"/> </form:form> </td> </tr> </c:forEach> </table> </body> </html>
- Edit file mahasiswa_search.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Episode 5</title> <link href="<c:url value="/css/style.css"/>" rel="stylesheet" type="text/css" /> </head> <body> <a href="<c:url value="/mahasiswa/all.asik" />">Show All</a> --- <a href="<c:url value="/mahasiswa/add.asik" />">Add New</a> --- <a href="<c:url value="/mahasiswa/search.asik" />">Search</a> <h1>Please Input:</h1> <form:form> <div> <label for="nim">NIM</label> <input type="text" id="nim" name="nim" value="${mahasiswa.nim}"/> </div> <div> <label> </label> <input type="submit" value="Search"/> <input type="reset" value="Cancel"/> </div> </form:form> <br /> <br /> <br /> <c:if test = "${!(empty mahasiswa)}"> <table width="500" border="1" cellpadding="2" cellspacing="2"> <tr> <th width="30%">NIM</th> <th width="50%">Nama</th> <th width="10%">Umur</th> <th width="20%" colspan="2"> </th> </tr> <tr> <td>${mahasiswa.nim}</td> <td>${mahasiswa.nama}</td> <td>${mahasiswa.umur}</td> <td> <form:form action="edit.asik"> <input type="hidden" id="nim" name="nim" value="${mahasiswa.nim}"/> <input type="hidden" id="nama" name="nama" value="${mahasiswa.nama}"/> <input type="hidden" id="umur" name="umur" value="${mahasiswa.umur}"/> <input type="submit" value="Edit" style="width:60px;"/> </form:form> </td> <td> <form:form action="delete.asik"> <input type="hidden" id="nim" name="nim" value="${mahasiswa.nim}"/> <input type="submit" value="Delete" style="width:60px;"/> </form:form> </td> </tr> </table> </c:if> </body> </html>
- Edit file mahasiswa_show.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Episode 5</title> <link href="<c:url value="/css/style.css"/>" rel="stylesheet" type="text/css" /> </head> <body> <a href="<c:url value="/mahasiswa/all.asik" />">Show All</a> --- <a href="<c:url value="/mahasiswa/add.asik" />">Add New</a> --- <a href="<c:url value="/mahasiswa/search.asik" />">Search</a> <br /> <br /> <br /> <table width="500" border="1" cellpadding="2" cellspacing="2"> <tr> <th width="30%">NIM</th> <th width="50%">Nama</th> <th width="10%">Umur</th> <th width="20%" colspan="2"> </th> </tr> <c:forEach items="${mahasiswas}" var="mahasiswa" varStatus="status"> <c:if test="${status.index % 2 == 0}"> <tr> </c:if> <c:if test="${status.index % 2 == 1}"> <tr> </c:if> <td>${mahasiswa.nim}</td> <td>${mahasiswa.nama}</td> <td>${mahasiswa.umur}</td> <td> <form:form action="edit.asik"> <input type="hidden" id="nim" name="nim" value="${mahasiswa.nim}"/> <input type="hidden" id="nama" name="nama" value="${mahasiswa.nama}"/> <input type="hidden" id="umur" name="umur" value="${mahasiswa.umur}"/> <input type="submit" value="Edit" style="width:60px;"/> </form:form> </td> <td> <form:form action="delete.asik"> <input type="hidden" id="nim" name="nim" value="${mahasiswa.nim}"/> <input type="submit" value="Delete" style="width:60px;"/> </form:form> </td> </tr> </c:forEach> </table> </body> </html>
- Silahkan testing aplikasi anda, http://localhost:8080/firstProgram
Kira-kira result nya seperti ini: