我从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 值的好方法?
最佳答案
cookie
只是幕后的 Map
。要从 EL 访问 Map 接口(interface),请使用 ${cookie["user"].value}
关于java - Struts2中设置cookie值的正确方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14048735/