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