Moved ModeAttribute article from spring-mvc-java to spring-mvc-basics module
This commit is contained in:
parent
bb2765c4e3
commit
dc3c793717
@ -15,3 +15,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||||||
- [Spring @RequestMapping New Shortcut Annotations](http://www.baeldung.com/spring-new-requestmapping-shortcuts)
|
- [Spring @RequestMapping New Shortcut Annotations](http://www.baeldung.com/spring-new-requestmapping-shortcuts)
|
||||||
- [Spring MVC Custom Validation](http://www.baeldung.com/spring-mvc-custom-validator)
|
- [Spring MVC Custom Validation](http://www.baeldung.com/spring-mvc-custom-validator)
|
||||||
- [Using Spring @ResponseStatus to Set HTTP Status Code](http://www.baeldung.com/spring-response-status)
|
- [Using Spring @ResponseStatus to Set HTTP Status Code](http://www.baeldung.com/spring-response-status)
|
||||||
|
- [Spring MVC and the @ModelAttribute Annotation](http://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation)
|
@ -4,11 +4,15 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
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.ModelAttribute;
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
import com.baeldung.model.Employee;
|
import com.baeldung.model.Employee;
|
||||||
|
|
||||||
@ -24,8 +28,33 @@ public class EmployeeController {
|
|||||||
employeeMap.put(3L, new Employee(3L, "Mike", "223334411", "admin"));
|
employeeMap.put(3L, new Employee(3L, "Mike", "223334411", "admin"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/employee/{Id}",produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
|
@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) {
|
public @ResponseBody Employee getEmployeeById(@PathVariable final Long Id) {
|
||||||
return employeeMap.get(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";
|
||||||
|
}
|
||||||
|
|
||||||
|
@ModelAttribute
|
||||||
|
public void addAttributes(final Model model) {
|
||||||
|
model.addAttribute("msg", "Welcome to the Netherlands!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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"%>
|
||||||
|
<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><input type="submit" value="Submit" /></td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</form:form>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
@ -0,0 +1,25 @@
|
|||||||
|
<%@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>
|
||||||
|
<h3>${msg}</h3>
|
||||||
|
<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>
|
@ -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)
|
- [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)
|
- [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)
|
- [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)
|
- [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)
|
- [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)
|
- [Working with Date Parameters in Spring](https://www.baeldung.com/spring-date-parameters)
|
||||||
|
@ -1,20 +1,29 @@
|
|||||||
package com.baeldung.web.controller;
|
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.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
|
||||||
import org.springframework.ui.ModelMap;
|
import org.springframework.ui.ModelMap;
|
||||||
import org.springframework.validation.BindingResult;
|
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 org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
import java.util.*;
|
import com.baeldung.model.Employee;
|
||||||
|
|
||||||
@SessionAttributes("employees")
|
@SessionAttributes("employees")
|
||||||
@Controller
|
@Controller
|
||||||
@ControllerAdvice
|
|
||||||
public class EmployeeController {
|
public class EmployeeController {
|
||||||
|
|
||||||
Map<Long, Employee> employeeMap = new HashMap<>();
|
Map<Long, Employee> employeeMap = new HashMap<>();
|
||||||
@ -51,11 +60,6 @@ public class EmployeeController {
|
|||||||
return "employeeView";
|
return "employeeView";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ModelAttribute
|
|
||||||
public void addAttributes(final Model model) {
|
|
||||||
model.addAttribute("msg", "Welcome to the Netherlands!");
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/employees/{name}", method = RequestMethod.GET)
|
@RequestMapping(value = "/employees/{name}", method = RequestMethod.GET)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResponseEntity<List<Employee>> getEmployeeByNameAndBeginContactNumber(@PathVariable final String name, @MatrixVariable final String beginContactNumber) {
|
public ResponseEntity<List<Employee>> getEmployeeByNameAndBeginContactNumber(@PathVariable final String name, @MatrixVariable final String beginContactNumber) {
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<h3>Welcome, Enter The Employee Details</h3>
|
<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>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td><form:label path="name">Name</form:label></td>
|
<td><form:label path="name">Name</form:label></td>
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
<body>
|
<body>
|
||||||
|
|
||||||
<h2>Submitted Employee Information</h2>
|
<h2>Submitted Employee Information</h2>
|
||||||
<h3>${msg}</h3>
|
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Name :</td>
|
<td>Name :</td>
|
||||||
@ -20,10 +19,6 @@
|
|||||||
<td>Contact Number :</td>
|
<td>Contact Number :</td>
|
||||||
<td>${contactNumber}</td>
|
<td>${contactNumber}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
|
||||||
<td>Working Area :</td>
|
|
||||||
<td>${workingArea}</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
</table>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
x
Reference in New Issue
Block a user