You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by jt...@honda.co.nz on 2004/07/05 02:23:36 UTC

how to de-reference bean name using java method?





I'd like to be able to code the following line:

<c:out value="${Constants.HOME_DIR_KEY}"/>


where Constants.HOME_DIR_KEY is a java method that returns the name of bean
"home_directory".

The following two lines work:
<%= Constants.HOME_DIR_KEY %>                        // returns the value
"home_directory"
<c:out value="${home_directory} />    // returns the contents of bean named
"home_directory"

However nothing is written to the page with:
<c:out value="${Constants.HOME_DIR_KEY}"/>

I suppose it doesn't work because Constants.HOME_DIR_KEY isn't a bean.
Any ideas on how to de-reference Constants.HOME_DIR_KEY for <c:out> ?


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


RE: how to de-reference bean name using java method?

Posted by Erez Efrati <er...@netmedia.net.il>.
To make the following line work 
<c:out value="${Constants.HOME_DIR_KEY}"/>

You must store an instance of a class under the token name of
"Constants" in the session context and better yet in the servlet-context
so every page in your application would have access to it. Putting it in
the request or page context is useless in this case, cause you would
have to repeat this on every request.

As fas as I know, you cannot access a static member method from JSTL, so
you need to do something like: (put this line in your application
startup code).

ServletCtx.putAttribute (Constants.getInstance(), "Constants");

After this you can freely write <c:out value="${Constants.homeDir}" />

Assuming that the class has something like this:

Class Constants {
	
	public static final string HOME_DIR = "...";


	private static Constants m_instance == null;

	public String getHomeDir () {
		
		return HOME_DIR;
	}

	public Constants getInstance () {
		if (m_instance == null)
			m_instance = new Constants ();
		
		return m_instance;
	}
}


Hope this helps,

Erez

-----Original Message-----
From: jthompson@honda.co.nz [mailto:jthompson@honda.co.nz] 
Sent: Monday, July 05, 2004 2:24 AM
To: Struts Users Mailing List
Subject: how to de-reference bean name using java method?






I'd like to be able to code the following line:

<c:out value="${Constants.HOME_DIR_KEY}"/>


where Constants.HOME_DIR_KEY is a java method that returns the name of
bean
"home_directory".

The following two lines work:
<%= Constants.HOME_DIR_KEY %>                        // returns the
value
"home_directory"
<c:out value="${home_directory} />    // returns the contents of bean
named
"home_directory"

However nothing is written to the page with:
<c:out value="${Constants.HOME_DIR_KEY}"/>

I suppose it doesn't work because Constants.HOME_DIR_KEY isn't a bean.
Any ideas on how to de-reference Constants.HOME_DIR_KEY for <c:out> ?


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




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