我从一本Struts2教科书上拿了下面的代码示例,代码的目的是在Action类中设置一个cookie,然后jsp页面应该从cookie中取出内容然后显示。
登录动作类:
public class LoginAction implements Action,ServletResponseAware{
private HttpServletResponse response;
...
public void setServletResponse(HttpServletResponse response)
{
this.response=response;
}
public String execute() throws Exception
{
Cookie c= new Cookie("user",getUsername());
c.setMaxAge(60*60);
response.addCookie(c);
return SUCCESS;
}
JSP 页面:
<html>
<head>
<title>Cookie Success Page</title>
</head>
<body>
<br/>Welcome ${cookie.user.value}, thanks for logging in.
</body>
</html>
我现在遇到的问题是,${cookie.user.value}
无论我提供什么用户名,它都将始终显示为空白。
也许这不是在 Struts2 中设置 cookie 值的好方法?