struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Haroon Rafique <haroon.rafi...@utoronto.ca>
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> 
MC> <%@ page language="java" import="com.marsh-bourdon.Constants" %>
MC> 
MC> I then just refer to the constants within the page like so:
MC> 
MC> <%= Constants.KEY_FOR_SOMETHING %>
MC> 
MC> This should then be checked by your ide for typos etc.
MC> 
MC> Cheers
MC> 
MC> Christopher Marsh-Bourdon
MC> www.marsh-bourdon.com
MC> 

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: 
http://www.mail-archive.com/user@struts.apache.org/msg06778.html

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 
context
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"/>
    </span>
</c:out>

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.

Cheers,

MC> -----Original Message-----
MC> From: David Kennedy [mailto:david.kennedy@swanlabs.com] 
MC> Sent: 21 March 2005 14:18
MC> To: Struts Users Mailing List
MC> Subject: Using constants in JSP pages
MC> 
MC> 
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> 
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> 
MC> I can achieve (1) by defining a simple constants class and using the 
MC> 'unstandard' taglib:
MC> 	<un: var="foo" type="com.foo.Constants" field="FOO"/>
MC> 
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> 
MC> All I can think of is
MC> <un: var="<%=com.foo.Constants.FOO_NAME%>" type="..." field="...">
MC> but if I'm going to use <%= %> I might as well just write
MC> 	<%= com.foo.Constants.FOO %>
MC> rather than use bind at all!
MC> 
MC> (*) Where obviously the real var names are longer and easier to typo 
MC> than foo.
MC> 

--
Haroon Rafique
<haroon.rafique@utoronto.ca>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message