20

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

Dónal
  • 185,044
  • 174
  • 569
  • 824

6 Answers6

4

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)

ncgz
  • 259
  • 1
  • 2
  • 8
1

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 https://rogerkeays.com/access-java-static-methods-and-constants-from-el

Roger Keays
  • 3,117
  • 1
  • 31
  • 23
1

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

Dónal
  • 185,044
  • 174
  • 569
  • 824
0

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?

maxp
  • 5,454
  • 7
  • 28
  • 30
0

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.

paulgreg
  • 18,493
  • 18
  • 46
  • 56
-3

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>

JeeBee
  • 17,476
  • 5
  • 50
  • 60