SpringMVC study notes-04 annotation development

1.@Controller

@Controller is used to annotate the processor class. The class with this annotation will be automatically instantiated and injected into the container after being scanned by the container.

2.@RequestingMapping

1. value attribute

@RequestingMapping uses the value attribute to specify the request path. When there is only one parameter, the value is assigned to the value attribute by default.

    @AliasFor("path")
    String[] value() default {};

A. @RequestingMapping is placed on the method

package com.zzt.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

    @RequestMapping("/some.do")
    public ModelAndView doSome(){
        ModelAndView modelAndView = new ModelAndView();

        // 存放数据 框架会自动将数据放到request作用域
        modelAndView.addObject("msg","hello-word");

        // 指定视图的路径 框架会自动通过forward进行请求转发 request.getRequestDispatcher("/show.jsp").forward(..)
        modelAndView.setViewName("show");

        return modelAndView;
    }

}

At this point, if the request wants to call the doSome method of the controller object, the request path needs to be some.do.

<a href="some.do">发起请求</a>
 

B. @RequestingMapping is placed on the class

package com.zzt.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/Test")
public class MyController {

    @RequestMapping("/some.do")
    public ModelAndView doSome(){
        ModelAndView modelAndView = new ModelAndView();

        // 存放数据 框架会自动将数据放到request作用域
        modelAndView.addObject("msg","hello-word");

        // 指定视图的路径 框架会自动通过forward进行请求转发 request.getRequestDispatcher("/show.jsp").forward(..)
        modelAndView.setViewName("show");

        return modelAndView;
    }

}

At this point, if the request wants to call the doSome method of the controller object, the request path needs to be /Test/some.do.

<a href="/Test/some.do">发起请求</a>

C.value attribute specifies multiple mappings

value is an array that can store multiple path mappings; that is, we can have multiple request paths mapped to the same method. In order to prevent the compiler from misunderstanding that this is the assignment of two attributes, we use {} to indicate that the value is assigned to value[ ].

    <a href="some.do">发起第一个请求</a>
    <a href="other.do">发起第二个请求</a>
package com.zzt.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

    @RequestMapping(value = {"/some.do","/other.do"} )
    public ModelAndView doSome(){
        ModelAndView modelAndView = new ModelAndView();

        // 存放数据 框架会自动将数据放到request作用域
        modelAndView.addObject("msg","hello-word");

        // 指定视图的路径 框架会自动通过forward进行请求转发 request.getRequestDispatcher("/show.jsp").forward(..)
        modelAndView.setViewName("show");

        return modelAndView;
    }

}

2. method

method is used to specify that the method of annotation requires the corresponding request type, commonly used get and post; if not specified, it means that the request type is not limited.

In the web, we should have learned that the get request is not safe. It will append the passed parameters to the url, but this does not mean that get has no advantage. In fact, the post request needs to resubmit the form when it is rolled back. , And get is not needed. For some occasions without security requirements, get is sufficient.

package com.zzt.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/test")
public class MyController {

    @RequestMapping(value = "/some.do" , method = RequestMethod.GET)
    public ModelAndView doSome(){
        ModelAndView modelAndView = new ModelAndView();

        // 存放数据 框架会自动将数据放到request作用域
        modelAndView.addObject("msg","hello-get");
        
        modelAndView.setViewName("show");

        return modelAndView;
    }

    @RequestMapping(value = "/other.do", method = RequestMethod.POST)
    public ModelAndView doOther(){
        ModelAndView modelAndView = new ModelAndView();

        // 存放数据 框架会自动将数据放到request作用域
        modelAndView.addObject("msg","hello-post");
        
        modelAndView.setViewName("show");

        return modelAndView;
    }

    @RequestMapping(value = "/noLimit.do")
    public ModelAndView doNoLimit(){
        ModelAndView modelAndView = new ModelAndView();

        // 存放数据 框架会自动将数据放到request作用域
        modelAndView.addObject("msg","hello-nolimit");
        
        modelAndView.setViewName("show");

        return modelAndView;
    }

}
    <p>第一个项目</p>
    <a href="test/other.do">发起some.do请求</a>
    <br>
    <form action="test/some.do" method="post">
        <input type="submit" value="post请求提交">
    </form>
    <a href="test/noLimit.do">noLimit.do-get方式</a>
    <form action="test/noLimit.do" method="post">
        <input type="submit" value="post提交noLimit">
    </form>

1

Now we modify the request method:

    <p>第一个项目</p>
    <a href="test/other.do">发起some.do请求</a>
    <br>
    <form action="test/some.do" method="post">
        <input type="submit" value="post请求提交">
    </form>

It can be seen that if we use a request method other than the specified method, it will not be able to access normally because it is not supported.

3. @RequestParam

        @RequestParam is used to specify which parameter value should be assigned to the currently specified attribute. The usage is the same as @Param in Mybatis; it can only be used in the processor method. When using object reception, the framework will look up the attributes required by the entity class Request parameters.

A. value is used to specify the parameter name in the request

    <p>提交参数</p>
    <form action="test/receiveProperty.do" method="post">
        姓名:<input type="text" name="rname"><br>
        年龄:<input type="text" name="rage"><br>
        <input type="submit" value="提交参数">
    </form>
    @RequestMapping(value = "/receiveProperty.do")
    public ModelAndView doReceiveProperty(@RequestParam(value = "rname") String name,@RequestParam(value = "rage") Integer age){
        ModelAndView modelAndView = new ModelAndView();

        System.out.println("age = " + age);

        modelAndView.addObject("name",name);
        modelAndView.addObject("age",age);

        modelAndView.setViewName("showProperty");

        return modelAndView;
    }

B. required is used to specify whether the parameter must exist

The default value of required is true, which means that the parameter must be included in the request. If it does not exist, an error will occur. The advantage of this is to avoid that when we know the request path, we do not directly access through the normal process of submitting parameters: http://localhost:8080/SpringMVC_01/test/receiveProperty.do .

As shown in the figure above, even if the request path is known, the server will not respond normally because the parameters are not submitted normally.

 

Guess you like

Origin blog.csdn.net/qq_39304630/article/details/112872236