diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index e796099190..33d6306c5a 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -194,7 +194,6 @@ cargo-maven2-plugin ${cargo-maven2-plugin.version} - true jetty8x embedded diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/Company.java b/spring-mvc-java/src/main/java/com/baeldung/model/Company.java new file mode 100644 index 0000000000..558507268a --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Company.java @@ -0,0 +1,38 @@ +package com.baeldung.model; + +public class Company { + + private long id; + private String name; + + public Company() { + super(); + } + + public Company(final long id, final String name) { + this.id = id; + this.name = name; + } + + 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; + } + + @Override + public String toString() { + return "Company [id=" + id + ", name=" + name + "]"; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java b/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java index d0f6b724eb..fb0a452219 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java @@ -8,15 +8,17 @@ public class Employee { private long id; private String name; private String contactNumber; + private String workingArea; public Employee() { super(); } - public Employee(final long id, final String name, final String contactNumber) { + public Employee(final long id, final String name, final String contactNumber, final String workingArea) { this.id = id; this.name = name; this.contactNumber = contactNumber; + this.workingArea = workingArea; } public String getName() { @@ -43,9 +45,17 @@ public class Employee { this.contactNumber = contactNumber; } + public String getWorkingArea() { + return workingArea; + } + + public void setWorkingArea(final String workingArea) { + this.workingArea = workingArea; + } + @Override public String toString() { - return "Employee [id=" + id + ", name=" + name + ", contactNumber=" + contactNumber + "]"; + return "Employee [id=" + id + ", name=" + name + ", contactNumber=" + contactNumber + ", workingArea=" + workingArea + "]"; } } diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java index ba7f35fd65..663b9cc4d2 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java @@ -13,12 +13,14 @@ import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; 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; import org.springframework.web.servlet.view.ResourceBundleViewResolver; import org.springframework.web.servlet.view.XmlViewResolver; +import org.springframework.web.util.UrlPathHelper; @Configuration @EnableWebMvc @@ -96,4 +98,12 @@ public class WebConfig extends WebMvcConfigurerAdapter { return list; } + + @Override + public void configurePathMatch(final PathMatchConfigurer configurer) { + final UrlPathHelper urlPathHelper = new UrlPathHelper(); + urlPathHelper.setRemoveSemicolonContent(false); + + configurer.setUrlPathHelper(urlPathHelper); + } } diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/CompanyController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/CompanyController.java new file mode 100644 index 0000000000..8228eafd5c --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/CompanyController.java @@ -0,0 +1,56 @@ +package com.baeldung.web.controller; + +import com.baeldung.model.Company; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +import java.util.HashMap; +import java.util.Map; + +@Controller +public class CompanyController { + + Map companyMap = new HashMap<>(); + + @RequestMapping(value = "/company", method = RequestMethod.GET) + public ModelAndView showForm() { + return new ModelAndView("companyHome", "company", new Company()); + } + + @RequestMapping(value = "/company/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET) + public @ResponseBody Company getCompanyById(@PathVariable final long Id) { + return companyMap.get(Id); + } + + @RequestMapping(value = "/addCompany", method = RequestMethod.POST) + public String submit(@ModelAttribute("company") final Company company, final BindingResult result, final ModelMap model) { + if (result.hasErrors()) { + return "error"; + } + model.addAttribute("name", company.getName()); + model.addAttribute("id", company.getId()); + + companyMap.put(company.getId(), company); + + return "companyView"; + } + + @RequestMapping(value = "/companyEmployee/{company}/employeeData/{employee}", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> getEmployeeDataFromCompany(@MatrixVariable(pathVar = "employee") final Map matrixVars) { + return new ResponseEntity<>(matrixVars, HttpStatus.OK); + } + + @RequestMapping(value = "/companyData/{company}/employeeData/{employee}", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> getCompanyName(@MatrixVariable(value = "name", pathVar = "company") final String name) { + final Map result = new HashMap(); + result.put("name", name); + return new ResponseEntity<>(result, HttpStatus.OK); + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java index 2166c00873..fd5fa6bc27 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java @@ -1,35 +1,40 @@ package com.baeldung.web.controller; -import java.util.HashMap; -import java.util.Map; - +import com.baeldung.model.Employee; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; -import org.springframework.web.bind.annotation.ControllerAdvice; -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.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; -import com.baeldung.model.Employee; +import java.util.*; +@SessionAttributes("employees") @Controller @ControllerAdvice public class EmployeeController { Map employeeMap = new HashMap<>(); + @ModelAttribute("employees") + public void initEmployees() { + employeeMap.put(1L, new Employee(1L, "John", "223334411", "rh")); + employeeMap.put(2L, new Employee(2L, "Peter", "22001543", "informatics")); + employeeMap.put(3L, new Employee(3L, "Mike", "223334411", "admin")); + } + @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) { + @RequestMapping(value = "/employee/{Id}", produces = {"application/json", "application/xml"}, method = RequestMethod.GET) + public + @ResponseBody + Employee getEmployeeById(@PathVariable final long Id) { return employeeMap.get(Id); } @@ -40,6 +45,7 @@ public class EmployeeController { } model.addAttribute("name", employee.getName()); model.addAttribute("contactNumber", employee.getContactNumber()); + model.addAttribute("workingArea", employee.getWorkingArea()); model.addAttribute("id", employee.getId()); employeeMap.put(employee.getId(), employee); @@ -52,4 +58,53 @@ public class EmployeeController { model.addAttribute("msg", "Welcome to the Netherlands!"); } + @RequestMapping(value = "/employees/{name}", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> getEmployeeByNameAndBeginContactNumber(@PathVariable final String name, @MatrixVariable final String beginContactNumber) { + final List employeesList = new ArrayList(); + for (final Map.Entry employeeEntry : employeeMap.entrySet()) { + final Employee employee = employeeEntry.getValue(); + if (employee.getName().equalsIgnoreCase(name) && employee.getContactNumber().startsWith(beginContactNumber)) { + employeesList.add(employee); + } + } + return new ResponseEntity<>(employeesList, HttpStatus.OK); + } + + @RequestMapping(value = "/employeesContacts/{contactNumber}", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> getEmployeeBycontactNumber(@MatrixVariable(required = true) final String contactNumber) { + final List employeesList = new ArrayList(); + for (final Map.Entry employeeEntry : employeeMap.entrySet()) { + final Employee employee = employeeEntry.getValue(); + if (employee.getContactNumber().equalsIgnoreCase(contactNumber)) { + employeesList.add(employee); + } + } + return new ResponseEntity<>(employeesList, HttpStatus.OK); + } + + @RequestMapping(value = "employeeData/{employee}", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> getEmployeeData(@MatrixVariable final Map matrixVars) { + return new ResponseEntity<>(matrixVars, HttpStatus.OK); + } + + @RequestMapping(value = "employeeArea/{workingArea}", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> getEmployeeByWorkingArea(@MatrixVariable final Map> matrixVars) { + final List employeesList = new ArrayList(); + final LinkedList workingArea = matrixVars.get("workingArea"); + for (final Map.Entry employeeEntry : employeeMap.entrySet()) { + final Employee employee = employeeEntry.getValue(); + for (final String area : workingArea) { + if (employee.getWorkingArea().equalsIgnoreCase(area)) { + employeesList.add(employee); + break; + } + } + } + return new ResponseEntity<>(employeesList, HttpStatus.OK); + } + } diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/companyHome.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/companyHome.jsp new file mode 100644 index 0000000000..38ef8d12ee --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/companyHome.jsp @@ -0,0 +1,31 @@ +<%@ 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 a Company + + +

Welcome, Enter The Company Details

+ + + + + + + + + + + + + + +
Name
Id
+
+ + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/companyView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/companyView.jsp new file mode 100644 index 0000000000..8f34059b0a --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/companyView.jsp @@ -0,0 +1,20 @@ +<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> + + + Spring MVC Form Handling + + + +

Submitted Company Information

+ + + + + + + + + +
Name :${name}
ID :${id}
+ + \ No newline at end of file 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 index c000bea39f..8a32fd12b6 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp @@ -22,6 +22,10 @@ Contact Number + + Working Area + + 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 index 9a9b879a35..627a9d9ddb 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp @@ -20,6 +20,10 @@ Contact Number : ${contactNumber} + + Working Area : + ${workingArea} + \ No newline at end of file