memulai belajar spring mvc framework

Saya lagi belajar spring mvc

Bismillah

q> Sebelumnya sudah membuat maven project disini
https://saifiahmada.wordpress.com/2014/12/01/membuat-project-maven-menggunakan-sts/

q> dengan menggunakan project belajar-maven, pada link di atas, lanjut melangkah🙂

q> Edit pom.xml

q> Tambahin ini

<properties>
 <org.springframework.version>4.0.2.RELEASE</org.springframework.version>
 </properties>

q> Buka websitenya spring untuk dependency spring
http://spring.io/blog/2009/12/02/obtaining-spring-3-artifacts-with-maven/

q> Scroll ke bawah sampai ketemu tag xml dependence

q> Copy paste ke pom.xml,

<dependencies>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- Expression Language (depends on spring-core) Define this if you use
 Spring Expression APIs (org.springframework.expression.*) -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-expression</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define
 this if you use Spring Bean APIs (org.springframework.beans.*) -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-beans</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core,
 spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-aop</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- Application Context (depends on spring-core, spring-expression, spring-aop,
 spring-beans) This is the central artifact for Spring's Dependency Injection
 Container and is generally always defined -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- Various Application Context utilities, including EhCache, JavaMail,
 Quartz, and Freemarker integration Define this if you need any of these integrations -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context-support</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- Transaction Management Abstraction (depends on spring-core, spring-beans,
 spring-aop, spring-context) Define this if you use Spring Transactions or
 DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-tx</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context,
 spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jdbc</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA,
 and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx)
 Define this if you need ORM (org.springframework.orm.*) -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-orm</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB,
 JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans,
 spring-context) Define this if you need OXM (org.springframework.oxm.*) -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-oxm</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- Web application development utilities applicable to both Servlet and
 Portlet Environments (depends on spring-core, spring-beans, spring-context)
 Define this if you use Spring MVC, or wish to use Struts, JSF, or another
 web framework with Spring (org.springframework.web.*) -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-web</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans,
 spring-context, spring-web) Define this if you use Spring MVC with a Servlet
 Container such as Apache Tomcat (org.springframework.web.servlet.*) -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans,
 spring-context, spring-web) Define this if you use Spring MVC with a Portlet
 Container (org.springframework.web.portlet.*) -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc-portlet</artifactId>
 <version>${org.springframework.version}</version>
 </dependency>

 <!-- Support for testing Spring applications with tools such as JUnit and
 TestNG This artifact is generally always defined with a 'test' scope for
 the integration testing framework and unit testing stubs -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-test</artifactId>
 <version>${org.springframework.version}</version>
 <scope>test</scope>
 </dependency>
 </dependencies>

q> Simpan file pom.xml, maka eclipse ( versi : STS ) akan melakukan donwload terhadap library spring.

q> Setelah pom.xml disimpan maka eclipse STS ini akan mendownloadkan library yang ada diperlukan

Selection_575

q> create file web.xml untuk project, klik kanan pada Deployment Descriptor > Generate Deployment Descriptor Stub

Selection_576

q> Pada gambar terlihat web.xml pada folder WEB-INF

q> Buka dokumentasi spring
Selection_572

q> Pada halaman dokumentasi referense Cari text dengan CTRL+F ketikkan dispatcher
Selection_573

q> Cari
Selection_574


<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/example/*</url-pattern>
</servlet-mapping>
</web-app>

q> Copy dan paste ke web.xml, kemudian ubah example menjadi dispatcher

q>web.xml sekarang menjadi


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>belajar-maven</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.htm</url-pattern>
<url-pattern>*.json</url-pattern>
<url-pattern>*.xml</url-pattern>
</servlet-mapping>
</web-app>

q> Buat file dispatcher-servlet.xml, nama file dispatcher ini harus sama dengan nilai servlet-name pada web.xml

q> Pada eclipse STS menu File > New > Other > Ketik spring bean
Selection_579

q> Klik Next
Selection_578

q> Klik Finish

Selection_580

q> Tambahkan namespace pada dispatcher-servlet.xml

Selection_581

q> File dispatcher-servlet.xml sekarang :


<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.saifiahmada.maven.controller" />
</beans>

q> Buat package com.saifiahmada.maven.controller serta buat Class IndexController

Selection_584

q> File IndexController.java


package com.saifiahmada.maven.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

 @RequestMapping("/index")
 public String index(){
 return "/WEB-INF/jsp/index.jsp";
 }

}

q> Buat index.jsp ,, klik kanan pada folder WEB-INF > New > JSP File
Selection_583

 

q> Pada file index.jsp , tambah tulisan hello dunia. spring mvc

q> Jalankan lagi jetty, kemudian akses localhost:8080/index.html

Selection_585

q> Sudah berhasil menampilkan hello dunia

Alhamdulillah

 

Published by

saifiahmada

Begin a big Process with a bit trying

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