Archive

Archive for April, 2014

Ctrl + O Untuk Show Class Details ( Eclipse Shortcut )

April 23, 2014 Leave a comment

Shortcuts CTRL + O pada editor eclipse menunjukkan list semua varibale dan method pada current class. Jika ditekan sekali lagi menunjukkan juga list inheritedĀ  method dari parent class. Print screen dari editor saya di class ExceptionController.

CTRL + O

CTRL + O

Semoga bermanfaat. šŸ™‚

Advertisements

Spring MVC Exception Handling Dengan @ControllerAdvice Annotation

April 22, 2014 Leave a comment

Spring MVC menyediakan cara yang mudah untuk meng-handle exception dan error. @ExceptionHandler annotation merupakan core untuk feature handling exception dan error. Untuk setiap controller kita dapat mendefinisikan sebuah metode yang secara otomatis dipanggil jikaĀ exception dan error pada controller terjadi.

Contoh.

@ExceptionHandler(ArithmeticException.class)
public String getArithmeticEception(ArithmeticException e) {
/* Login in here */
return "error";
}

Method tersebut akan dieksekusi saat exception dengan type ArithmeticException terjadi di controller dimana method tersebut didefinisikan. Untuk membuat method tersebut dieksekusi untuk exception dengan type ArithmeticException yang terjadi di semua controller makan digunakan @ControllerAdvice annotation.

Berikut ini example lengkapnya.

Langkah 1. Buat JSP view untuk memilih jenis exception ( ExceptionPage.jsp )

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Exception Page</title>
</head>
<body>
Exception Example <br>
<a href="SQLException.do">SQLException Example</a> <br>
<a href="<%=request.getContextPath()%>/RuntimeException.do">RuntimeException Example</a> <br>
<a href="<%=request.getContextPath()%>/ArithmeticException.do">ArithmeticException Example</a>
</body>
</html>

Exception Page

Exception Page

Buat JSP view untuk error message ( ExceptionErrorPage.jsp )

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Exception Error Page</title>
</head>
<body>
<h2>Ops! Something went wrong</h2>
<h2><b>${errorMessage}</b></h2>
</body>
</html>

Exception Error Message

Exception Error Message

Langkah 2 : Buat Spring controller ( ExceptionController.java )

import java.sql.SQLException;

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

@Controller
public class ExceptionController {
public boolean exceptionFlag;
public String errorMessage;

@RequestMapping(value="/SQLException.do", method= RequestMethod.GET)
public String testSQLException() throws SQLException{
exceptionFlag = true;
errorMessageĀ  = "SQL Exception";

if(exceptionFlag == true){
throw new SQLException(errorMessage );
}

return null;
}

@RequestMapping(value="/RuntimeException.do", method= RequestMethod.GET)
public String testRuntimeException() throws RuntimeException{
exceptionFlag = true;
errorMessageĀ  = "Runtime Exception";

if(exceptionFlag == true){
throw new RuntimeException(errorMessage);
}
return null;
}

@RequestMapping(value="/ArithmeticException.do", method= RequestMethod.GET)
public String testArithmeticException() throws ArithmeticException{
exceptionFlag = true;
errorMessageĀ  = "Arithmetic Exception";

if(exceptionFlag == true){
throw new ArithmeticException(errorMessage);
}
return null;
}
}

controller ini mapping request dari link di JSP view yang telah kita buat sebelumnya. Method pada controller ini hanya untuk membuat exception.

Buat Spring controller yang akan meng-handle jika exception terjadi ( ExceptionControllerAdvice.java )

package com.mvc.controller;

import java.sql.SQLException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class ExceptionControllerAdvice {

@ExceptionHandler(SQLException.class)
public ModelAndView getSQLException(SQLException e){

ModelAndView mdv = new ModelAndView("ExceptionErrorPage");
mdv.addObject("errorMessage", e.getMessage());

return mdv;
}

@ExceptionHandler(RuntimeException.class)
public ModelAndView getRuntimexception(RuntimeException e){

ModelAndView mdv = new ModelAndView("ExceptionErrorPage");
mdv.addObject("errorMessage", e.getMessage());

return mdv;
}

@ExceptionHandler(ArithmeticException.class)
public ModelAndView getArithmeticEception(ArithmeticException e){

ModelAndView mdv = new ModelAndView("ExceptionErrorPage");
mdv.addObject("errorMessage", e.getMessage());

return mdv;
}
}


Controller tersebut secara sederhana hanya terdiri @ExceptionHandler dan @ControllerAdvice annotation.

Referensi : dari berbagai sumber.
Semoga bermanfaat

Spring MVC Form Handling

April 18, 2014 Leave a comment

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