Hello again,

to be used in a JSF value binding expression (opposed to my example using JSP 
expression language) you may have to use the prefix "applicationScope" to 
reference the fields of the Constants class.

So - to use your example - you would have to write something like the following:

<h:inputHidden id="a"
               value="#{applicationScope.Constants.LOGIN}" />

Hope that helps...

Cheers,

René


----- Original Message -----
From: René Zanner
[mailto:[EMAIL PROTECTED]
To: [email protected]
Sent: Tue, 26
Sep 2006 09:31:53 +0200
Subject: Re: How to reference a static value in
JSF


> Hi "JS Portal Support",
> 
> sorry about the last incomplete post - my mail client was so eager to send
> that mail away - I could not stop him from doing so... ;o)
> 
> Last year I did something you intend. I was heavily disturbed about how
> ignorant the JSF standard is about this basic programming requirement to use
> constants instead of hard-coded values in your JSPs. 
> 
> I put all my application wide constants in a single "Constants" class. Then
> I used a ServletContextListener to put all the fields via introspection in a
> Map in the application scope (not as a backing bean, but as ServletContext
> attribute) using the key "Constants".
> 
> Constants class:
> ----------------
> 
> public final class Constants {
> 
>       // ID for reference from the ServletContext:
>       public static final String ID = "Constants";
>       
>       // other constants follow...
>       public static final String TAB_DETAILS = "tabDetails";
>       ...
>       
>       // "cache" holding all public fields by it's field name:
>       private static Map nameToValueMap = createNameToValueMap();
>       
>       /**
>        * Puts all public static fields into the resulting Map.
>        * Uses the name of the field as key to reference it's in the Map.
>        * 
>        * @return a Map of field names to field values of 
>        *         all public static fields of this class
>        */
>       private static Map createNameToValueMap() {
>               Map result = new HashMap();
>               Field[] publicFields = Constants.class.getFields();
>               for (int i = 0; i < publicFields.length; i++) {
>                       Field field = publicFields[i];
>                       String name = field.getName();
>                       try {
>                               result.put(name, field.get(null));
>                       } catch (Exception e) {
>                               System.err.println("Initialization of Constants 
> class failed!");
>                               e.printStackTrace(System.err);
>                       }
>               }
>               return result;
>       }
>       
>       public static Map getNameToValueMap() {
>               return nameToValueMap;
>       }
> 
> }
> 
> Listener class:
> ---------------
> 
> ...
> public void contextInitialized(ServletContextEvent event) {
>       ServletContext sc = event.getServletContext();
>       sc.setAttribute(Constants.ID, Constants.getNameToValueMap());
> }
> ...
> 
> This way I could access all my constants in the JSPs via JSF expression:
> 
>    <c:if test="${globalView.linieTab eq Constants.TAB_DETAILS}">
> 
> Since the expression language allows you to reference mapped values via
> Map['key'] or Map.key, I could use the "dot" syntax to mimic a normal static
> access to the Constants classes fields.
> 
> I hope this helps you achieve your goal of reusable constant values
> throughout your application.
> 
> Ciao,
> 
> René
> 
> 
> ----- Original Message -----
> From: JS Portal Support
> [mailto:[EMAIL PROTECTED]
> To: [email protected]
> Sent: Mon, 25 Sep
> 2006 13:20:13 +0200
> Subject: How to reference a static value in JSF
> 
> 
> > Hi,
> > 
> > Just started to implement Shale and JSF. As I've always worked more with
> > scriptlets and Servlets I can't seem to figure out how to reference a
> public
> > static value from one of my classes in my JSP's. I've always used:
> > 
> > <input type="hidden" name="a"
> > value="<%=com.jsportal.projectportal.web.webActions.login%>"/>
> > 
> > Now I try to achieve this with the following:
> > 
> > <h:inputHidden id="a"
> > value="${com.jsportal.projectportal.web.webActions.login}"/>
> > 
> > I understand this doesn't work as it will try to call
> > com.getJsportal()....etc. But how do I do it, as the <%=%> is not allowed
> in
> > these taglib tags?
> > 
> > Thanks,
> > Joost
> > 
> > 
> 

Reply via email to