Java constants in JSP [duplicate]

荒凉一梦 提交于 2019-11-30 06:44:17

问题


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 without using Scriptlet code such as:

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

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?

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?

Cheers, Don


回答1:


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);

and then access it in a jsp page

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

(you might have to create getters for each constant)




回答2:


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




回答3:


Looks like a duplicate of accessing constants in JSP (without 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.

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:

${bean.manager_role}

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




回答4:


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?




回答5:


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

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) :

    <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.




回答6:


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?


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

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



来源:https://stackoverflow.com/questions/127328/java-constants-in-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!