Moved ModeAttribute article from spring-mvc-java to spring-mvc-basics module

This commit is contained in:
Gerardo Roza
2019-07-01 22:08:02 -03:00
parent bb2765c4e3
commit dc3c793717
8 changed files with 106 additions and 20 deletions

View File

@@ -18,7 +18,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Upload and Display Excel Files with Spring MVC](http://www.baeldung.com/spring-mvc-excel-files)
- [web.xml vs Initializer with Spring](http://www.baeldung.com/spring-xml-vs-java-config)
- [The HttpMediaTypeNotAcceptableException in Spring MVC](http://www.baeldung.com/spring-httpmediatypenotacceptable)
- [Spring MVC and the @ModelAttribute Annotation](http://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation)
- [Spring MVC @PathVariable with a dot (.) gets truncated](http://www.baeldung.com/spring-mvc-pathvariable-dot)
- [A Quick Example of Spring Websockets @SendToUser Annotation](http://www.baeldung.com/spring-websockets-sendtouser)
- [Working with Date Parameters in Spring](https://www.baeldung.com/spring-date-parameters)

View File

@@ -1,20 +1,29 @@
package com.baeldung.web.controller;
import com.baeldung.model.Employee;
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.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
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 java.util.*;
import com.baeldung.model.Employee;
@SessionAttributes("employees")
@Controller
@ControllerAdvice
public class EmployeeController {
Map<Long, Employee> employeeMap = new HashMap<>();
@@ -35,7 +44,7 @@ public class EmployeeController {
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()) {
@@ -51,11 +60,6 @@ public class EmployeeController {
return "employeeView";
}
@ModelAttribute
public void addAttributes(final Model model) {
model.addAttribute("msg", "Welcome to the Netherlands!");
}
@RequestMapping(value = "/employees/{name}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<List<Employee>> getEmployeeByNameAndBeginContactNumber(@PathVariable final String name, @MatrixVariable final String beginContactNumber) {

View File

@@ -8,7 +8,7 @@
<body>
<h3>Welcome, Enter The Employee Details</h3>
<form:form method="POST" action="/spring-mvc-java/addEmployee" modelAttribute="employee">
<form:form method="POST" action="/spring-mvc-basics/addEmployee" modelAttribute="employee">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>

View File

@@ -6,7 +6,6 @@
<body>
<h2>Submitted Employee Information</h2>
<h3>${msg}</h3>
<table>
<tr>
<td>Name :</td>
@@ -20,10 +19,6 @@
<td>Contact Number :</td>
<td>${contactNumber}</td>
</tr>
<tr>
<td>Working Area :</td>
<td>${workingArea}</td>
</tr>
</table>
</body>
</html>