Session attribute not responding inside EL expression

I know this might look like a duplicate question . Unfortunately there is no acceptable working answer. Even the OP ran into a different problem than what he is talking about.

The POJO class is 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

      

The servlet code is 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 scripts worked and I also didn't get any Exceptions.

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

      

+3


source to share


1 answer


There are a couple of problems here.

  • Your bean (or "POJO" as you want to call it) does not adhere to the Javabeans specification as boolean properties . In particular, that getters boolean

    are of the form isPropertyName()

    , not getPropertyName()

    .

    Correct 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;
    }
    
          


  • You are setting the session attribute of the EL reserved variable name by referencing the HTTP Request Header . Simply put: the variable is ${header}

    already reserved. It is supposed to be used to access the HTTP request header in EL, for example ${header['User-Agent']}

    .

    Please enter another name:

    session.setAttribute("staticHeader", staticHeader);
    
          

    Note that I also renamed the Java variable for clarity, because this definitely does not represent a " response header ". Otherwise, it would be easy to confuse other people reading this code and possibly also yourself later, once you have a fuller knowledge of Java servlets and HTTP (I'm just wondering what exactly "static header" means in in this context, as an educated guess I think you are abusing "user roles" or "user groups", but alas).

    This way it is available in EL as ${staticHeader}

    , and you don't have to fiddle with this ugly script workaround by pasting a copy into the page context under a different name.


  • Your attempts to access the bean property are not EL spec . You should use a form ${bean.propertyName}

    , not ${bean.propertyName}

    and of course not other forms. If you really want the parenthesis notation (because it will be supplied as another variable), you still need to make sure the string value propertyName

    is as in ${bean['propertyName']}

    .

    So this should do:

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

    Note that this does not access the field private

    , it calls the isAdmin()

    getter method instead . Moreover, the presence of a field private

    has nothing to do with EL, you can even remove it altogether.




I highly recommend that you (and all the commenters on the question) pause on the JSP / Servlet project you are currently working on and go through a sane JSP / Servlet book / tutorial. All of the above is described in it.

+3


source







All Articles