struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Haroon Rafique <>
Subject RE: Using constants in JSP pages
Date Mon, 21 Mar 2005 15:19:31 GMT
On Today at 2:24pm, MC=>Marsh-Bourdon, Christopher <Christopher.Marsh-Bourd...:

MC> This is my way of doing this, and it may be a tad crap.  In the page 
MC> tag at the top of the JSP I add one or more imports like so:
MC> <%@ page language="java" import="com.marsh-bourdon.Constants" %>
MC> I then just refer to the constants within the page like so:
MC> <%= Constants.KEY_FOR_SOMETHING %>
MC> This should then be checked by your ide for typos etc.
MC> Cheers
MC> Christopher Marsh-Bourdon

Hi David, Christopher,

I'm sure other people will pipe in shortly, but a variation of this has 
been discussed in the mailing list before. From the archives:

In a nutshell:

1) Provide a method in your Constants file which puts all your Constants 
in a Map
2) At app startup make that map available to the application (e.g., in the 
3) Use JSTL, to now access your constants directly without scriptlets.

David, from your original email, I'm sure you could figure out a naming 
scheme to put constants from foo package and bar package under different 
names in the context (like Constants_foo and Constants_bar, etc). That 
should achieve 1).

To achieve 2), you could possibly use the following <c:out> construct.

<c:out value="${Constants_foo.FOO}">
    <span style="color: red">
    <fmt:message key="undefined.constant"/>

Since you're talking about JSP pages, I'm not at all sure how you can get 
the compiler to spot mistakes. But with the above JSTL, you could, at 
least, see it in a visible manner on the JSP page that an undefined 
constant was used.

Hope this was helpful.


MC> -----Original Message-----
MC> From: David Kennedy [] 
MC> Sent: 21 March 2005 14:18
MC> To: Struts Users Mailing List
MC> Subject: Using constants in JSP pages
MC> Most of our pages have a header/navbar which performs some highlighting 
MC> of the current page's category etc. This means all pages must define 
MC> beans to say things like, "My category is FOO, my sub-category is BAR"
MC> I would like to get the compiler to spot typos:
MC> (1) To make sure FOO matches one of the defined categories in the navbar.
MC> (2) To make sure the category variable itself is correct.
MC> I can achieve (1) by defining a simple constants class and using the 
MC> 'unstandard' taglib:
MC> 	<un: var="foo" type="" field="FOO"/>
MC> How can I achieve (2)? A little paranoid I know, but I've already 
MC> defined "fo" by mistake in a couple of places(*), and there doesn't seem 
MC> to be any advantage to using a constant if it is in turn defined by 
MC> another hand-typed variable!
MC> All I can think of is
MC> <un: var="<>" type="..." field="...">
MC> but if I'm going to use <%= %> I might as well just write
MC> 	<%= %>
MC> rather than use bind at all!
MC> (*) Where obviously the real var names are longer and easier to typo 
MC> than foo.

Haroon Rafique

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message