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

查看:74
本文介绍了在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标记不可见,该标记期望属性由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.

ETA:我给的旧链接无效.在此答案中可以找到新的链接: 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

current.user

current.user





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

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