2017-08-24 121 views
0

我有,我定义的会话变量名称这样一个java Constant.java文件:JSTL动态移除标签

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的文件,我用下面的定义会话VAR:

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" %> 

所以我能够得到sessionSco的内容PE与VAR:

// get the content of the session var 
${sessionScope[Constant.ATT_SESSION_MESSAGE]} 
// which return the same result as 
${sessionScope.session_message} 

问题是,我怎么能删除使用删除与恒VAR值标签的会议无功?

我试过以下,但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" /> 

任何想法?

+0

不应该只是:''? –

+0

不,因为这将尝试删除常量变量,而不是名称为Constant.ATT_SESSION_MESSAGE(该变量称为“session_message”)内容的会话变量 –

+0

对不起,但你错了。删除常数?我不知道你在做什么。 https://www.tutorialspoint.com/jsp/jstl_core_remove_tag.htm –

回答

0

从我能看到的问题是,你实际上并没有设置一个会话变量。你所做的只是调用一个公共变量。所以当你认为你要删除一个会话变量时,你实际上并没有做任何事情。这就是为什么它会持续下去。

尝试实际上首先设置一个会话变量有:

session.setAttribute("user", Constant.ATT_SESSION_MESSAGE); 

或JSTL:

<c:set var="user" value="${Constant.ATT_SESSION_MESSAGE}" scope="session" /> 

然后将其删除:

<c:remove var="user"/> 
+0

我已经在servlet中使用了 session.setAttribute(Constant.ATT_SESSION_MESSAGE,“message sample”);当 然后我用 $ {sessionScope [Constant.ATT_SESSION_MESSAGE]} 这就是为什么我不能用删除标签,变种的名称在另一个文件 –

+0

首先定义调用它在JSP页面中,你设置了一个会话变量,引号先去吧!其次,一旦你设置了一个会话变量,你只能通过你在引号中写的内容来访问它。所以:“session.getAttribute(”messageSample“);”。你在变量中不能有空格... –

+0

我已经编辑了这篇文章来解释更多我尝试去做的事情:messageSample是我在会话变量var中存储var的内容,其名称是session_message:通过session.getAttribute(Constant.ATT_SESSION_MESSAGE)访问var,它返回“messageSample” –

0

我已经找到一种方法,用jsp scriptlet实现我想要的...

<% session.removeAttribute(Config.ATT_SESSION_MESSAGE); %> 

有没有办法与jstl获得相同的结果?