Persiapan:
- Tau cara Start New (Dynamic Web) Project menggunakan Eclipse. Jika belum tahu silahkan baca artikel ini
- Sudah membaca artikel Spring Web MVC – Quick Start with Eclipse (Episode 1). Jika belum, silahkan baca artikel ini
Apakah anda sudah mengerti alur dari program yang sudah anda buat pada Episode 1? Jika belum, akan saya coba jelaskan agak detail:
- Anda (client) merequest alamat http://localhost:8080/firstProgram/hello.asik
- Web Server akan membaca settingan web.xml, disitu kita deklarasikan kalau ada request yang berakhiran dengan .asik maka akan diproses oleh Class
org.springframework.web.servlet.DispatcherServlet
, dengan settingan yang dibuat ada di tjong-servlet.xml - Lalu si Web Server akan membaca settingan lagi di tjong-servlet.xml, disitu kita deklarasikan, jika ada yang merequest page “/hello.asik” akan diatur oleh Controller class=”
com.firstProgram.web.HelloController”
- Web Server akan membaca class
“com.firstProgram.web.HelloController”
, pada class HelloController, kita sudah mendeklarasikan untuk menampilkan file pages/hello.jsp. Maka Web Server akan menampilkan pages/hello.jsp kepada Anda (client).
Coba anda perhatikan, jika anda ingin membuat banyak page, pasti akan sangat merepotkan jika harus mendeklarasikan satu persatu file yang anda buat ke dalam tjong-servlet.xml (Khusus untuk controler, anda harus mendeklarasikan satu persatu Controller untuk page yang anda buat). Untuk mengatasi hal tersebut – Spring telah mempersiapkan beberapa optional untuk mengatur konfigurasi Controller dan View – silahkan baca (http://static.springsource.org/spring/docs/2.5.x/reference/view.html) jika ingin mengetahui lebih lanjut. Untuk contoh pada artikel ini saya akan mencoba menggunakan class org.springframework.web.servlet.view.ResourceBundleViewResolver.
Langsung ke contoh saja, silahkan ikuti langkah2 berikut:
- Tambahkan javax.servlet-jstl.jar ke folder lib. link bisa diambil di sini.
- Edit tjong-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- the application context definition for the springapp DispatcherServlet --> <!-- Contoh Pada Episode 1 --> <!-- <bean name="/hello.asik" /> --> <!-- Contoh Pada Episode 2 --> <context:component-scan base-package="com.firstProgram.web"/> <!--Kesalahan pada contoh Gery, see the comment below--> <!--<bean id="viewResolver" p:basename="views"/>--> <bean id="viewResolver" p:basename="view"/> </beans>
*edited 21 March 2011: thanks to Gery
- buat Source Folder (selanjutnya untuk file-file configurasi sebaiknya disatukan saja di dalam folder ini) – pada saat build akan di-compile ke dalam Classpath. Caranya klik kanan nama Project – New – Source Folder. Beri nama resources
- buat file view.propertiesdalam folder resources yang baru saja anda buat.
pages/hello.url = /WEB-INF/pages/hello.jsp pages/hello.class= org.springframework.web.servlet.view.JstlView
- pindahkan folder pages menjadi di dalam folder WEB-INF. (Untuk alas an keamanan).
- Edit HelloController.java
package com.firstProgram.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Controller public class HelloController{ @RequestMapping (value="/hello.asik", method=RequestMethod.GET) public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { return new ModelAndView("pages/hello"); } }
- Testing aplikasi anda. http://localhost:8080/firstProgram/hello.asik. Seharusnya tidak ada perubahan pada tampilan nya, yang berubah hany konfigurasi di belakang layarnya saja.