From 1ee5b0580097a781d9d87b387d7aaa99a5b1bf9f Mon Sep 17 00:00:00 2001 From: Dheeraj-Baluja Date: Thu, 17 Jul 2014 01:36:40 +0530 Subject: [PATCH] adding spring form changes --- .../spring/controller/EmployeeController.java | 33 ++++++++++++++++ .../org/baeldung/spring/form/Employee.java | 33 ++++++++++++++++ .../src/main/resources/webMvcConfig.xml | 21 ++++++---- .../webapp/WEB-INF/view/employeeAdded.jsp | 24 ++++++++++++ .../main/webapp/WEB-INF/view/employeeHome.jsp | 38 +++++++++++++++++++ .../src/main/webapp/WEB-INF/view/error.jsp | 20 ++++++++++ .../src/main/webapp/WEB-INF/web.xml | 2 +- spring-mvc-xml/src/main/webapp/index.jsp | 18 +++++++++ 8 files changed, 181 insertions(+), 8 deletions(-) create mode 100644 spring-mvc-xml/src/main/java/org/baeldung/spring/controller/EmployeeController.java create mode 100644 spring-mvc-xml/src/main/java/org/baeldung/spring/form/Employee.java create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeAdded.jsp create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/error.jsp create mode 100644 spring-mvc-xml/src/main/webapp/index.jsp diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/EmployeeController.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/EmployeeController.java new file mode 100644 index 0000000000..0057f68df9 --- /dev/null +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/EmployeeController.java @@ -0,0 +1,33 @@ +package org.baeldung.spring.controller; + +import javax.validation.Valid; + +import org.baeldung.spring.form.Employee; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +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; + + +@Controller +public class EmployeeController { + + @RequestMapping(value = "/employee", method = RequestMethod.GET) + public ModelAndView showForm() { + return new ModelAndView("employeeHome", "employee", new Employee()); + } + + @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) + public String submit(@Valid @ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) { + if (result.hasErrors()) { + return "error"; + } + model.addAttribute("name", employee.getName()); + model.addAttribute("contactNumber", employee.getContactNumber()); + model.addAttribute("id", employee.getId()); + return "employeeAdded"; + } +} diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Employee.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Employee.java new file mode 100644 index 0000000000..7a9f7a4196 --- /dev/null +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Employee.java @@ -0,0 +1,33 @@ +package org.baeldung.spring.form; + +public class Employee { + + private String name; + private long id; + private String contactNumber; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getContactNumber() { + return contactNumber; + } + + public void setContactNumber(String contactNumber) { + this.contactNumber = contactNumber; + } + +} diff --git a/spring-mvc-xml/src/main/resources/webMvcConfig.xml b/spring-mvc-xml/src/main/resources/webMvcConfig.xml index 5f6e26643b..278f6c5533 100644 --- a/spring-mvc-xml/src/main/resources/webMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/webMvcConfig.xml @@ -1,16 +1,23 @@ - - - - - + + + + + + - + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeAdded.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeAdded.jsp new file mode 100644 index 0000000000..1457bc5fc8 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeAdded.jsp @@ -0,0 +1,24 @@ +<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> + + +Spring MVC Form Handling + + + +

Submitted Employee Information

+ + + + + + + + + + + + + +
Name :${name}
ID :${id}
Contact Number :${contactNumber}
+ + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp new file mode 100644 index 0000000000..2f434fb7bd --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp @@ -0,0 +1,38 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + + + + + +SpringMVCExample + + + +

Welcome, Enter The Employee Details

+ + + + + + + + + + + + + + + + + + +
Name
Id
Contact Number
+
+ + + + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/error.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/error.jsp new file mode 100644 index 0000000000..8f3d83af17 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/error.jsp @@ -0,0 +1,20 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +SpringMVCExample + + + +

Pleas enter the correct details

+ + + + +
Retry
+ + + + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index 671813ac90..1a4128fb50 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -39,7 +39,7 @@ 10 - index.html + index.jsp \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/index.jsp b/spring-mvc-xml/src/main/webapp/index.jsp new file mode 100644 index 0000000000..1ecfcec9d7 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/index.jsp @@ -0,0 +1,18 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Spring MVC Examples + + + +

Spring MVC Examples

+ + + + \ No newline at end of file