Yes, I've settled on simply using the 'evil' <%=com.foo.BAR%> approach in my JSPs as (a) the most concise and readable (b) the most useful. I precompile all JSPs via Ant so the compiler can now 'see' typos.

Recipe 2.3 of Siggelkow's "Jakarta Struts Cookbook" (O'Reilly) discusses the map and also <unstandard:bind>. My problem with this is that it doesn't solve my immediate typo problem. Consider bind, as I discussed below, I have to give a 'var' argument which is a string. Problem: I can now typo the value of the var argument, sort of the same problem once removed. With the map suggestion - bleurgh, I really dislike it. Far too much code, and no real compiler help again as I'm back to asking for attributes by name really.

This book also suggests that <%=com.foo.BAR%> is rather evil, as it's the gateway to embedding entire programs in scriptlets, but I have to say that it's better than the solutions for this problem at least.

Cheers,
David Kennedy

Marsh-Bourdon, Christopher wrote:
Agreed, but with my fluff most ides (Eclipse, JBuilder etc) will parse and
throw an exception/error if the ... in <%= ... %> is undeclared or there is
a typo there.  That at least gives David the answer (albeit a bit of hash)
David was after.

Cheers

Christopher Marsh-Bourdon
www.marsh-bourdon.com

-----Original Message-----
From: Haroon Rafique [mailto:[EMAIL PROTECTED] Sent: 21 March 2005 15:20
To: Struts Users Mailing List
Subject: RE: Using constants in JSP pages



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:[EMAIL PROTECTED] 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
<[EMAIL PROTECTED]>


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------------------
The information contained herein is confidential and is intended solely for the
addressee. Access by any other party is unauthorised without the express written permission of the sender. If you are not the intended recipient, please contact the sender either via the company switchboard on +44 (0)20 7623 8000, or
via e-mail return. If you have received this e-mail in error or wish to read our
e-mail disclaimer statement and monitoring policy, please refer to http://www.drkw.com/disc/email/ or contact the sender. 3166
--------------------------------------------------------------------------------



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



--
David Kennedy
Swan Labs
http://www.swanlabs.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to