通过常量在JSP EL中获取变量不起作用

问题描述 投票:1回答:1

我使用GlassFish 4.1 web profile,据我所知使用EL 3.0。我做了所有这里解释的一切 - https://stackoverflow.com/a/3735006/5057736然而我的这个解决方案的实现不起作用。

这是我不变的课程

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

这是我设置属性的方式:

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

这是我的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>

这是我得到的输出

Method 1:TEST
Method 2:
Method 3:TEST

为什么方法2不起作用?

java jsp java-ee glassfish el
1个回答
0
投票
<c:set var="TITLE" value="<%=CommmonKeys.TITLE%>" />
Method 2:${requestScope[TITLE]}

按照上面的说法更改您的代码,应该可以正常工作。这个对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.