Sunday, March 27, 2011

Java constants in JSP

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

From stackoverflow
  • 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" %>
    <%
    User user = (User) session.getAttribute(Constants.ATTR_CURRENT_USER);
    %>
    
    <h1>Welcome <%=user.getFirstName()%></h1>
    
    
    Don : If you could rewrite this without using Scriptlet, you will have answered my question.
  • 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)

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

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

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

  • I finally tracked down the unstandard tag library builds.

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

0 comments:

Post a Comment