Home > Java and Web > Spring MVC Form Handling

Spring MVC Form Handling

Goal post ini membuat form untuk mengambil data user detail dan mem-print data user detail tersebut di page view.

User Form

User Form

User View

User View

Langkah 1 : Buat user form ( RegistrationForm.jsp ) dan user view ( SuccessRegistration.jsp )

RegistrationForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Form</title>
</head>
<body>
<div align="center">
<form:form action="SuccessRegistration.do" method="post" commandName="userForm" >
<table border="0">
<tr>
<td colspan="2" align="center"><h2>Spring MVC Form Demo - Registration</h2></td>
</tr>
<tr>
<td>User Name:</td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td>E-mail:</td>
<td><form:input path="email" /></td>
</tr>

<tr>
<td>Position:</td>
<td><form:select path="position" items="${positionList}" /></td>
</tr>
<tr>
<td>Programming Language:</td>
<td><form:checkboxes path="programmingLanguage" items="${programmingLanguageList}"></form:checkboxes></td>
</tr>
<tr>
<td>Avaiable:</td>
<tr>
<td>Yes</td>
<td><form:radiobutton path="avaiable" value="Yes"></form:radiobutton></td>
</tr>
<tr>
<td>No</td>
<td><form:radiobutton path="avaiable" value="No"></form:radiobutton></td>
</tr>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Register" /></td>
</tr>
</table>
</form:form>
</div>
</body>
</html>

SuccessRegistration.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Form</title>
</head>
<body>
<div align="center">
<form:form action="SuccessRegistration.do" method="post" commandName="userForm" >
<table border="0">
<tr>
<td colspan="2" align="center"><h2>Spring MVC Form Demo - Registration</h2></td>
</tr>
<tr>
<td>User Name:</td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td>E-mail:</td>
<td><form:input path="email" /></td>
</tr>

<tr>
<td>Position:</td>
<td><form:select path="position" items="${positionList}" /></td>
</tr>
<tr>
<td>Programming Language:</td>
<td><form:checkboxes path="programmingLanguage" items="${programmingLanguageList}"></form:checkboxes></td>
</tr>
<tr>
<td>Avaiable:</td>
<tr>
<td>Yes</td>
<td><form:radiobutton path="avaiable" value="Yes"></form:radiobutton></td>
</tr>
<tr>
<td>No</td>
<td><form:radiobutton path="avaiable" value="No"></form:radiobutton></td>
</tr>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Register" /></td>
</tr>
</table>
</form:form>
</div>
</body>
</html>

Langkah 2 : Buat model class / form backing object ( User.java )

package com.mvc.model;

import java.util.List;

public class User {
private String username;
private String password;
private String email;
private String position;
private List<String> programmingLanguage;
private String avaiable;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public List<String> getProgrammingLanguage() {
return programmingLanguage;
}
public void setProgrammingLanguage(List<String> programmingLanguage) {
this.programmingLanguage = programmingLanguage;
}
public String getAvaiable() {
return avaiable;
}
public void setAvaiable(String avaiable) {
this.avaiable = avaiable;
}

}

Langkah 3 : Buat controller class ( RegistrationController.java )

package com.mvc.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mvc.model.User;

@Controller
public class RegistrationController {

@RequestMapping(value="FormRegistration.do", method=RequestMethod.GET)
public String showFormRegistration(ModelMap model){
User userForm = new User();

model.addAttribute("userForm", userForm);

List<String> positionList = new ArrayList<String>();
positionList.add("Team Leader");
positionList.add("System Analysis");
positionList.add("Programmer");
positionList.add("Designer");
model.addAttribute("positionList", positionList);

List<String> programmingLanguageList = new ArrayList<String>();
programmingLanguageList.add("Java");
programmingLanguageList.add("C#");
programmingLanguageList.add("PHP");
programmingLanguageList.add("C++");
model.addAttribute("programmingLanguageList", programmingLanguageList);

return "RegistrationForm";
}

@RequestMapping(value="SuccessRegistration.do",method=RequestMethod.POST)
public String showSuccessRegistration(@ModelAttribute("userForm")User user, ModelMap model){
model.addAttribute("userForm", user);
return "SuccessRegistration";
}

}

Referensi : dari berbagai sumber.
Semoga bermanfaat

  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: