java - 访问 JSP 中的常量(没有 scriptlet)

标签 java jsp session jstl

我有一个定义各种 session 属性名称的类,例如

class Constants {
    public static final String ATTR_CURRENT_USER = "current.user";
}

我想在 JSP 中使用这些常量来测试这些属性是否存在,例如:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.example.Constants" %>

<c:if test="${sessionScope[Constants.ATTR_CURRENT_USER] eq null}">
    <%-- Do somthing --%>
</c:if>

但我似乎无法正确获取语法。此外,为了避免在多个地方重复上面相当冗长的测试,我想将结果分配给本地(页面范围)变量,并改为引用它。我相信我可以用 <c:set> 做到这一点,但我再次努力寻找正确的语法。

更新:根据以下建议,我尝试了:

<c:set var="nullUser" scope="session"
value="${sessionScope[Constants.ATTR_CURRENT_USER] eq null}" />

这没有用。因此,我尝试替换常量的字面值。我还将常量添加到页面的内容中,因此可以在呈现页面时验证常量的值

<c:set var="nullUser" scope="session"
value="${sessionScope['current.user'] eq null}" />
<%= "Constant value: " + WebHelper.ATTR_CURRENT_PARTNER %>

这很好,它在页面上打印了预期值“current.user”。我无法解释为什么使用 String 文字有效,但是当两者似乎具有相同的值时,对常量的引用却不起作用。帮助.....

最佳答案

它在您的示例中不起作用,因为 ATTR_CURRENT_USER 常量对 JSTL 标记不可见,这些标记期望属性由 getter 函数公开。我还没有尝试过,但公开常量的最简洁方法似乎是 unstandard tag library .

ETA:我提供的旧链接无效。可以在此答案中找到新链接:Java constants in JSP

阐明您所看到的行为的代码片段: 示例类:

package com.example;

public class Constants
{
    // attribute, visible to the scriptlet
    public static final String ATTR_CURRENT_USER = "current.user";

    // getter function;
    // name modified to make it clear, later on, 
    // that I am calling this function
    // and not accessing the constant
    public String getATTR_CURRENT_USER_FUNC()
    {
        return ATTR_CURRENT_USER;
    }


}    

JSP 页面的片段,显示示例用法:

<%-- Set up the current user --%>
<%
    session.setAttribute("current.user", "Me");
%>

<%-- scriptlets --%>
<%@ page import="com.example.Constants" %>
<h1>Using scriptlets</h1>
<h3>Constants.ATTR_CURRENT_USER</h3>
<%=Constants.ATTR_CURRENT_USER%> <br />
<h3>Session[Constants.ATTR_CURRENT_USER]</h3>
<%=session.getAttribute(Constants.ATTR_CURRENT_USER)%>

<%-- JSTL --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="cons" class="com.example.Constants" scope="session"/>

<h1>Using JSTL</h1>
<h3>Constants.getATTR_CURRENT_USER_FUNC()</h3>
<c:out value="${cons.ATTR_CURRENT_USER_FUNC}"/>
<h3>Session[Constants.getATTR_CURRENT_USER_FUNC()]</h3>
<c:out value="${sessionScope[cons.ATTR_CURRENT_USER_FUNC]}"/>
<h3>Constants.ATTR_CURRENT_USER</h3>
<c:out value="${sessionScope[Constants.ATTR_CURRENT_USER]}"/>
<%--
Commented out, because otherwise will error:
The class 'com.example.Constants' does not have the property 'ATTR_CURRENT_USER'.

<h3>cons.ATTR_CURRENT_USER</h3>
<c:out value="${sessionScope[cons.ATTR_CURRENT_USER]}"/>
--%>
<hr />

这个输出:

使用小脚本

常量.ATTR_CURRENT_USER

当前用户

session [Constants.ATTR_CURRENT_USER]


使用 JSTL

Constants.getATTR_CURRENT_USER_FUNC()

当前用户

session [Constants.getATTR_CURRENT_USER_FUNC()]

常量.ATTR_CURRENT_USER



关于java - 访问 JSP 中的常量(没有 scriptlet),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/122254/

相关文章:

java - 用于网页警报的事件监听器

jquery - onmouseover 显示存储在数据库中的 blob 中的图像

jsp - jSTL - forEach,具有 bean 列表作为属性的 bean

asp.net - 使用 Azure Redis 缓存 session 状态提供程序维护跨不同域的 session

php - 如何在 PHP 中回显动态 session 变量?

java - 函数调用与i+1,++i的实现有区别吗?

java - java 数组上出现 NullPointerException,已创建新对象

java - 链表索引计数器

java - spring-mvc错误: Neither BindingResult nor plain target object for bean name 'userBean' available as request attribute

javascript - Backbone.js、Rest API 和匿名 session