目录
springmvc常用注解,操作传入参数@RequestParam@RequestBody@PathVariable@RequestHeader@CookieValue@ModelAttribute@SessionAttributesspringmvc常用注解,操作传入参数
@RequestParam
一般用于jsp参数名和后台方法参数指定,对应
/*
    * value=name 当jsp的参数和方法上的参数对应不上,可以指明
    * required() default true;默认true   有参数则必须传
    * */
    public String testRequestParam(@RequestParam(name = "name",required = false) String username){
        System.out.println("执行了..........");
        System.out.println(username);
        return "success";
    }
<body>
    <a href="anno/testRequestParam" rel="external nofollow"  rel="external nofollow" >testRequestParam</a>
 
</body>
不传参数,required()设置为false,方法有参数
测试

@RequestBody
一般用于获取post请求的方法体,jsp参数格式为键值对,即 key-value
该注解不适应于get请求,一般用于post请求,例如表单提交
如果要用于get请求,则需
@RequestBody(required = false)
否则报错,此时方法参数为null
@RequestMapping(path = "testRequestBody")
    public String testRequestBody(@RequestBody(required = false) String body){
        System.out.println("执行了..........");
        System.out.println(body);
        return "success";
    }
<body>
    <%--<a href="anno/testRequestParam" rel="external nofollow"  rel="external nofollow" >testRequestParam</a>--%>
 
    <form action="anno/testRequestBody" method="post">
        用户名:<input type="text" name="username"/><br>
        密码:<input type="text" name="password"/><br>
        <input type="submit" value="提交"/><br>
    </form>
</body>
测试


@PathVariable
URL的占位符,restful风格,传参格式 url地址后/10
restful请求方式: get,post,put 配合注解@RequestMapping设置请求方式
@RequestMapping(path = "testPathVariable/{sid}",method = RequestMethod.GET)
@RequestMapping(path = "testPathVariable/{sid}",method = RequestMethod.GET)
    /*
    * {sid}表示URL的占位符
    * boolean required() default true;默认参数必须传
    * */
    public String testPathVariable(@PathVariable("sid") String id){
        System.out.println("执行了..........");
        System.out.println(id);
        return "success";
    }
<a href="anno/testPathVariable/10" rel="external nofollow" >testPathVariable</a>
可以下载postman客户端,模拟发送不同的请求方式

测试:


@RequestHeader
获取请求头的某些属性值 如浏览器类型、版本等 不常用
@RequestMapping(path = "testRequestHeader",method = RequestMethod.GET)
    /*获取请求头的某些属性值 如浏览器类型、版本等*/
    public String testRequestHeader(@RequestHeader(value = "Accept") String head){
        System.out.println("执行了..........");
        System.out.println(head);
        return "success";
    }
<a href="anno/testRequestHeader" rel="external nofollow" >testRequestHeader</a>

@CookieValue
获取JSESSIONID的值
@RequestMapping(path = "testCookieValue",method = RequestMethod.GET)
    public String testCookieValue(@CookieValue(value = "JSESSIONID") String JSESSIONID){
        System.out.println("执行了..........");
        System.out.println(JSESSIONID);
        return "success";
    }
<a href="anno/testCookieValue" rel="external nofollow" >testCookieValue</a><br>

@ModelAttribute
用于封装的数据不全补全数据,或者检查封装数据等场景
可作用于方法和参数
修饰方法,方法入参需和控制器方法同参类型,该方法优先于控制器之前执行,且分类有返回值和无返回值
有返回值,则该方法的返回值和控制器的入参相同相同 无返回值,则该方法的参数除了和控制器的入参相同外,还需加一个map类型参数map<string,objct>例子:
注解修饰的方法有返回值写法
 @RequestMapping(path = "testModelAttribute")
    public String testModelAttribute(User user){
        System.out.println("执行了..........");
        System.out.println(user);
        return "success";
    }
 
    @ModelAttribute
    //修饰方法,该方法优先于控制器之前执行
    public User showUser(User user){
        /*模拟jsp传的user封装数据不全,
        通过名字查询数据库对应的信息
        返回全的user对象*/
        user.setBirthday(new Date());
        return user;
    }
<form action="anno/testModelAttribute" method="post">
        用户名:<input type="text" name="uname"/><br>
        年龄:<input type="text" name="age"/><br>
        <input type="submit" value="提交"/><br>
    </form>
注解修饰的方法无返回值写法
@RequestMapping(path = "testModelAttribute")
    public String testModelAttribute(@ModelAttribute("key") User user){
        System.out.println("执行了..........");
        System.out.println(user);
        return "success";
    }
 
    @ModelAttribute
    //修饰方法,该方法优先于控制器之前执行
    public void showUser(User user, Map<String,User> userMap){
        /*模拟jsp传的user封装数据不全,
        通过名字查询数据库对应的信息
        返回全的user对象*/
        user.setBirthday(new Date());
        userMap.put("key",user);
    }
测试


@SessionAttributes
注解只能作用于类,用于存取数据到session域对象中,实现方法数据共享
实现方式:从request域对象中复制数据到session域中
/**
 * @Date 2019/9/12 2:05
 * by mocar
 */
@Controller
@RequestMapping(path = "/anno")
@SessionAttributes(names = {"msg"})//从request域对象中复制到session域对象
public class annoController {
 
    
    @RequestMapping("/setRequest")//存入
    public String setRequest(ModelMap modelMap){
        System.out.println("setRequest......");
        modelMap.addAttribute("msg","test");//往Request域对象存值
        return "success";
    }
 
    @RequestMapping("/getSession")//获取
    public String getSession(ModelMap modelMap){
        System.out.println("getSession.......");
        Object msg = modelMap.get("msg");
        System.out.println(msg.toString());
        return "success";
    }
 
    @RequestMapping("/delSession")//删除
    public String delSession(SessionStatus sessionStatus,ModelMap modelMap){
        System.out.println("delSession.......");
        sessionStatus.setComplete();
        Object msg = modelMap.get("msg");
        System.out.println(msg.toString());
        return "success";
    }
 
}
jsp:
     <br>
    <a href="anno/setRequest" rel="external nofollow" >setRequest</a><br>
    <a href="anno/getSession" rel="external nofollow" >getSession</a><br>
    <a href="anno/delSession" rel="external nofollow" >delSession</a><br>
success.jsp 设置不忽略EL表达式,显示session域数据
<%--
  Created by IntelliJ IDEA.
  User: Mocar
  Date: 2019/9/11
  Time: 4:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>快速入门</title>
</head>
<body>
    <h3>success</h3>
 
    ${sessionScope}
 
</body>
</html>
setsession

getsession

delsession

以上为个人经验,希望能给大家一个参考,也希望大家多多支持免费资源网。

