
package com.dineshonjava.emp.bean;
/**
* @author Dinesh Rajput
*
*/
public class Employee {
private int empId;
private String name;
private Long salary;
private int age;
/**
* @return the empId
*/
public int getEmpId() {
return empId;
}
/**
* @param empId the empId to set
*/
public void setEmpId(int empId) {
this.empId = empId;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the salary
*/
public Long getSalary() {
return salary;
}
/**
* @param salary the salary to set
*/
public void setSalary(Long salary) {
this.salary = salary;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
public String toString(){
return "Employee{ name-"+name+" age-"+age+" salary-"+salary+"}";
}
}
EmployeeException.javapackage com.dineshonjava.emp.exception;
/**
* @author Dinesh Rajput
*
*/
public class EmployeeException extends RuntimeException {
private static final long serialVersionUID = -4794572499177930357L;
private String exceptionMsg;
public EmployeeException(String exceptionMsg) {
this.exceptionMsg = exceptionMsg;
}
public String getExceptionMsg(){
return this.exceptionMsg;
}
public void setExceptionMsg(String exceptionMsg) {
this.exceptionMsg = exceptionMsg;
}
}
EmployeeController.javapackage com.dineshonjava.emp.controller;
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 org.springframework.web.servlet.ModelAndView;
import com.dineshonjava.emp.bean.Employee;
import com.dineshonjava.emp.exception.EmployeeException;
/**
* @author Dinesh Rajput
*
*/
@Controller
public class EmployeeController {
@RequestMapping(value = "/employee", method = RequestMethod.GET)
public ModelAndView employee() {
return new ModelAndView("employeeForm", "command", new Employee());
}
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String addEmployee(@ModelAttribute("SpringWeb")Employee employee, ModelMap model) {
if(employee.getName().length() < 5 ){
throw new EmployeeException("Given name is too short");
}else{
model.addAttribute("name", employee.getName());
}
if( employee.getAge() < 10 ){
throw new EmployeeException("Given age is too low");
}else{
model.addAttribute("age", employee.getAge());
}
model.addAttribute("empId", employee.getEmpId());
model.addAttribute("salary", employee.getSalary());
return "employeeDetail";
}
}
Spring Web configuration file web.xml<web-app id="WebApp_ID" version="3.0" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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_3_0.xsd">
<display-name>SpringMVCExceptionApp</display-name>
<welcome-file-list>
<welcome-file>/</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>sdnext</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name><param-value>/WEB-INF/config/sdnext-servlet.xml</param-value></init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sdnext</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Spring Web configuration file sdnext-servlet.xml<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.dineshonjava.emp.controller">
</context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.dineshonjava.emp.exception.EmployeeException">
ExceptionPage
</prop>
</props>
</property>
<property name="defaultErrorView" value="error"></property>
</bean>
</beans>
Here you specified ExceptionPage as an exception view in case EmployeeException occurs, if there is any other type of exception then a generic view error will take place.<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
&body>
Employee Data Form
<form:form action="/sdnext/addEmployee" method="POST">
<table><tbody>
<tr> <td><form:label path="empId">Employee :</form:label></td> <td><form:input path="empId"></form:input></td> </tr>
<tr> <td><form:label path="name">EmployeeName:/form:label></form:label></td> <td><form:input path="name"></form:input></td> </tr>
<tr> <td><form:label path="age">Employee Age:</form:label></td> <td><form:input path="age"></form:input></td> </tr>
<tr> <td><form:label path="salary">Employee Salary:</form:label></td> <td><form:input path="salary"></form:input></td> </tr>
<tr> <td colspan="2"><input type="submit" value="Submit"/> </td> </tr>
</tbody></table>
</form:form>
</body>
</html>
employeeDetail.jsp<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>
<h2>
Submitted Employee Information</h2>
<table border="1"><tbody>
<tr> <td>Employee ID </td> <td>${empId}</td> </tr>
<tr> <td>Employee Name</td> <td>${name}</td> </tr>
<tr> <td>Employee Age</td> <td>${age}</td> </tr>
<tr> <td>Employee Salary</td> <td>${salary}</td> </tr>
</tbody></table>
</body>
</html>
ExceptionPage.jsp<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Spring MVC Exception Handling</title>
</head>
<body>
Spring MVC Exception Handling
${exception.exceptionMsg}
</body>
</html>
error.jsp<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Spring Error Page</title>
</head>
<body>
An error occurred, please contact dineshonjava at admin@dineshonjava.com
</body>
</html>
Once you are done with creating source and configuration files, export your application. Right click on your application and use Export-> WAR File option and save your SpringExceptionApp.war file in Tomcat's webapps folder.






Labels: Spring MVC, Spring3.0