2017-08-24 119 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獲得相同的結果?