Spring MVC @CookieValue注解的使用

Spring MVC @CookieValue注解用于将请求的cookie数据映射到功能处理方法的参数上。

创建HelloWorldController

package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorldController {
	@RequestMapping("/cookieValueTest")
	public void cookieValueTest(@CookieValue(value="JSESSIONID")String sessionId) {
		System.out.println("通过@CookieValue获得JSESSIONID:"+sessionId);
	}
}

创建index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>index</title>
</head>
<body>
	<h4>测试@CookieValue注解</h4>
	<a href="cookieValueTest">cookieValueTest</a>
	<br>
</body>
</html>

启动Tomcat访问index.jsp

点击链接访问cookieValueTest,控制台输出:

猜你喜欢

转载自blog.csdn.net/dwenxue/article/details/81607452