SpringMVC——常用注解说明

RequestParam

  • 作用:把请求中指定名称的参数赋值给控制器中的形参。
  • 常用属性:
属性功能
value请求参数中的名称赋值给方法形参 与name属性互为别名
name请求参数中的名称赋值给方法形参 与value属性互为别名
required请求参数中是否必须传入此参数。默认值:true,表示必须提供,如果不提供将报错
示例代码

编写jsp代码

<%@page contentType="text/html; charset=UTF-8" language="java"  isELIgnored="false" %>

<html>
    <head>
        <title>requestMapping的使用</title>
    </head>
    <body>
        <!--requestParams注解的使用-->
        <a href="springmvc/useRequestParam?name=test">测试 requestParam注解</a>
    </body>
</html>

编写Controller代码

package com.liang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {

    @RequestMapping(path = "/useRequestParam")
    public String useRequestParam(@RequestParam(name = "name") String username, @RequestParam(value = "age", required = false) Integer age){
        System.out.println("执行了useRequestParam方法..."+username+" : "+age);
        return "success";
    }
}

RequestBody

  • 作用:用于获取请求体内容,直接使用得到key=value&key=value……结构的数据。get请求方式不适用。
  • 常用属性:
属性功能
required是否必须有请求体,默认值:true,此时get请求方式会报错,若值为false时,get请求为null
示例代码

编写jsp代码

<%@page contentType="text/html; charset=UTF-8" language="java"  isELIgnored="false" %>

<html>
    <head>
        <title>requestMapping的使用</title>
    </head>
    <body>
        <!--RequestBody注解的使用-->
        <!--post请求方式-->
        <form action="springmvc/useRequestBody" method="post">
            用户名称:<input type="text" name="username"/><br/>
            用户密码:<input type="password" name="password"/><br/>
            用户年龄:<input type="text" name="age"/><br/>
            <input type="submit" value="保存"/>
        </form>
        <!--get请求方式-->
        <a href="springmvc/useRequestBody?body=test">测试 RequestBody 注解 get请求</a>
    </body>
</html>

编写Controller代码

package com.liang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {

    @RequestMapping(path = "/useRequestBody")
    public String useRequestBody(@RequestBody(required = false) String body){
        System.out.println("执行了useRequestBody方法..."+body);
        return "success";
    }
}

PathVaribale

  • 作用:用于绑定URL中的占位符,Spring3.0后加入了url支持占位符
  • 常用属性:
属性功能
value用于指定url中占位符名称
required是否必须提供占位符
示例代码

编写jsp代码

<%@page contentType="text/html; charset=UTF-8" language="java"  isELIgnored="false" %>

<html>
    <head>
        <title>requestMapping的使用</title>
    </head>
    <body>
        <!--PathVariable注解-->
        <a href="springmvc/usePathVariable/100/200">PathVariable注解</a>
    </body>
</html>

编写Controller代码

package com.liang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {

    @RequestMapping(path = "/usePathVariable/{id}/{i}")
    public String usePathVariable(@PathVariable("id") Integer id , @PathVariable("i") Integer i){
        System.out.println("执行了usePathVariable方法...  id=" + id + " , i="+i);
        return "success";
    }
}

RequestHeader

  • 作用:用于获取请求消息头
  • 常用属性:
属性功能
value/name需要获取到的消息头名称
required是否必须有此消息头
示例代码

编写jsp代码

<%@page contentType="text/html; charset=UTF-8" language="java"  isELIgnored="false" %>

<html>
    <head>
        <title>requestMapping的使用</title>
    </head>
    <body>
        <!--RequestHeader注解-->
        <a href="springmvc/useRequestHeader">获取请求消息头</a>
    </body>
</html>

编写Controller代码

package com.liang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {

    @RequestMapping(path = "/useRequestHeader")
    public String useRequestHeader(@RequestHeader(name = "Accept-Language", required = false) String requestHeader){
        System.out.println("执行了useRequestHeader方法...  "+requestHeader );
        return "success";
    }
}

CookieValue

  • 作用:用于把指定cookie名称的值传入控制器方法参数中。
  • 常用属性
属性功能
value指定cookie的名称
required是否必须有此cookie
示例代码

编写jsp代码

<%@page contentType="text/html; charset=UTF-8" language="java"  isELIgnored="false" %>

<html>
    <head>
        <title>requestMapping的使用</title>
    </head>
    <body>
        <!--CookieValue注解-->
        <a href="springmvc/useCookieValue">获取cookie的值</a>
    </body>
</html>

编写Controller代码

package com.liang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller(value = "annotationController")
@RequestMapping(path = "/springmvc")
public class AnnotationController {

    @RequestMapping(path = "/useCookieValue")
    public String useCookieValue(@CookieValue(value = "JSESSIONID", required = false) String cookieValue){
        System.out.println("执行了useCookieValue方法...  " + cookieValue );
        return "success";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值