Tip: Mainly introduce the configuration and use of some common components of springmvc
Article Directory
Preface
Springmvc is developed based on components, so the required components need to be configured before the formal development.
I have one dish, if you make a mistake, please lightly spray it.
1. Analysis of the execution process of the entry case
1. When the Tomcat server startup time because web.xml
of the configuration dispatcherServlet
is configured <load-on-startup>1</load-on-startup>
so that when you start, the label DispatcherServlet
object is created and loaded <init-param>
configuration tab springmvc.xml
.
2. Because springmvc.xml
annotation scanning is enabled in and the @Controller tag is added to HelloController, the HelloController object will be created.
3. The request sent from the index.jsp page will arrive first. The dispatcherServlet core controller (front controller) will find the method to be executed according to the configured @RequestMapping annotation.
4. After executing the method, the return value will find the returned jsp page according to the configured view parser.
5. The Tomcat server renders the page and responds
2. Component analysis in the entry case
1. Front Controller (DispatcherServlet)
2. Handler Mapping (HandlerMapping)
3. Processor (Handler)
4. Processor Adapter (HandlAdapter)
5. View Resolver
6. View
Simply put, the front-end controller is like a commander on the battlefield. He has a group of minions with different functions. When fighting, he only needs to give instructions to different minions according to his needs. The minions under his hands complete it. The result will also be returned to the commander after the mission.
Three, commonly used annotation analysis
@RequestMapping
Role: used to establish the correspondence between url and processing method (API)
Instructions:
- Function and class: the first-level access directory
- Acting on the method: the second level of access to the directory
Properties of RequestMapping:
- path specifies the url of the request path
- The value attribute is the same as the path attribute
- mthod specifies the request method of the method
- params specifies the conditions that limit the request parameters
- headers The request headers that must be included in the request sent
Sample code:
So the request path here is: http://localhost:8080/user/hello
@RequestParam
Function: Pass the parameter with the specified name in the request to the formal parameter assignment in the controller
Attributes:
- value: the name in the request parameter
- required: Whether this parameter must be provided in the request parameters, the default value is true, must be provided
Sample code:
rear end:
@RequestMapping("/testRequestParam")
public String testRequestParam(@RequestParam(name = "name") String username){
System.out.println("testRequestParam执行了。。。");
System.out.println(username);
return "success";
}
front end:
<a href="anno/testRequestParam?name=哈哈">RequestParam</a>
@RequestBody
Function: used to obtain the content of the request body (note: the get method is not allowed)
Attributes
- required: whether there must be a request body, the default value is true
Sample code:
rear end:
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody String body){
System.out.println("testRequestBody执行了。。。");
System.out.println(body);
return "success";
}
front end:
<a href="anno/testPathVariable/10">testPathVariable</a>
AthPathVariable
Role: Have a placeholder in the binding URL. For example: /delete/{id} in the url, {id} is a placeholder
Attributes
-
value: Specify the placeholder name
in the URL Restful style URL -
The request path is the same, and different methods in the background can be executed according to different request methods
-
The advantages of restful style URL
- Clear structure
- Standards compliant
- Easy to understand
- Easy to expand
Sample code:
rear end:
@RequestMapping("/testPathVariable/{sid}")
public String testPathVariable(@PathVariable(name = "sid") String id){
System.out.println("testPathVariable执行了。。。");
System.out.println(id);
return "success";
}
front end:
<form action="anno/testRequestBody" method="post">
用户姓名:<input type="text" name="username" /><br/>
用户年龄:<input type="text" name="age" /><br/>
<input type="submit" value="提交" />
</form>
@RequestHeader
Role: Get the value of the specified request header
Attributes
- value: the name of the request header
Sample code:
rear end:
@RequestMapping("/testRequestHeader")
public String testRequestHeader(@RequestHeader(value = "Accept") String header){
System.out.println("testRequestHeader执行了。。。");
System.out.println(header);
return "success";
}
front end:
<a href="anno/testRequestHeader">testRequestHeader</a>
@CookieValue
Role: used to obtain the value of the name of the specified cookie
Attributes
- value: the name of the cookie
Sample code:
rear end:
@RequestMapping("/testCookieValue")
public String testCookieValue(@CookieValue(value = "JSESSIONID") String cookieValue){
System.out.println("testCookieValue执行了。。。");
System.out.println(cookieValue);
return "success";
}
front end:
<a href="anno/testCookieValue">testCookieValue</a>
@ModelAttribute
effect
- Appear on the method: Indicates that the current method will be executed before the controller method is executed.
- Appear on the parameter: Get the specified data to assign the parameter.
Application scenarios
- When the submitted form data is not complete entity data, ensure that the fields not submitted use the original data of the database.
Sample code:
rear end:
@RequestMapping("/testModelAttribute")
public String testModelAttribute(@ModelAttribute("abc") User user){
System.out.println("testModelAttribute执行了。。。");
System.out.println(user);
return "success";
}
@ModelAttribute
public void showUser(String username, Map<String,User> map){
User user = new User();
user.setUsername(username);
user.setAge(20);
user.setDate(new Date());
map.put("abc",user);
}
It should be noted here that only the useless data in the current form will be assigned with the data specified by the @ModelAttribute annotation.
front end:
<form action="anno/testModelAttribute" method="post">
用户姓名:<input type="text" name="username" /><br/>
用户年龄:<input type="text" name="age" /><br/>
<input type="submit" value="提交" />
</form>
@SessionAttributes
Role: used to execute parameter sharing between controller methods multiple times
Attributes
- value: Specify the name of the stored attribute
Sample code:
rear end:
@RequestMapping("/testSessionAttributes")
public String testSessionAttributes(Model model){
System.out.println("testSessionAttributes。。。");
model.addAttribute("msg","张三");
return "success";
}
@RequestMapping("/getSessionAttributes")
public String getSessionAttributes(ModelMap modelMap){
System.out.println("getSessionAttributes。。。");
String msg = (String) modelMap.get("msg");
System.out.println(msg);
return "success";
}
@RequestMapping("/delSessionAttributes")
public String delSessionAttributes(SessionStatus status){
System.out.println("getSessionAttributes。。。");
status.setComplete();
return "success";
}
front end:
<a href="anno/testSessionAttributes">testSessionAttributes</a>
<a href="anno/getSessionAttributes">getSessionAttributes</a>
<a href="anno/delSessionAttributes">delSessionAttributes</a>
to sum up
It is basically enough to master the first 4 annotations, and the latter annotations are rarely used.