From aea2aafe06a9aa3416fc59aff60f6ea9f86d8fd0 Mon Sep 17 00:00:00 2001 From: Roshan Thomas Date: Fri, 11 Mar 2016 22:43:49 -0500 Subject: [PATCH 1/4] Content Negotiation Strategy --- spring-mvc-java/pom.xml | 13 +++++ .../java/org/baeldung/model/Employee.java | 51 +++++++++++++++++ .../config/ContentManagementWebConfig.java | 53 ++++++++++++++++++ .../web/controller/EmployeeController.java | 55 +++++++++++++++++++ .../main/webapp/WEB-INF/view/employeeHome.jsp | 33 +++++++++++ .../main/webapp/WEB-INF/view/employeeView.jsp | 24 ++++++++ 6 files changed, 229 insertions(+) create mode 100644 spring-mvc-java/src/main/java/org/baeldung/model/Employee.java create mode 100644 spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ContentManagementWebConfig.java create mode 100644 spring-mvc-java/src/main/java/org/baeldung/web/controller/EmployeeController.java create mode 100644 spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp create mode 100644 spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 694a21d5d2..d1e24a4d3d 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -99,6 +99,19 @@ thymeleaf ${thymeleaf.version} + + + + com.fasterxml.jackson.core + jackson-core + 2.1.2 + + + com.fasterxml.jackson.core + jackson-databind + 2.1.2 + + diff --git a/spring-mvc-java/src/main/java/org/baeldung/model/Employee.java b/spring-mvc-java/src/main/java/org/baeldung/model/Employee.java new file mode 100644 index 0000000000..5365068a89 --- /dev/null +++ b/spring-mvc-java/src/main/java/org/baeldung/model/Employee.java @@ -0,0 +1,51 @@ +package org.baeldung.model; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class Employee { + + private long id; + private String name; + private String contactNumber; + + public Employee() { + super(); + } + + public Employee(final long id, final String name, final String contactNumber) { + this.id = id; + this.name = name; + this.contactNumber = contactNumber; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getContactNumber() { + return contactNumber; + } + + public void setContactNumber(final String contactNumber) { + this.contactNumber = contactNumber; + } + + @Override + public String toString() { + return "Employee [id=" + id + ", name=" + name + ", contactNumber=" + contactNumber + "]"; + } + +} diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ContentManagementWebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ContentManagementWebConfig.java new file mode 100644 index 0000000000..6fd68394ea --- /dev/null +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ContentManagementWebConfig.java @@ -0,0 +1,53 @@ +package org.baeldung.spring.web.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; + +@EnableWebMvc +@Configuration +public class ContentManagementWebConfig extends WebMvcConfigurerAdapter { + + public ContentManagementWebConfig() { + super(); + } + + // API + + @Override + public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { + configurer.favorPathExtension(true). + favorParameter(false). + parameterName("mediaType"). + ignoreAcceptHeader(true). + useJaf(false). + defaultContentType(MediaType.TEXT_HTML). + mediaType("xml", MediaType.APPLICATION_XML). + mediaType("html", MediaType.TEXT_HTML). + mediaType("json", MediaType.APPLICATION_JSON); + } + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); + registry.addViewController("/sample.html"); + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + bean.setOrder(0); + return bean; + } + +} diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/EmployeeController.java b/spring-mvc-java/src/main/java/org/baeldung/web/controller/EmployeeController.java new file mode 100644 index 0000000000..8a41718b8c --- /dev/null +++ b/spring-mvc-java/src/main/java/org/baeldung/web/controller/EmployeeController.java @@ -0,0 +1,55 @@ +package org.baeldung.web.controller; + +import java.util.HashMap; +import java.util.Map; + +import org.baeldung.model.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.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; + +@Controller +public class EmployeeController { + + Map employeeMap = new HashMap<>(); + + @RequestMapping(value = "/employee", method = RequestMethod.GET) + public ModelAndView showForm() { + return new ModelAndView("employeeHome", "employee", new Employee()); + } + + @RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET) + public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) { + return employeeMap.get(Id); + } + + @RequestMapping(value = "/employee/{Id}", method = RequestMethod.GET) + public String getEmployeeByIdHtmlView(@PathVariable final long Id, final ModelMap model) { + model.addAttribute("name", employeeMap.get(Id).getName()); + model.addAttribute("contactNumber", employeeMap.get(Id).getContactNumber()); + model.addAttribute("id", employeeMap.get(Id).getId()); + + return "employeeView"; + } + + @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) + public String submit(@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()); + + employeeMap.put(employee.getId(), employee); + + return "employeeView"; + } + +} diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp new file mode 100644 index 0000000000..c000bea39f --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp @@ -0,0 +1,33 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + + + +Form Example - Register an Employee + + +

Welcome, Enter The Employee Details

+ + + + + + + + + + + + + + + + + + +
Name
Id
Contact Number
+
+ + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp new file mode 100644 index 0000000000..1457bc5fc8 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.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 From 800285f30ce556f8b788ae4d0f5f9c55b8e9b486 Mon Sep 17 00:00:00 2001 From: Roshan Thomas Date: Fri, 11 Mar 2016 22:54:45 -0500 Subject: [PATCH 2/4] Content Negotiation Strategy --- spring-mvc-xml/pom.xml | 12 ++++ .../com/baeldung/spring/form/Employee.java | 60 ++++++++++--------- .../contentManagementWebMvcConfig.xml | 43 +++++++++++++ 3 files changed, 86 insertions(+), 29 deletions(-) create mode 100644 spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml diff --git a/spring-mvc-xml/pom.xml b/spring-mvc-xml/pom.xml index fc7deaad61..50cafb44b5 100644 --- a/spring-mvc-xml/pom.xml +++ b/spring-mvc-xml/pom.xml @@ -98,6 +98,18 @@ test + + + com.fasterxml.jackson.core + jackson-core + 2.1.2 + + + com.fasterxml.jackson.core + jackson-databind + 2.1.2 + + diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java index f41496212f..1fc7b82123 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java @@ -2,46 +2,48 @@ package com.baeldung.spring.form; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlRootElement; +@XmlRootElement public class Employee { - private long id; + private long id; - @NotNull - @Size(min = 1) - private String name; - @NotNull - @Size(min = 1) - private String contactNumber; + @NotNull + @Size(min = 1) + private String name; + @NotNull + @Size(min = 1) + private String contactNumber; - public Employee() { - super(); - } + public Employee() { + super(); + } - // + // - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(final String name) { - this.name = name; - } + public void setName(final String name) { + this.name = name; + } - public long getId() { - return id; - } + public long getId() { + return id; + } - public void setId(final long id) { - this.id = id; - } + public void setId(final long id) { + this.id = id; + } - public String getContactNumber() { - return contactNumber; - } + public String getContactNumber() { + return contactNumber; + } - public void setContactNumber(final String contactNumber) { - this.contactNumber = contactNumber; - } + public void setContactNumber(final String contactNumber) { + this.contactNumber = contactNumber; + } } diff --git a/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml b/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml new file mode 100644 index 0000000000..0fcd85e399 --- /dev/null +++ b/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From b9ddf89745a8f0692d70f2da746e91401a0b6acd Mon Sep 17 00:00:00 2001 From: Roshan Thomas Date: Fri, 11 Mar 2016 22:55:41 -0500 Subject: [PATCH 3/4] Content Negotiation Strategy --- .../spring/controller/EmployeeController.java | 54 +++++++++++++------ 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/EmployeeController.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/EmployeeController.java index baffe7b503..753f243edb 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/EmployeeController.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/EmployeeController.java @@ -1,34 +1,56 @@ package com.baeldung.spring.controller; -import javax.validation.Valid; +import java.util.HashMap; +import java.util.Map; -import com.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.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; +import com.baeldung.spring.form.Employee; + @Controller public class EmployeeController { - @RequestMapping(value = "/employee", method = RequestMethod.GET) - public ModelAndView showForm() { - return new ModelAndView("employeeHome", "employee", new Employee()); - } + Map employeeMap = new HashMap<>(); - @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"; - } + @RequestMapping(value = "/employee", method = RequestMethod.GET) + public ModelAndView showForm() { + return new ModelAndView("employeeHome", "employee", new Employee()); + } - model.addAttribute("name", employee.getName()); - model.addAttribute("contactNumber", employee.getContactNumber()); - model.addAttribute("id", employee.getId()); - return "employeeView"; - } + @RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET) + public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) { + return employeeMap.get(Id); + } + + @RequestMapping(value = "/employee/{Id}", method = RequestMethod.GET) + public String getEmployeeByIdHtmlView(@PathVariable final long Id, final ModelMap model) { + model.addAttribute("name", employeeMap.get(Id).getName()); + model.addAttribute("contactNumber", employeeMap.get(Id).getContactNumber()); + model.addAttribute("id", employeeMap.get(Id).getId()); + + return "employeeView"; + } + + @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) + public String submit(@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()); + + employeeMap.put(employee.getId(), employee); + + return "employeeView"; + } } From 5e6778f5a74284ce02501bf9ef30a964b67f096c Mon Sep 17 00:00:00 2001 From: Roshan Thomas Date: Fri, 11 Mar 2016 23:01:45 -0500 Subject: [PATCH 4/4] Formatting --- .../com/baeldung/spring/form/Employee.java | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java index 1fc7b82123..66b2e9f185 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java @@ -7,43 +7,43 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Employee { - private long id; + private long id; - @NotNull - @Size(min = 1) - private String name; - @NotNull - @Size(min = 1) - private String contactNumber; + @NotNull + @Size(min = 1) + private String name; + @NotNull + @Size(min = 1) + private String contactNumber; - public Employee() { - super(); - } + public Employee() { + super(); + } - // + // - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(final String name) { - this.name = name; - } + public void setName(final String name) { + this.name = name; + } - public long getId() { - return id; - } + public long getId() { + return id; + } - public void setId(final long id) { - this.id = id; - } + public void setId(final long id) { + this.id = id; + } - public String getContactNumber() { - return contactNumber; - } + public String getContactNumber() { + return contactNumber; + } - public void setContactNumber(final String contactNumber) { - this.contactNumber = contactNumber; - } + public void setContactNumber(final String contactNumber) { + this.contactNumber = contactNumber; + } }