Getting variable in JSP EL by constant does not work
Asked Answered
L

1

1

I use GlassFish 4.1 web profile which as I understand uses EL 3.0. I did everything as was explained here - https://mcmap.net/q/194973/-how-to-reference-constants-in-el however my implementation of this solution doesn't work.

This is my constant class

public class CommonKeys {
    public static final String TITLE = "SOME_KEY";
}

This is how I set attribute:

request.setAttribute(CommonKeys.TITLE, "TEST");

This is my jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="org.temp.CommonKeys"%>

<div> Method 1:<%=request.getAttribute(CommonKeys.TITLE)%></div>
<div> Method 2:${requestScope[CommmonKeys.TITLE]}</div>
<div> Method 3:${requestScope["SOME_KEY"]}</div>

This is the output I get

Method 1:TEST
Method 2:
Method 3:TEST

Why does Method 2 not work?

Layfield answered 13/6, 2017 at 9:56 Comment(3)
@Jozef Chocholacek I did as was explained at the link you provided. However, it doesn't work. That is the reason of this question.Layfield
Looks like a bug/omission in the EL specification/implementation (no time to investigate where) that you cannot use constant as a key. I'd try <c:set var="_key" value="${CommmonKeys.TITLE}" /> and then ${requestScope[_key]}, but that would be just a workaround.Ephesus
@Jozef Chocholacek Thank you for your suggestions. I also think that something doesn't work. But what is this something I can't find - that is why I asked this question. Could you remove this possible duplicate?Layfield
F
0
<c:set var="TITLE" value="<%=CommmonKeys.TITLE%>" />
Method 2:${requestScope[TITLE]}

Change your code as per above, should be working fine. It works for me.

Fortuna answered 16/4, 2019 at 3:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.