Move A Quick Guide to Spring MVC Matrix Variables
This commit is contained in:
@@ -8,7 +8,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
### Relevant Articles:
|
||||
- [Integration Testing in Spring](https://www.baeldung.com/integration-testing-in-spring)
|
||||
- [A Quick Guide to Spring MVC Matrix Variables](https://www.baeldung.com/spring-mvc-matrix-variables)
|
||||
- [File Upload with Spring MVC](https://www.baeldung.com/spring-file-upload)
|
||||
- [Introduction to HtmlUnit](https://www.baeldung.com/htmlunit)
|
||||
- [Upload and Display Excel Files with Spring MVC](https://www.baeldung.com/spring-mvc-excel-files)
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
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 + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement
|
||||
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, final String workingArea) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.contactNumber = contactNumber;
|
||||
this.workingArea = workingArea;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 + ", workingArea=" + workingArea + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
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.MatrixVariable;
|
||||
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.model.Company;
|
||||
|
||||
@Controller
|
||||
public class CompanyController {
|
||||
|
||||
Map<Long, Company> 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<Map<String, String>> getEmployeeDataFromCompany(@MatrixVariable(pathVar = "employee") final Map<String, String> matrixVars) {
|
||||
return new ResponseEntity<>(matrixVars, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/companyData/{company}/employeeData/{employee}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public ResponseEntity<Map<String, String>> getCompanyName(@MatrixVariable(value = "name", pathVar = "company") final String name) {
|
||||
final Map<String, String> result = new HashMap<String, String>();
|
||||
result.put("name", name);
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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.MatrixVariable;
|
||||
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.SessionAttributes;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.baeldung.model.Employee;
|
||||
|
||||
@SessionAttributes("employees")
|
||||
@Controller
|
||||
public class EmployeeController {
|
||||
|
||||
Map<Long, Employee> 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) {
|
||||
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("workingArea", employee.getWorkingArea());
|
||||
model.addAttribute("id", employee.getId());
|
||||
|
||||
employeeMap.put(employee.getId(), employee);
|
||||
|
||||
return "employeeView";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/employees/{name}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public ResponseEntity<List<Employee>> getEmployeeByNameAndBeginContactNumber(@PathVariable final String name, @MatrixVariable final String beginContactNumber) {
|
||||
final List<Employee> employeesList = new ArrayList<Employee>();
|
||||
for (final Map.Entry<Long, Employee> 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<List<Employee>> getEmployeeBycontactNumber(@MatrixVariable(required = true) final String contactNumber) {
|
||||
final List<Employee> employeesList = new ArrayList<Employee>();
|
||||
for (final Map.Entry<Long, Employee> 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<Map<String, String>> getEmployeeData(@MatrixVariable final Map<String, String> matrixVars) {
|
||||
return new ResponseEntity<>(matrixVars, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "employeeArea/{workingArea}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public ResponseEntity<List<Employee>> getEmployeeByWorkingArea(@MatrixVariable final Map<String, LinkedList<String>> matrixVars) {
|
||||
final List<Employee> employeesList = new ArrayList<Employee>();
|
||||
final LinkedList<String> workingArea = matrixVars.get("workingArea");
|
||||
for (final Map.Entry<Long, Employee> 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
|
||||
pageEncoding="ISO-8859-1" %>
|
||||
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Form Example - Register a Company</title>
|
||||
</head>
|
||||
<body>
|
||||
<h3>Welcome, Enter The Company Details</h3>
|
||||
|
||||
<form:form method="POST" action="/spring-mvc-java/addCompany"
|
||||
modelAttribute="company">
|
||||
<table>
|
||||
<tr>
|
||||
<td><form:label path="name">Name</form:label></td>
|
||||
<td><form:input path="name"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="id">Id</form:label></td>
|
||||
<td><form:input path="id"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input type="submit" value="Submit"/></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form:form>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,20 +0,0 @@
|
||||
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
|
||||
<html>
|
||||
<head>
|
||||
<title>Spring MVC Form Handling</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>Submitted Company Information</h2>
|
||||
<table>
|
||||
<tr>
|
||||
<td>Name :</td>
|
||||
<td>${name}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>ID :</td>
|
||||
<td>${id}</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,37 +0,0 @@
|
||||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
|
||||
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Form Example - Register an Employee</title>
|
||||
</head>
|
||||
<body>
|
||||
<h3>Welcome, Enter The Employee Details</h3>
|
||||
|
||||
<form:form method="POST" action="/spring-mvc-basics/addEmployee" modelAttribute="employee">
|
||||
<table>
|
||||
<tr>
|
||||
<td><form:label path="name">Name</form:label></td>
|
||||
<td><form:input path="name" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="id">Id</form:label></td>
|
||||
<td><form:input path="id" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="contactNumber">Contact Number</form:label></td>
|
||||
<td><form:input path="contactNumber" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="workingArea">Working Area</form:label></td>
|
||||
<td><form:input path="workingArea" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input type="submit" value="Submit" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form:form>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,24 +0,0 @@
|
||||
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
|
||||
<html>
|
||||
<head>
|
||||
<title>Spring MVC Form Handling</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>Submitted Employee Information</h2>
|
||||
<table>
|
||||
<tr>
|
||||
<td>Name :</td>
|
||||
<td>${name}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>ID :</td>
|
||||
<td>${id}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Contact Number :</td>
|
||||
<td>${contactNumber}</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.spring.web.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = WebConfig.class)
|
||||
public class EmployeeMvcIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEmployeeGETisPerformed_thenRetrievedStatusAndViewNameAndAttributeAreCorrect() throws Exception {
|
||||
mockMvc.perform(get("/employee")).andExpect(status().isOk()).andExpect(view().name("employeeHome")).andExpect(model().attributeExists("employee")).andDo(print());
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
import com.baeldung.model.Employee;
|
||||
import com.baeldung.spring.web.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = WebConfig.class)
|
||||
public class EmployeeNoMvcIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private EmployeeController employeeController;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
employeeController.initEmployees();
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Test
|
||||
public void whenInitEmployees_thenVerifyValuesInitiation() {
|
||||
Employee employee1 = employeeController.employeeMap.get(1L);
|
||||
Employee employee2 = employeeController.employeeMap.get(2L);
|
||||
Employee employee3 = employeeController.employeeMap.get(3L);
|
||||
|
||||
Assert.assertTrue(employee1.getId() == 1L);
|
||||
Assert.assertTrue(employee1.getName().equals("John"));
|
||||
Assert.assertTrue(employee1.getContactNumber().equals("223334411"));
|
||||
Assert.assertTrue(employee1.getWorkingArea().equals("rh"));
|
||||
|
||||
Assert.assertTrue(employee2.getId() == 2L);
|
||||
Assert.assertTrue(employee2.getName().equals("Peter"));
|
||||
Assert.assertTrue(employee2.getContactNumber().equals("22001543"));
|
||||
Assert.assertTrue(employee2.getWorkingArea().equals("informatics"));
|
||||
|
||||
Assert.assertTrue(employee3.getId() == 3L);
|
||||
Assert.assertTrue(employee3.getName().equals("Mike"));
|
||||
Assert.assertTrue(employee3.getContactNumber().equals("223334411"));
|
||||
Assert.assertTrue(employee3.getWorkingArea().equals("admin"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user