SpringMVC study notes (1) sequel-detailed configuration

Tip: Mainly introduce the configuration and use of some common components of springmvc


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.xmlof the configuration dispatcherServletis configured <load-on-startup>1</load-on-startup>so that when you start, the label DispatcherServletobject is created and loaded <init-param>configuration tab springmvc.xml.

2. Because springmvc.xmlannotation 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

Insert picture description here
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:

  1. Function and class: the first-level access directory
  2. Acting on the method: the second level of access to the directory

Properties of RequestMapping:

  1. path specifies the url of the request path
  2. The value attribute is the same as the path attribute
  3. mthod specifies the request method of the method
  4. params specifies the conditions that limit the request parameters
  5. headers The request headers that must be included in the request sent

Sample code:
Insert picture description here
Insert picture description here
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:

  1. value: the name in the request parameter
  2. 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

  1. 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

  1. value: Specify the placeholder name
    in the URL Restful style URL

  2. The request path is the same, and different methods in the background can be executed according to different request methods

  3. The advantages of restful style URL

    1. Clear structure
    2. Standards compliant
    3. Easy to understand
    4. 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

  1. 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

  1. 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

  1. Appear on the method: Indicates that the current method will be executed before the controller method is executed.
  2. Appear on the parameter: Get the specified data to assign the parameter.

Application scenarios

  1. 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

  1. 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";
    }

Insert picture description here

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.Insert picture description here

Guess you like

Origin blog.csdn.net/u013456390/article/details/113430369