@RequestMapping
This annotation can be applied to classes as well as methods, in which case the annotation will be applied to all processor methods of the controller class as the first level of the URL to access the directory.
The sample
@Controller
@RequestMapping(path = "/test")
public class HelloController {
@RequestMapping(path = "/hello")
public String sayHello(a) {
System.out.println("halo SpringMVC");
return "success"; }}Copy the code
<%@ page contentType="text/html; charset=UTF-8" language="java"%> < HTML > <head> <title> home </title> </head> <body> <h2>Hello World! </h2> <a href="test/hello"> Jump example </a> </body> </ HTML >Copy the code
The URL after running the jump:
attribute
(1) Value: Specifies the URL of the request, which is used in the same way as the path attribute. It supports receiving multiple strings, for example
@RequestMapping({"/hello"."/test"})
Copy the code
(2) method: specifies the RequestMethod, for example, requestmethod.get.
(3) Params: used to specify conditions that limit the request parameters. It requires that the key and value of the request parameters must be the same as the specified, otherwise the logical method will not be executed.
The sample
// The request parameter must have accountName and money cannot be 100; @RequestMapping(path ="/hello", params = {"accountName"."moeny! 100" })
public String sayHello() {
System.out.println("halo SpringMVC");
return "success";
}
Copy the code
Requested link:
<a href="test/hello? accountName=aaa&money>100"> Delete account, amount 100</a>Copy the code
@RequestBody
This annotation is used to get the content of the request. The structure of the content is: key=value&key=value… .
The GET request method does not apply to this annotation.
attribute
Required: Whether the request body is required. The default value is true. When the value is true, an error is reported in the GET request mode. If the value is false, the GET request returns null.
The sample
@Controller
@RequestMapping(path = "/test")
public class HelloController {
@RequestMapping(path = "/RequestBody")
public String testRequestBody(@RequestBody(required = false)String body) {
System.out.println(body);
return "success"; }}Copy the code
Request way
<form action="test/RequestBody" method="post">User Name:<input type="text" name="username" ><br/>User password:<input type="password" name="password" ><br/>User age:<input type="text" name="age" ><br/>
<input type="submit" value="Save">
</form>
Copy the code
Console output:
@ResponseBody
In the processing logic method in the previous control layer class, we returned the view name, and we probably only wanted to return data to the front end and not to the page.
The @responseBody annotation can be placed in front of the return type or on a method, and it can place the return value in the response body
The sample
@Controller
@RequestMapping(path = "/test")
public class HelloController {
@RequestMapping(path = "/ResponseBody")
@ResponseBody
public String testResponseBody(HttpServletRequest request) {
return "url:"+ request.getRequestURI(); } @requestMapping (path =)"/ResponseBody")
public @ResponseBody String testResponseBody(HttpServletRequest request) {
return "url:"+ request.getRequestURI(); * /}}Copy the code
Request link:
<a href="test/ResponseBody"> test ResponseBody < / a >Copy the code
The page is as follows:
@RequestParam
Passes the named parameter in the request URL to the logical method
attribute
Value: specifies the name of the parameter in the request URL
Required: Whether this parameter must be provided in the request parameter. The default value is true, which indicates that this parameter must be provided. If this parameter is not provided, an error is reported.
The sample
@Controller
@RequestMapping(path = "/test")
public class HelloController {
@RequestMapping(path = "/RequestParam")
public String testRequestParam(@RequestParam("uname") String uname,
@RequestParam(value = "age", required = false) Integer age) {
System.out.println(uname + " is " + age + "years old");
return "success"; }}Copy the code
Request link:
<a href="test/RequestParam? uname=on1">Test RequestParam</a>
Copy the code
@PathVariable
This annotation is used to get placeholders in the request URL, such as the string on1 in test/PathVariable/on1.
attribute
Value: Specifies the placeholder name in the URL
Required: Whether placeholders must be provided in the request parameters.
The sample
@Controller
@RequestMapping(path = "/test")
public class HelloController {
// If the method parameter name is the same as the placeholder name, you can click on the value attribute.
@RequestMapping(path = "/PathVariable/{uname}")
public String testPathVariable(@PathVariable("uname") String uname) {
System.out.println(uname);
return "success"; }}Copy the code
Request link
a href="test/PathVariable/on1"> test PathVariable < / a > < br >Copy the code
@ModelAttribute
Comment on methods
The method annotated by @ModelAttribute is executed before each method of the control-layer class executes, and the return value of that method is bound to the Model object. This annotation can modify methods that have/have no return value.
@Controller
@RequestMapping(path = "/test")
public class HelloController {
@RequestMapping(path = "/ModelAttribute")
public String testModelAttribute(User user) {
user.setUname("gua");
System.out.println(user);
return "success";
}
@ModelAttribute
public User getUserByID(Model model) {
// get User from database
User user = new User();
user.setAge(20);
user.setUname("on1");
user.setId(11);
return user; // equivalent to model.addattribute ("user",user);}}Copy the code
Request link:
<a href="test/ModelAttribute? id=11"> test ModelAttribute < / a > < br >Copy the code
Success page
<%@ page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false"% > < HTML > < head > < title > success < / title > < / head > < body > < h3 > successful jump to this page < / h3 >${user.id}
${user.uname}
${user.age}
</body>
</html>
Copy the code
Page display output:
Console output:
When the annotation specifies the attribute name
When @modelAttribute specifies the attribute name, the return value of the method is also placed in Model, and the corresponding key value is the attribute name.
@Controller
@RequestMapping(path = "/test")
public class HelloController {
@RequestMapping(path = "/ModelAttribute")
public String testModelAttribute(User user, Model model) {
user.setUname("gua");
System.out.println(user);
return "success";
}
@ModelAttribute("myUser")
public User getUserByIDUser User = new User(); user.setAge(20); user.setUname("on1");
user.setId(11);
returnuser; // equivalent to model.addattribute ("user",user); }}Copy the code
Jump to the SUCCESS page:
<%@ page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false"% > < HTML > < head > < title > success < / title > < / head > < body > < h3 > successful jump to this page < / h3 >${myUser.id}
${myUser.uname}
${myUser.age}
</body>
</html>
Copy the code
Page display:
Console output:
The @modelAttribute and @requestMapping annotations are the same method
In this case, the value of @requestMapping is part of the route of the request and part of the view name to be jumped. The return value of the method is no longer the jump view name, but is placed in model as the value of the key-value pair. The key value of the key-value pair is the attribute name specified by @modelAttribute.
In addition, if other methods of the Controller class are requested, methods decorated with both annotations are not executed first.
@Controller
@RequestMapping(path = "/test")
public class HelloController {
/ * this time to jump page url is http://localhost:8080/test/ModelAttribute2 res - success as a key value to be put in the model * /
@RequestMapping(path = "/ModelAttribute2")
@ModelAttribute("res")
public String testModelAttribute2(a) {
return "success"; }}Copy the code
Jump to ModelAttribute2 page:
<%@ page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false"% > < HTML > < head > < title > success < / title > < / head > < body > < h3 > successful jump to this page < / h3 >${res}
</html>
Copy the code
View structure at this point:
Page display:
Comment on the parameters
When the @ModelAttribute(“key”) annotation is on a method parameter, it fetches the value corresponding to the key value from the currently implicit Model object and copies the value to the annotated method parameter.
@Controller
@RequestMapping(path = "/test")
public class HelloController {
@ModelAttribute("myUser")
public User getUserByID(Model model) {
User user = new User();
user.setAge(20);
user.setUname("on1");
user.setId(11);
model.addAttribute("newStr"."sngu");
returnuser; // equivalent to model.addattribute ("user",user);
}
@RequestMapping(path = "/ModelAttribute3")
public String testModelAttribute3(@ModelAttribute("myUser") User user, @ModelAttribute("newStr") String str) {
System.out.println(user);
System.out.println(str);
return "success"; }}Copy the code
Console output:
Page display:
If @modelAttribute does not specify an attribute value user here, it defaults to the first letter of the user class, lowercase user, and null if STR also does not specify an attribute value.