【发布时间】:2018-02-02 09:26:11
【问题描述】:
我有一个 java Constant.java 文件,我在其中定义了这样的会话变量名称:
package com.pakageName;
public class Config {
// name of sessions var
public static final String ATT_SESSION_USER = "session_user";
public static final String ATT_SESSION_MESSAGE = "session_message";
...
}
在 Servlet 文件中,我使用以下内容来定义会话变量:
session.setAttribute(Constant.ATT_SESSION_MESSAGE, "this is the content of the var I wan't to display on the page");
Constant.java 文件包含在我的 jsp 文件中,使用:
<%@ page import="com.pakageName.Config" %>
所以我可以通过以下方式获取 sessionScope var 的内容:
// get the content of the session var
${sessionScope[Constant.ATT_SESSION_MESSAGE]}
// which return the same result as
${sessionScope.session_message}
问题是,如何使用带有常量变量值的删除标签删除这些会话变量?
我尝试了以下但 var 属性不接受 el 表达式...
// throw exception because var attribute doesn't accept el
<c:remove var="${sessionScope[Constant.ATT_SESSION_MESSAGE]}" scope="session" />
<c:remove var="${Constant.ATT_SESSION_MESSAGE}" scope="session" />
// don't remove anything
<c:remove var="Constant.ATT_SESSION_MESSAGE" />
// work but the name is hard coded
<c:remove var="session_message" scope="session" />
有什么想法吗?
【问题讨论】:
-
不应该只是:
<c:remove var="Constant.ATT_SESSION_MESSAGE" scope="session" />? -
否,因为这将尝试删除 Constant var 而不是名称为 Constant.ATT_SESSION_MESSAGE 内容的会话 var(该 var 称为“session_message”)
-
对不起,你错了。删除常数?我不知道你在说什么。 tutorialspoint.com/jsp/jstl_core_remove_tag.htm
-
我不想删除常量,常量允许我存储会话变量的名称。我想要的是删除名称是常量变量内容的会话变量。我已经编辑了帖子以解释更多