【发布时间】:2015-07-12 00:04:46
【问题描述】:
我知道这可能看起来像 duplicate question。不幸的是,没有可接受的有效答案。甚至 OP 也面临着不同的问题,而不是它所说的问题。
POJO 类如下:
private boolean admin = false;
private boolean isNormal = false;
public void setAdmin(boolean admin) {
this.admin = admin;
}
public boolean getAdmin() {
return admin;
}
public void setIsNormal(boolean isNormal) {
this.isNormal= isNormal;
}
public boolean getIsNormal() {
return isNormal;
}
// In this class I have many boolean flags like above two. I need to access those in the my JSP
Servlet 代码如下:
System.out.println(responseHeader.getAdmin()); //printed 'True'
session.setAttribute("header", responseHeader);
request.getRequestDispatcher("/DashBoard/Shipper").forward(request, response);
JSP 下面的代码:
<%StaticHeader sh = (StaticHeader)session.getAttribute("header");//getting the StaticHeader Object from the session
pageContext.setAttribute("headerFromSession",sh); // set the StaticHeader Object again into PageContext (may be unnecessary):
%>
以下所有场景都不起作用,我也没有遇到任何异常。
1.) <c:if test="${headerFromSession.getAdmin()}"> //seems to be standard, formal way. But it didn't work
2.) <c:if test="${headerFromSession.Admin}"> // Is this legal? I mean, 'admin' is a private variable.
3.) <c:if test="${headerFromSession.ADMIN}">
4.) <c:if test="${headerFromSession[Admin]}">
5.) <c:if test="${headerFromSession[ADMIN]}">
6.) <c:if test="${headerFromSession}"> //This seems like totally not correct. Because I have many boolean flages which I have already set to the StaticHeader Object
【问题讨论】:
-
能否请您出示您的 servlet 代码?这将有助于更好地理解
-
好的,我假设
header是StaticHeader的一种类型,那么您如何设置SHIPPER的SHIPPER的属性? -
如果
header已经正确设置为更宽的范围,那么为什么需要再次将其设置为更窄的范围?此外,使用 JSTL 完全排除 Scriptlets。 -
是的,为什么不只使用
<c:if test="${header}">而不是<c:if test="${header.ADMIN}">,因为header还包含布尔值。 -
@Tiny 我认为会话变量不能在 EL 中直接访问。所以我把它放在PageContext中。可能是额外的工作,但我面临的问题与此无关。不是吗?