Belajar Spring MVC Post dan GET Method


Belajar Spring MVC  POST Method dan Spring MVC GET Method

1. Pertama, Ikuti article ini https://undebugable.wordpress.com/2013/07/10/memulai-spring-mvc-dengan-maven-dan-eclipse/ hingga point no 8 untuk membuat project kosong spring mvc dengan maven (nama package dan yang lain sesuaikan sendiri).

2. package milik saya com.ysf.getpost, anda mungkin membuat yang berbeda.

3. webapp/WEB-INF/web.xml :

<web-app id=”WebApp_ID” version=”2.4″
xmlns=”http://java.sun.com/xml/ns/j2ee&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”&gt;

<display-name>Spring MVC Form Handling</display-name>

<servlet>
<servlet-name>HelloWeb</servlet-name> <!– menjelasakan tentang HelloWeb-servlet.xml –>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

4. webapp/WEB-INF/HelloWeb-servlet.xml :

<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:context=”http://www.springframework.org/schema/context&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd”&gt;

<context:component-scan base-package=”com.ysf.getpost” /> <!– mungkin package anda berbeda?maka seuaikan! –>

<bean>
<property name=”prefix” value=“/WEB-INF/jsp/” /><!– menjelaskan folder jsp yang dipakai –>
<property name=”suffix” value=”.jsp” />
</bean>

</beans>

5.  com/ysf/getpost/Student.java

package com.ysf.getpost;

public class Student {

private Integer age;
private String name;
private Integer id;

public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}

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

6. com/ysf/getpost/StudentController.java

package com.ysf.getpost;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;

@Controller
public class StudentController {

@RequestMapping(value = “/student”, method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView(“student”, “command”, new Student());
}

@RequestMapping(value = “/addStudent”, method = RequestMethod.POST)
public String addStudent(@ModelAttribute(“SpringWeb”)Student student,   ModelMap model) {
model.addAttribute(“name”, student.getName());
model.addAttribute(“age”, student.getAge());
model.addAttribute(“id”, student.getId());

return “result”;
}
}

7. Buat folder jsp di bawah WEB-INF

8. buat WEBINF/jsp/student.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Student Information</h2>
<form:form method="POST" action="/BelajarGetPost/addStudent"> <!-- Sesuaikan Mungkin milik anda beda karena perbedaan nama project-->
   <table>
    <tr>
        <td><form:label path="name">Name</form:label></td>
        <td><form:input path="name" /></td>
    </tr>
    <tr>
        <td><form:label path="age">Age</form:label></td>
        <td><form:input path="age" /></td>
    </tr>
    <tr>
        <td><form:label path="id">id</form:label></td>
        <td><form:input path="id" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table>  
</form:form>
</body>
</html>
9. Buat WEBINF/jsp/result.jsp
 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Submitted Student Information</h2>
   <table>
    <tr>
        <td>Name</td>
        <td>${name}</td>
    </tr>
    <tr>
        <td>Age</td>
        <td>${age}</td>
    </tr>
    <tr>
        <td>ID</td>
        <td>${id}</td>
    </tr>
</table>  
</body>
</html>
 
Demikian.

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