JSP中的Java常量 [英] Java constants in JSP

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

问题描述

我有一个定义各种常量名称的类,例如

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

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

我想在JSP 中使用这些常量而不用使用Scriptlet代码,例如:

I would like to use these constants within a JSP without using Scriptlet code such as:

<%@ page import="com.example.Constants" %>
<%= Constants.ATTR_CURRENT_USER %>

Apache中似乎有一个标签 unstandard taglib,提供此功能。但是,我找不到任何方法来下载这个taglib。我开始怀疑它是否已被弃用并且功能已移至另一个(Apache)标记库?

There appears to be a tag in the Apache unstandard taglib that provides this functionality. However, I cannot find any way to download this taglib. I'm beginning to wonder if it's been deprecated and the functionality has been moved to another (Apache) tag library?

有谁知道我可以从哪里获得此库,或者如果它不可用,如果还有其他方法我可以在不使用scriptlet代码的情况下访问JSP中的常量吗?

Does anyone know where I can get this library, or if it's not available, if there's some other way I can access constants in a JSP without using scriptlet code?

干杯,
Don

Cheers, Don

推荐答案

在应用程序启动时,您可以将Constants类添加到servletContext,然后在任何jsp页面中访问它

On application startup, you can add the Constants class to the servletContext and then access it in any jsp page

servletContext.setAttribute("Constants", com.example.Constants);

然后在jsp页面中访问它

and then access it in a jsp page

<c:out value="${Constants.ATTR_CURRENT_USER}"/>

(您可能需要为每个常量创建getter)

(you might have to create getters for each constant)

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

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