views:

2913

answers:

7

Hi,

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

A: 
JeeBee
If you could rewrite this without using Scriptlet, you will have answered my question.
Don
+1  A: 

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
I really want to avoid creating getters for each constant
Don
This was very helpful, thanks.
James Kingsbery
A: 

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
+1  A: 

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

Don
A: 

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
+1  A: 

I finally tracked down the unstandard tag library builds.

Don
A: 

Just refer to current.user in your JSP. In my seven years of Java programming, I've never gone back and changed the value the constant refers to and therefore had to update the JSPs. You simply ain't gonna need the indirection and flexibility that using the constant everywhere provides.

John Topley