Some common annotations and usage of springMVC

Preface

I just finished learning springMVC, and I will present common annotations, hoping to improve my understanding and application of springMVC, and I hope to help everyone!
First of all, we know the three-tier architecture of the server side: presentation layer, business layer, and persistence layer, and springMVC is the framework of the presentation layer.
MVC workflow:
First, the client (usually a browser) makes a request. The first component to accept this request is generally a front controller. It passes different requests to different back-end controllers for processing, and the back-end controller can call the corresponding model object to process specific business logic, and finally return a specific view response to the client.
The following figure shows the operation details of the springMVC framework:
SpringMVC workflow: The
first front-end controller that accepts this request is called DispatcherServlet, and the back-end controller is called Controller. The one responsible for processing the request URL and the back-end controller mapping is called HandMapping. It has multiple types and is more flexible. It is also configured on an xml file. The model object responsible for business logic processing is generally the DAO/DTO component we usually write. But its final return is more flexible. Controller returns a ModelAndView object to DispatcherServlet. ModelAndView can carry a view object or the logical name of a view object. If it carries the logical name of a view object, the DispatcherServlet needs a ViewResolver to find the view object used to render the response. Finally, DispatcherServlet dispatches the request to the view object specified by the ModelAndView object. The view object is responsible for rendering the response returned to the customer.
(From Sogou Encyclopedia)
Insert picture description here

@Controller:

Acting on the class, it indicates that the current class is a controller class. After adding this annotation, the spring framework will scan the method of the class that uses the annotation, and will detect whether the method uses the @RequestMapping annotation.

@RequestMapping:

Role: Used to establish the correspondence between the request URL and the method of processing the request
. Scope: The scope is on the method or class. If the annotation is used on the class, then the JSP access to the page needs to be before the path of the href access method Add the path
attribute of the class :
path: path
value: alias of path
method: determine the request method value is an enumerated type
params: "username" you will request this method later, you must pass me a username attribute if the comment has a value for username Then the value of username must also be the value you pass.
headers: request headers that must be included in the sending request

@Controller
public class HelloController {

    @RequestMapping(path = "/Hello",params={"username=heihei"},headers={"Accept"})
    public String sayHellio() {
        System.out.println("Hello SpringMVC");
        return "success";
    }
    }

@RequestParam:

Scenario: If the uploaded parameter does not match the parameter of the accessed method, then the parameter cannot be obtained, so what should I do?

    @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(name = "name") String username) {
        System.out.println("执行了");
        System.out.println(username);
        return "success";
    }

@RequestBody:

It is used to get the content of the request header, which is directly obtained in the form of key=value&key=value The
get request method is not available

    //获取请求体
    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String body) {
        System.out.println(body);
        return "success";
    }

@RequestHeader:

Get the specified request header

    //    获取请求头的值  需要哪个填哪个 (据说可以防爬虫 可以试下)
    @RequestMapping("/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value = "Accept") String header) {
        System.out.println(header);
        return "success";
    }

@CookieValue :

   //获取cookie的值
    @RequestMapping("/testCookieValue")
    public String testCookieValue(@CookieValue(value = "JSESSIONID") String cookieValue) {
        System.out.println(cookieValue);
        return "success";
    }

@ModelAttribute :

Comment on the method

In this case, the method will be called before each controller method is called.

   //获取cookie的值
    @RequestMapping("/testCookieValue")
    public String testCookieValue(@CookieValue(value = "JSESSIONID") String cookieValue) {
        System.out.println(cookieValue);
        return "success";
    }

@SessionAttributes

Used for multiple execution of method parameter sharing between controllers.
This annotation can only be used on classes

//@SessionAttributes(value = {"msg"}) //把msg存入到session域中

    //获取SessionAttributes的值
    @RequestMapping("/testSessionAttributes")
    public String testSessionAttributes(Model model) {
        System.out.println("testSessionAttributes执行了");
        //底层会存储到request域当中
        model.addAttribute("msg", "haha");
        return "success";
    }

    @RequestMapping("/getSessionAttributes")
    public String getSessionAttributes(ModelMap model) {
        System.out.println("testSessionAttributes执行了");
        //从session中获取值
        String msg = (String) model.get("msg");
        System.out.println(msg);
        return "success";
    }

@RequestBody automatically encapsulates the request body and bean correspondence

@ResponseBodyt automatically converts beans into json data

rely:

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.0</version>
        </dependency>

ps: When using jquery's ajax upload, solve the problem of static resource interception
springmvc.xml

    <!--前端控制器,哪些静态资源不拦截 两参数:请求路径带js任何文件也都不拦截 以后js下的任何文件都不拦截 -->
    <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>

 //模拟异步请求和响应
    @RequestMapping("/testAjax")
    public @ResponseBody User1 testAjax(@RequestBody User1 user1) { //拿到请求体的内容
        System.out.println("异步");
        //客户端发送的是ajax请求 传的是json字符串,后端把json字符串封装到user1对象中
        System.out.println(user1);

        //做响应,模拟查询
        user1.setUsername("查完了");
        user1.setAge(40);
        user1.setPassword("ahha");

        System.out.println(user1);
        //做响应
        return user1;
    }

The above are some common annotations. I will detail the upload interceptor, file upload and download, exception handling and other articles later!

Guess you like

Origin blog.csdn.net/Pzzzz_wwy/article/details/105458806