找到你要的答案

Q:Session attribute not responding inside EL expression

Q:会话属性在EL表达式中未响应

I know this may look like a duplicate question. Unfortunately there is no acceptable, working answer. Even the OP was facing a different issue, not what the question it says.

POJO class below :

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 code below :

System.out.println(responseHeader.getAdmin()); //printed 'True'
session.setAttribute("header", responseHeader);
request.getRequestDispatcher("/DashBoard/Shipper").forward(request, response);

JSP below code :

<%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): 
%>

None of these below scenarios didn't work and I didn't get any Exceptions either.

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

我知道这可能看起来像一个重复的问题。不幸的是,没有可接受的,工作的答案。即使是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
answer1: 回答1:

There are a couple of problems here.

  1. Your bean (or "POJO" as you want to call it) doesn't adhere the Javabeans specification as to boolean properties. Particularly that boolean getters have the form of isPropertyName() and not getPropertyName().

    Fix it accordingly:

    private boolean admin = false;
    private boolean normal = false; 
    
    public void setAdmin(boolean admin) {
        this.admin = admin;
    }
    
    public boolean isAdmin() {
        return admin;
    }
    
    public void setNormal(boolean normal) {
        this.normal = normal;
    }
    
    public boolean isNormal() {
        return normal;
    }
    

  2. You're setting a session attribute on the name of a reserved EL variable referring the HTTP request header. Simply put: the ${header} variable is already reserved. This is supposed to be used to access a HTTP request header in EL such as ${header['User-Agent']}.

    Give it a different name:

    session.setAttribute("staticHeader", staticHeader);
    

    Note that I also renamed the Java variable for clarity, because this definitely doesn't represent a "response header". It would otherwise only be confusing to other people reading this code and possibly also to yourself later once you get more fluent in Java Servlets and HTTP (I only still wonder what exactly a "static header" means in this context, as an educated guess I think that you're overcomplicating "user roles" or "user groups", but alas).

    This way it's available in EL as ${staticHeader} and you don't need to mess with that ugly scriptlet workaround of putting a copy in the page context under a different name.


  3. Your attempts to access the bean property doesn't adhere the EL specification. You should be using the form of ${bean.propertyName} and not ${bean.PropertyName} and for sure not the other forms. If you really need the brace notation (because it's to be supplied as another variable), then you still need to make sure that the string value is propertyName as in ${bean['propertyName']}.

    So, this should do:

    <c:if test="${staticHeader.admin}">
    

    Do note that this doesn't access the private field, instead it invokes the isAdmin() getter method. Moreover, the presence of the private field is irrelevant to EL, you could even remove it altogether.


I warmly recommend you (and all commenters on the question) to take a pause on the JSP/Servlet project you're currently working and go through a sane JSP/Servlet book/tutorial. All of above is covered therein.

这里有几个问题。

  1. 你的豆(或“POJO”你想称呼它)不遵循JavaBeans规范作为布尔值属性。特别是,布尔干将有ispropertyname()形式和不getpropertyname()。

    相应地修复:

    private boolean admin = false;
    private boolean normal = false; 
    
    public void setAdmin(boolean admin) {
        this.admin = admin;
    }
    
    public boolean isAdmin() {
        return admin;
    }
    
    public void setNormal(boolean normal) {
        this.normal = normal;
    }
    
    public boolean isNormal() {
        return normal;
    }
    

  2. 你在保留EL变量指的HTTP请求标头的名称会话属性。简单地说:{头}变量已经被保留。这是用于访问在埃尔比如$ {头[ 'user-agent ]一个HTTP请求头}。

    给它一个不同的名字:

    session.setAttribute("staticHeader", staticHeader);
    

    请注意,我也更名为清晰的java的变量,因为这绝对不代表“响应头”。否则只会给其他人阅读这段代码混乱,也可能对自己以后一旦你得到更流利的java servlet和HTTP(我更想知道的是一个“静态头”就是在这样的背景下,作为一个受过教育的猜测,我认为你把事情复杂化的“用户角色”或“用户组”,但可惜)。

    这样可以在El达{ staticheader },你不需要那个丑陋的脚本的方法把一个复制页面中的背景下的一个不同的名字。


  3. 您访问bean属性的尝试不符合EL规范。你应该使用$ {豆的形式。属性名}和不达{豆。属性名},肯定不是其他形式。如果你真的需要支撑的符号(因为它是作为另一个变量),然后你还需要确保字符串值是属性名为{豆[ 'propertyname美元] }。

    所以,这应该做:

    <c:if test="${staticHeader.admin}">
    

    请注意,这并不是进入私人领域,而是调用isadmin() getter方法。此外,私人领域的存在与EL无关,你甚至可以完全移除它。


我热烈推荐你(对问题的所有人)采取暂停的JSP/Servlet项目你目前的工作和经过理智的JSP/Servlet的书/教程。以上所有内容都包括在内。

java  jsp  el  jsp-tags