Spring MVC common annotations for java learning

Spring MVC common annotations for java learning

0x00 Preface

Continue with the previous article to record some annotations commonly used by spring MVC.

0x01 common notes

Requestparam annotation

@Controller
public class HelloContraoller {
    @RequestMapping(path = "/hello",params = {"username"})
    public String sayHello(@RequestParam(name="name") String username){
        System.out.println("username"+username);

        return "success";

Use requestparam to assign the parameter with the specified name in the request to the formal parameter in the controller.

Requestbody annotation

@RequestMapping(path = "/tijiao")
public String submit(@RequestBody String body){
    System.out.println(body);

    return "success";
}

Used to get the content of the request body of the post request

Pathvariable annotation

    @RequestMapping(path = "/tijiao/{id}")
    public String submit(@PathVariable (name = "username") String username ){
        System.out.println(username);

        return "success";
    }

Has a placeholder in the binding URL. For example, / delete / {ID} is a placeholder in the URL.

CookieValue

    @RequestMapping(path = "/tijiao")
    public String submit(@CookieValue(value = "JSESSIONID") String cookie){
        System.out.println(cookie);

        return "success";
    }

Value used to get the name of the specified cookie

Sessionattributes annotation

Set value:



@SessionAttributes(value = "msg")
//该注解表示将request域中获取的属性,存入session域中
@Controller
public class HelloContraoller {



    @RequestMapping(path = "/tijiao")
    public String submit(Model model){
//        该方法默认会自动存储到request域里面
        model.addAttribute("msg","abc");


        return "success";
    }


}

Get value:

@RequestMapping(path="/find")
public String find(ModelMap modelMap) {
String username = (String) modelMap.get("username");

return "success";

Delete value:

@RequestMapping(path="/delete")
public String delete(SessionStatus status) {
    status.setComplete();
    return "success";
    }
}

0x02 end

There are many notes, so it's easy to forget. Make a simple record.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>