@PathVariable,@RequestHeader,@CookieValue注释的使用

1.建立控制器

 

package masterSpringMvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DataBindingController
{
    //入口
    @RequestMapping("/test")
    public String index()
    {
        return "test";
    }
    
    //测试@PathVariable注释
    @RequestMapping(value="/pathVariableTest/{userId}")
    public String pathVariableTest(
            @PathVariable Integer userId,
            Model model
            )
    {
        model.addAttribute("userId",userId);
        return "pathVariableTest";
    }
    
    //测试RequestHeader注释
    @RequestMapping(value="/requestHeaderTest")
    public String requestHeaderTest(
            @RequestHeader("User-Agent") String userAgent,
            @RequestHeader(value="Accept") String accepts,
            Model model
            )
    {
        model.addAttribute("userAgent", userAgent);
        model.addAttribute("accepts", accepts);
        return "requestHeaderTest";
    }
    
    //测试@CookieValue注释
    @RequestMapping(value="/cookieValueTest")
    public String cookieValueTest(
            @CookieValue(value="JSESSIONID",defaultValue="none") String sessionId,
            Model model
            )
    {
        model.addAttribute("sessionId",sessionId);
        return "cookieValueTest";
    }
    
}

2.建立视图

pathVariableTest

requestHeaderTest

cookieValueTest

test