JSP 中的 Java 常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/127328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:36:48  来源:igfitidea点击:

Java constants in JSP

javajspconstantsjsp-tags

提问by Dónal

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

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

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

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

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

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

There appears to be a tag in the Apache unstandardtaglib 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?

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

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?

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

Cheers, Don

干杯,唐

回答by JeeBee

Why do you want to print the value of the constant on the JSP? Surely you are defining them so that in the JSP you can extract objects from the session and request before you present them?

为什么要在 JSP 上打印常量的值?您确定要定义它们,以便在 JSP 中您可以在呈现对象之前从会话和请求中提取对象吗?

<%@ page import="com.example.Constants" %>
<%@ page import="com.example.model.User" %>
<%
User user = (User) session.getAttribute(Constants.ATTR_CURRENT_USER);
%>

<h1>Welcome <%=user.getFirstName()%></h1>

回答by ncgz

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

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

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

and then access it in a jsp page

然后在jsp页面访问

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

(you might have to create getters for each constant)

(您可能必须为每个常量创建吸气剂)

回答by maxp

What kind of functionality do you want to use? That tag sould be able to access any public class field by class name and field name?

你想使用什么样的功能?该标签应该能够通过类名和字段名访问任何公共类字段吗?

Scriptlets linking done at compile time but taglib class field access has to use such java API as reflection at runtime. Do You really need that?

Scriptlet 链接在编译时完成,但 taglib 类字段访问必须在运行时使用 Java API 作为反射。你真的需要吗?

回答by Dónal

Turns out there's another tag librarythat provides the same functionality. It also works for Enum constants.

原来还有另一个提供相同功能的标签库。它也适用于枚举常量。

回答by paulgreg

I'll use jakarta-taglibs-unstandard-20060829.jar in my project but, you're true, it seems not available for download anymore.

我将在我的项目中使用 jakarta-taglibs-unstandard-20060829.jar 但是,你是真的,它似乎不再可供下载

I've got that in my pom.xml in order to get that library but I think It will work only because that library is now on my local repository (I cannot find it in official repositories) :

我在我的 pom.xml 中有它以获取该库,但我认为它只能工作,因为该库现在在我的本地存储库中(我在官方存储库中找不到它):

    <dependency>
        <groupId>jakarta</groupId>
        <artifactId>jakarta-taglibs-unstandard</artifactId>
        <version>20060829</version>
    </dependency>

I do not know if there's another alternative.

不知道有没有别的选择。

I hope so because it was a good way to access constants in JSP.

我希望如此,因为这是在 JSP 中访问常量的好方法。

回答by Roger Keays

Looks like a duplicate of accessing constants in JSP (without scriptlet)

看起来像在 JSP访问常量的副本(没有 scriptlet)

My answer was:

我的回答是:

Static properties aren't accessible in EL. The workaround I use is to create a non-static variable which assigns itself to the static value.

在 EL 中无法访问静态属性。我使用的解决方法是创建一个非静态变量,该变量将自身分配给静态值。

public final static String MANAGER_ROLE = 'manager';
public String manager_role = MANAGER_ROLE;

I use lombok to generate the getter and setter so that's pretty well it. Your EL looks like this:

我使用 lombok 来生成 getter 和 setter,这样就很好了。你的 EL 看起来像这样:

${bean.manager_role}

Full code at http://www.ninthavenue.com.au/java-static-constants-in-jsp-and-jsf-el

完整代码位于http://www.ninthavenue.com.au/java-static-constants-in-jsp-and-jsf-el