From a243105b01a2ac9e26189da8078d4c2c44200868 Mon Sep 17 00:00:00 2001 From: Roshan Thomas Date: Sun, 13 Mar 2016 20:21:35 -0400 Subject: [PATCH 1/6] Removing HTML media type --- .../config/ContentManagementWebConfig.java | 59 +++++++++---------- .../web/controller/EmployeeController.java | 9 --- .../spring/controller/EmployeeController.java | 9 --- .../contentManagementWebMvcConfig.xml | 7 +-- 4 files changed, 32 insertions(+), 52 deletions(-) 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 index 6fd68394ea..31e223260e 100644 --- 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 @@ -15,39 +15,38 @@ import org.springframework.web.servlet.view.JstlView; @Configuration public class ContentManagementWebConfig extends WebMvcConfigurerAdapter { - public ContentManagementWebConfig() { - super(); - } + public ContentManagementWebConfig() { + super(); + } - // API + // 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 configureContentNegotiation(final ContentNegotiationConfigurer configurer) { + configurer.favorPathExtension(false). + favorParameter(true). + parameterName("mediaType"). + ignoreAcceptHeader(true). + useJaf(false). + defaultContentType(MediaType.APPLICATION_JSON). + mediaType("xml", MediaType.APPLICATION_XML). + mediaType("json", MediaType.APPLICATION_JSON); + } - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); - registry.addViewController("/sample.html"); - } + @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; - } + @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 index 8a41718b8c..e18bbdbf63 100644 --- 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 @@ -29,15 +29,6 @@ public class EmployeeController { 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()) { 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 753f243edb..eb069ebf0a 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 @@ -30,15 +30,6 @@ public class EmployeeController { 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()) { diff --git a/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml b/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml index 0fcd85e399..49a5e7c35d 100644 --- a/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml @@ -23,16 +23,15 @@ - - + + - + - From 12ce3ee247444bec034543bda0da8e4727d2143b Mon Sep 17 00:00:00 2001 From: Roshan Thomas Date: Sun, 13 Mar 2016 20:26:59 -0400 Subject: [PATCH 2/6] Revert "Removing HTML media type" This reverts commit a243105b01a2ac9e26189da8078d4c2c44200868. --- .../config/ContentManagementWebConfig.java | 59 ++++++++++--------- .../web/controller/EmployeeController.java | 9 +++ .../spring/controller/EmployeeController.java | 9 +++ .../contentManagementWebMvcConfig.xml | 7 ++- 4 files changed, 52 insertions(+), 32 deletions(-) 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 index 31e223260e..6fd68394ea 100644 --- 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 @@ -15,38 +15,39 @@ import org.springframework.web.servlet.view.JstlView; @Configuration public class ContentManagementWebConfig extends WebMvcConfigurerAdapter { - public ContentManagementWebConfig() { - super(); - } + public ContentManagementWebConfig() { + super(); + } - // API + // API - @Override - public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { - configurer.favorPathExtension(false). - favorParameter(true). - parameterName("mediaType"). - ignoreAcceptHeader(true). - useJaf(false). - defaultContentType(MediaType.APPLICATION_JSON). - mediaType("xml", MediaType.APPLICATION_XML). - mediaType("json", MediaType.APPLICATION_JSON); - } + @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"); - } + @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; - } + @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 index e18bbdbf63..8a41718b8c 100644 --- 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 @@ -29,6 +29,15 @@ public class EmployeeController { 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()) { 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 eb069ebf0a..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 @@ -30,6 +30,15 @@ public class EmployeeController { 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()) { diff --git a/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml b/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml index 49a5e7c35d..0fcd85e399 100644 --- a/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml @@ -23,15 +23,16 @@ - - + + - + + From 67ae14e8d41e0e34a0271b4e9a37f6a729eb851a Mon Sep 17 00:00:00 2001 From: RoshanThomas Date: Sun, 13 Mar 2016 20:29:38 -0400 Subject: [PATCH 3/6] Removing HTML media type --- .../spring/web/config/ContentManagementWebConfig.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) 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 index 6fd68394ea..2c5b423029 100644 --- 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 @@ -23,14 +23,13 @@ public class ContentManagementWebConfig extends WebMvcConfigurerAdapter { @Override public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { - configurer.favorPathExtension(true). - favorParameter(false). + configurer.favorPathExtension(false). + favorParameter(true). parameterName("mediaType"). ignoreAcceptHeader(true). useJaf(false). - defaultContentType(MediaType.TEXT_HTML). + defaultContentType(MediaType.APPLICATION_JSON). mediaType("xml", MediaType.APPLICATION_XML). - mediaType("html", MediaType.TEXT_HTML). mediaType("json", MediaType.APPLICATION_JSON); } From 66f34a45035ee90e78070db62f2121701ed572ec Mon Sep 17 00:00:00 2001 From: RoshanThomas Date: Sun, 13 Mar 2016 20:30:25 -0400 Subject: [PATCH 4/6] Removing HTML media type --- .../org/baeldung/web/controller/EmployeeController.java | 9 --------- 1 file changed, 9 deletions(-) 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 index 8a41718b8c..e18bbdbf63 100644 --- 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 @@ -29,15 +29,6 @@ public class EmployeeController { 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()) { From 9b0682b2fbad77bbb02e9978318811881478844a Mon Sep 17 00:00:00 2001 From: RoshanThomas Date: Sun, 13 Mar 2016 20:35:54 -0400 Subject: [PATCH 5/6] Removing HTML media type --- .../contentManagementWebMvcConfig.xml | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml b/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml index 0fcd85e399..e68c88d19d 100644 --- a/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/contentManagementWebMvcConfig.xml @@ -13,31 +13,30 @@ - - + class="org.springframework.web.servlet.view.InternalResourceViewResolver"> + + - - - - - - + class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> + + + + + + - - - - - - - + + + + + + - \ No newline at end of file + From b9c58d69bce3e7d4d7f304f431328409373bd975 Mon Sep 17 00:00:00 2001 From: RoshanThomas Date: Sun, 13 Mar 2016 20:38:30 -0400 Subject: [PATCH 6/6] Removing HTML media type --- .../spring/controller/EmployeeController.java | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 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 753f243edb..aa25f47a2a 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 @@ -22,35 +22,24 @@ public class EmployeeController { @RequestMapping(value = "/employee", method = RequestMethod.GET) public ModelAndView showForm() { - return new ModelAndView("employeeHome", "employee", new Employee()); + 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"; + return employeeMap.get(Id); } @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"; + 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"; } }