访问 JSP 中的常量(没有 scriptlet) [英] accessing constants in JSP (without scriptlet)

查看:21
本文介绍了访问 JSP 中的常量(没有 scriptlet)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a class that defines the names of various session attributes, e.g.

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

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

I would like to use these constants within a JSP to test for the presence of these attributes, something like:

<%@ 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> 做到这一点,但我再次努力寻找正确的语法.

But I can't seem to get the sytax correct. Also, to avoid repeating the rather lengthy tests above in multiple places, I'd like to assign the result to a local (page-scoped) variable, and refer to that instead. I believe I can do this with <c:set>, but again I'm struggling to find the correct syntax.

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

UPDATE: Further to the suggestion below, I tried:

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

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

which didn't work. So instead, I tried substituting the literal value of the constant. I also added the constant to the content of the page, so I could verify the constant's value when the page is being rendered

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

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

This worked fine and it printed the expected value "current.user" on the page. I'm at a loss to explain why using the String literal works, but a reference to the constant doesn't, when the two appear to have the same value. Help.....

推荐答案

它在您的示例中不起作用,因为 ATTR_CURRENT_USER 常量对 JSTL 标记不可见,JSTL 标记期望属性由 getter 公开职能.我还没有尝试过,但是暴露常量的最干净的方法似乎是 非标准标签库.

It's not working in your example because the ATTR_CURRENT_USER constant is not visible to the JSTL tags, which expect properties to be exposed by getter functions. I haven't tried it, but the cleanest way to expose your constants appears to be the unstandard tag library.

预计到达时间:我提供的旧链接无效.可以在此答案中找到新链接:JSP 中的 Java 常量

ETA: Old link I gave didn't work. New links can be found in this answer: Java constants in JSP

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

Code snippets to clarify the behavior you're seeing: Sample class:

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 页面的片段,显示示例用法:

Snippet of the JSP page, showing sample usage:

<%-- 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 />

这输出:

current.user

current.user

当前用户



这篇关于访问 JSP 中的常量(没有 scriptlet)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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