Spring MVC @RequestParam @RequestHeader @CookieValue用法

 

 

摘要: package com.hust.springmvc1; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation

package com.hust.springmvc1;

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

@Controller
@RequestMapping("/springmvc")
public class SpringMVCTest { 

    private static final String SUCCESS = "success";

    /**
     * 了解:
     * @CookieValue: 映射一个Cookie值。 属性同@RequestParam
     */
    @RequestMapping("/testCookieValue")
    public String testCookieValue(@CookieValue("JSESSIONID") String sessionId) {
        System.out.println("CookieValue sessionId: " + sessionId);
        return SUCCESS;
    }

    /**
     * 了解:
     * 映射请求头信息
     * 用法@RequestParam
     */
    @RequestMapping("/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value="Accept-Language") String al) {
        System.out.println("testRequestHeader, Accept-Language: " + al);
        return SUCCESS;
    }   

    /**
     * @RequestParam 来映射请求参数
     * value 值即请求参数的参数名
     * required 该参数是来认定传进来的参数是否是必须给,默认为true
     * defaultValue 请求参数的默认值
     */
    @RequestMapping(value="/testRequestParam")
    public String testRequestParam(@RequestParam(value="username") String un,
            @RequestParam(value="age", required = false, defaultValue = "0") Integer age) {
        System.out.println("testRequestParam, username: " + un + "age: " + age);
        return SUCCESS;
    }
}

@RequestParam 来映射请求参数 
* value 值即请求参数的参数名 
* required 该参数是来认定传进来的参数是否是必须给,默认为true 
* defaultValue 请求参数的默认值

posted @ 2017-10-05 19:53  青春有多美  阅读(226)  评论(0编辑  收藏  举报