Skip to content

Commit 70322e1

Browse files
spring-mvc-matrix-variables - manually merging
1 parent a5bde90 commit 70322e1

10 files changed

Lines changed: 242 additions & 15 deletions

File tree

‎spring-mvc-java/pom.xml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@
194194
<artifactId>cargo-maven2-plugin</artifactId>
195195
<version>${cargo-maven2-plugin.version}</version>
196196
<configuration>
197-
<wait>true</wait>
198197
<container>
199198
<containerId>jetty8x</containerId>
200199
<type>embedded</type>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.model;
2+
3+
public class Company {
4+
5+
private long id;
6+
private String name;
7+
8+
public Company() {
9+
super();
10+
}
11+
12+
public Company(final long id, final String name) {
13+
this.id = id;
14+
this.name = name;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(final String name) {
22+
this.name = name;
23+
}
24+
25+
public long getId() {
26+
return id;
27+
}
28+
29+
public void setId(final long id) {
30+
this.id = id;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "Company [id=" + id + ", name=" + name + "]";
36+
}
37+
38+
}

‎spring-mvc-java/src/main/java/com/baeldung/model/Employee.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ public class Employee {
88
private long id;
99
private String name;
1010
private String contactNumber;
11+
private String workingArea;
1112

1213
public Employee() {
1314
super();
1415
}
1516

16-
public Employee(final long id, final String name, final String contactNumber) {
17+
public Employee(final long id, final String name, final String contactNumber, final String workingArea) {
1718
this.id = id;
1819
this.name = name;
1920
this.contactNumber = contactNumber;
21+
this.workingArea = workingArea;
2022
}
2123

2224
public String getName() {
@@ -43,9 +45,17 @@ public void setContactNumber(final String contactNumber) {
4345
this.contactNumber = contactNumber;
4446
}
4547

48+
public String getWorkingArea() {
49+
return workingArea;
50+
}
51+
52+
public void setWorkingArea(final String workingArea) {
53+
this.workingArea = workingArea;
54+
}
55+
4656
@Override
4757
public String toString() {
48-
return "Employee [id=" + id + ", name=" + name + ", contactNumber=" + contactNumber + "]";
58+
return "Employee [id=" + id + ", name=" + name + ", contactNumber=" + contactNumber + ", workingArea=" + workingArea + "]";
4959
}
5060

5161
}

‎spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
1414
import org.springframework.web.servlet.ViewResolver;
1515
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
16+
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
1617
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
1718
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
1819
import org.springframework.web.servlet.view.InternalResourceViewResolver;
1920
import org.springframework.web.servlet.view.JstlView;
2021
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
2122
import org.springframework.web.servlet.view.XmlViewResolver;
23+
import org.springframework.web.util.UrlPathHelper;
2224

2325
@Configuration
2426
@EnableWebMvc
@@ -96,4 +98,12 @@ private List<MediaType> getSupportedMediaTypes() {
9698

9799
return list;
98100
}
101+
102+
@Override
103+
public void configurePathMatch(final PathMatchConfigurer configurer) {
104+
final UrlPathHelper urlPathHelper = new UrlPathHelper();
105+
urlPathHelper.setRemoveSemicolonContent(false);
106+
107+
configurer.setUrlPathHelper(urlPathHelper);
108+
}
99109
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.web.controller;
2+
3+
import com.baeldung.model.Company;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.ModelMap;
8+
import org.springframework.validation.BindingResult;
9+
import org.springframework.web.bind.annotation.*;
10+
import org.springframework.web.servlet.ModelAndView;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
@Controller
16+
public class CompanyController {
17+
18+
Map<Long, Company> companyMap = new HashMap<>();
19+
20+
@RequestMapping(value = "/company", method = RequestMethod.GET)
21+
public ModelAndView showForm() {
22+
return new ModelAndView("companyHome", "company", new Company());
23+
}
24+
25+
@RequestMapping(value = "/company/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
26+
public @ResponseBody Company getCompanyById(@PathVariable final long Id) {
27+
return companyMap.get(Id);
28+
}
29+
30+
@RequestMapping(value = "/addCompany", method = RequestMethod.POST)
31+
public String submit(@ModelAttribute("company") final Company company, final BindingResult result, final ModelMap model) {
32+
if (result.hasErrors()) {
33+
return "error";
34+
}
35+
model.addAttribute("name", company.getName());
36+
model.addAttribute("id", company.getId());
37+
38+
companyMap.put(company.getId(), company);
39+
40+
return "companyView";
41+
}
42+
43+
@RequestMapping(value = "/companyEmployee/{company}/employeeData/{employee}", method = RequestMethod.GET)
44+
@ResponseBody
45+
public ResponseEntity<Map<String, String>> getEmployeeDataFromCompany(@MatrixVariable(pathVar = "employee") final Map<String, String> matrixVars) {
46+
return new ResponseEntity<>(matrixVars, HttpStatus.OK);
47+
}
48+
49+
@RequestMapping(value = "/companyData/{company}/employeeData/{employee}", method = RequestMethod.GET)
50+
@ResponseBody
51+
public ResponseEntity<Map<String, String>> getCompanyName(@MatrixVariable(value = "name", pathVar = "company") final String name) {
52+
final Map<String, String> result = new HashMap<String, String>();
53+
result.put("name", name);
54+
return new ResponseEntity<>(result, HttpStatus.OK);
55+
}
56+
}
Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
package com.baeldung.web.controller;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
5-
3+
import com.baeldung.model.Employee;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
66
import org.springframework.stereotype.Controller;
77
import org.springframework.ui.Model;
88
import org.springframework.ui.ModelMap;
99
import org.springframework.validation.BindingResult;
10-
import org.springframework.web.bind.annotation.ControllerAdvice;
11-
import org.springframework.web.bind.annotation.ModelAttribute;
12-
import org.springframework.web.bind.annotation.PathVariable;
13-
import org.springframework.web.bind.annotation.RequestMapping;
14-
import org.springframework.web.bind.annotation.RequestMethod;
15-
import org.springframework.web.bind.annotation.ResponseBody;
10+
import org.springframework.web.bind.annotation.*;
1611
import org.springframework.web.servlet.ModelAndView;
1712

18-
import com.baeldung.model.Employee;
13+
import java.util.*;
1914

15+
@SessionAttributes("employees")
2016
@Controller
2117
@ControllerAdvice
2218
public class EmployeeController {
2319

2420
Map<Long, Employee> employeeMap = new HashMap<>();
2521

22+
@ModelAttribute("employees")
23+
public void initEmployees() {
24+
employeeMap.put(1L, new Employee(1L, "John", "223334411", "rh"));
25+
employeeMap.put(2L, new Employee(2L, "Peter", "22001543", "informatics"));
26+
employeeMap.put(3L, new Employee(3L, "Mike", "223334411", "admin"));
27+
}
28+
2629
@RequestMapping(value = "/employee", method = RequestMethod.GET)
2730
public ModelAndView showForm() {
2831
return new ModelAndView("employeeHome", "employee", new Employee());
2932
}
3033

31-
@RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
32-
public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) {
34+
@RequestMapping(value = "/employee/{Id}", produces = {"application/json", "application/xml"}, method = RequestMethod.GET)
35+
public
36+
@ResponseBody
37+
Employee getEmployeeById(@PathVariable final long Id) {
3338
return employeeMap.get(Id);
3439
}
3540

@@ -40,6 +45,7 @@ public String submit(@ModelAttribute("employee") final Employee employee, final
4045
}
4146
model.addAttribute("name", employee.getName());
4247
model.addAttribute("contactNumber", employee.getContactNumber());
48+
model.addAttribute("workingArea", employee.getWorkingArea());
4349
model.addAttribute("id", employee.getId());
4450

4551
employeeMap.put(employee.getId(), employee);
@@ -52,4 +58,53 @@ public void addAttributes(final Model model) {
5258
model.addAttribute("msg", "Welcome to the Netherlands!");
5359
}
5460

61+
@RequestMapping(value = "/employees/{name}", method = RequestMethod.GET)
62+
@ResponseBody
63+
public ResponseEntity<List<Employee>> getEmployeeByNameAndBeginContactNumber(@PathVariable final String name, @MatrixVariable final String beginContactNumber) {
64+
final List<Employee> employeesList = new ArrayList<Employee>();
65+
for (final Map.Entry<Long, Employee> employeeEntry : employeeMap.entrySet()) {
66+
final Employee employee = employeeEntry.getValue();
67+
if (employee.getName().equalsIgnoreCase(name) && employee.getContactNumber().startsWith(beginContactNumber)) {
68+
employeesList.add(employee);
69+
}
70+
}
71+
return new ResponseEntity<>(employeesList, HttpStatus.OK);
72+
}
73+
74+
@RequestMapping(value = "/employeesContacts/{contactNumber}", method = RequestMethod.GET)
75+
@ResponseBody
76+
public ResponseEntity<List<Employee>> getEmployeeBycontactNumber(@MatrixVariable(required = true) final String contactNumber) {
77+
final List<Employee> employeesList = new ArrayList<Employee>();
78+
for (final Map.Entry<Long, Employee> employeeEntry : employeeMap.entrySet()) {
79+
final Employee employee = employeeEntry.getValue();
80+
if (employee.getContactNumber().equalsIgnoreCase(contactNumber)) {
81+
employeesList.add(employee);
82+
}
83+
}
84+
return new ResponseEntity<>(employeesList, HttpStatus.OK);
85+
}
86+
87+
@RequestMapping(value = "employeeData/{employee}", method = RequestMethod.GET)
88+
@ResponseBody
89+
public ResponseEntity<Map<String, String>> getEmployeeData(@MatrixVariable final Map<String, String> matrixVars) {
90+
return new ResponseEntity<>(matrixVars, HttpStatus.OK);
91+
}
92+
93+
@RequestMapping(value = "employeeArea/{workingArea}", method = RequestMethod.GET)
94+
@ResponseBody
95+
public ResponseEntity<List<Employee>> getEmployeeByWorkingArea(@MatrixVariable final Map<String, LinkedList<String>> matrixVars) {
96+
final List<Employee> employeesList = new ArrayList<Employee>();
97+
final LinkedList<String> workingArea = matrixVars.get("workingArea");
98+
for (final Map.Entry<Long, Employee> employeeEntry : employeeMap.entrySet()) {
99+
final Employee employee = employeeEntry.getValue();
100+
for (final String area : workingArea) {
101+
if (employee.getWorkingArea().equalsIgnoreCase(area)) {
102+
employeesList.add(employee);
103+
break;
104+
}
105+
}
106+
}
107+
return new ResponseEntity<>(employeesList, HttpStatus.OK);
108+
}
109+
55110
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1" %>
3+
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>Form Example - Register a Company</title>
8+
</head>
9+
<body>
10+
<h3>Welcome, Enter The Company Details</h3>
11+
12+
<form:form method="POST" action="/spring-mvc-java/addCompany"
13+
modelAttribute="company">
14+
<table>
15+
<tr>
16+
<td><form:label path="name">Name</form:label></td>
17+
<td><form:input path="name"/></td>
18+
</tr>
19+
<tr>
20+
<td><form:label path="id">Id</form:label></td>
21+
<td><form:input path="id"/></td>
22+
</tr>
23+
<tr>
24+
<td><input type="submit" value="Submit"/></td>
25+
</tr>
26+
</table>
27+
</form:form>
28+
29+
</body>
30+
31+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
2+
<html>
3+
<head>
4+
<title>Spring MVC Form Handling</title>
5+
</head>
6+
<body>
7+
8+
<h2>Submitted Company Information</h2>
9+
<table>
10+
<tr>
11+
<td>Name :</td>
12+
<td>${name}</td>
13+
</tr>
14+
<tr>
15+
<td>ID :</td>
16+
<td>${id}</td>
17+
</tr>
18+
</table>
19+
</body>
20+
</html>

‎spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
<td><form:label path="contactNumber">Contact Number</form:label></td>
2323
<td><form:input path="contactNumber" /></td>
2424
</tr>
25+
<tr>
26+
<td><form:label path="workingArea">Working Area</form:label></td>
27+
<td><form:input path="workingArea" /></td>
28+
</tr>
2529
<tr>
2630
<td><input type="submit" value="Submit" /></td>
2731
</tr>

‎spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
<td>Contact Number :</td>
2121
<td>${contactNumber}</td>
2222
</tr>
23+
<tr>
24+
<td>Working Area :</td>
25+
<td>${workingArea}</td>
26+
</tr>
2327
</table>
2428
</body>
2529
</html>

0 commit comments

Comments
 (0)