Spring MVC Cookie example
🏷️ Spring MVC
设置 Cookie
java
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
//..
@RequestMapping("/hello.html")
public String hello(HttpServletResponse response) {
response.addCookie(new Cookie("foo", "bar"));
//..
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
获取 Cookie
java
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
//..
@RequestMapping("/hello.html")
public String hello(
@CookieValue(value = "foo", defaultValue = "hello") String fooCookie) {
//..
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9