通过常量在JSP EL中获取变量不起作用 [英] Getting variable in JSP EL by constant does not work

查看:172
本文介绍了通过常量在JSP EL中获取变量不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GlassFish 4.1 Web配置文件,据我了解,它使用EL 3.0.我已按照此处的说明进行了所有操作- https://stackoverflow.com/a/3735006/5057736 但是我对此的实现解决方案不起作用.

I use GlassFish 4.1 web profile which as I understand uses EL 3.0. I did everything as was explained here - https://stackoverflow.com/a/3735006/5057736 however my implementation of this solution doesn't work.

这是我的恒定班级

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

这是我设置属性的方式:

This is how I set attribute:

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不起作用?

Why does Method 2 not work?

推荐答案

<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.

这篇关于通过常量在JSP EL中获取变量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆