springmvc三大注解标签——@PathVariable @RequestParam @RequestHeader

@PathVariable 注解

带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义
通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx”) 绑定到操作方法的入参中。

      localhost:8080/springmvc/hello/pathVariable/bigsea 
      localhost:8080/springmvc/hello/pathVariable/sea 
      /*
     * 这些URL 都会 执行此方法 并且将  <b>bigsea</b>、<b>sea</b> 作为参数 传递到name字段 
     * @param name 
     * @return 
     */  
    @RequestMapping("/pathVariable/{name}")  
    public String pathVariable(@PathVariable("name")String name){  
        System.out.println("hello "+name);  
        return "helloworld";  
    }

JSP(这里指定全路径):

<h1>pathVariable</h1>  
<a href="${pageContext.request.contextPath}/hello/pathVariable/bigsea" > name is bigsea </a>  
<br/>  
<a href="${pageContext.request.contextPath}/hello/pathVariable/sea" > name is sea</a>  
<br/>  

控制台上运行结果如下:

hello bigsea    
hello sea  

这里写图片描述
这里写图片描述

@RequestParam 绑定请求参数

在处理方法入参处使用 @RequestParam 可以把请求参数传递给请求方法

– value:参数名

– required:是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常

/** 
     * 如果 required = true 则表示请求参数对应的 字段 必须存在.如果不存在则会抛出异常
     * @param firstName 可以为null 
     * @param lastName 不能为null .为null报异常 
     * @param age age字段表示如果没有 age 参数 则默认值为 0  
     * @return 
    */  
    @RequestMapping("/requestParam")  
    public String requestParam(@RequestParam(value="firstName",required=false)String firstName,  
            @RequestParam( value="lastName" ,required = true) String lastName,  
            @RequestParam(value="age",required = false ,defaultValue="0")int age) {  
        System.out.println("hello my name is " + (firstName == null ? "" : firstName)  
                                        + lastName + "," + age +" years old this year");  
        return "helloworld";  
    }  
jsp 如下:
    <a href="requestParam?firstName=big&lastName=sea" > name is bigsea , age is 0 </a>  
<br/>  
<a href="requestParam?lastName=sea&age=23" > name is sea , age is 23 </a>  
<br/>  
<a href="requestParam" > throws exception </a>

控制台运行结果:

hello my name is bigsea,0 years old this year  
hello my name is sea,23 years old this yea

这里写图片描述

可以比较下@PathVariable和@RequestParam的请求路径
@PathVariable的url是这样的:http://host:port/…/path/参数值
/hello/pathVariable/bigsea

而@RequestParam的url是这样的:http://host:port/…/path?参数名=参数值
requestParam?lastName=sea&age=23

@RequestHeader 获取请求头

请求头包含了若干个属性,服务器可据此获知客户端的信息,通过
@RequestHeader 注解,可以把Request请求header部分的值绑定到方法的参数上。

示例代码:

这是一个Request 的header部分:
Host                    localhost:8080
Accept                  text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language         fr,en-gb;q=0.7,en;q=0.3
Accept-Encoding         gzip,deflate
Accept-Charset          ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive              300
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
                              @RequestHeader("Keep-Alive") long keepAlive)  {

  //...

}

那我们这里就可以通过相关的@RequestHeader 注解,将相关的request中header部分的属性值绑定到相关的参数上。

上面的代码,把request header部分的 Accept-Encoding的值,绑定到参数encoding上了, Keep-Alive header的值绑定到参数keepAlive上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值