You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Woodchuck <wo...@yahoo.com> on 2004/08/27 17:38:33 UTC
Struts or JSTL equivalent for this?
how can i do the following the non-scriptlet way on my jsp page?
<%@ page import="MyPackage.Constants"%>
<%= Constants.BUTTON__KEY %>
is there an elegant Struts or JSTL equivalent for the above?
thanks in advance!
__________________________________
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Struts or JSTL equivalent for this?
Posted by Emmanouil Batsis <Em...@eurodyn.com>.
Of course supplying getters for your constant values would work like a
charm for JSTL ;-)
Rick Reumann wrote:
> Woodchuck wrote:
>
>> how can i do the following the non-scriptlet way on my jsp page?
>>
>> <%@ page import="MyPackage.Constants"%>
>>
>> <%= Constants.BUTTON__KEY %>
>>
>>
>> is there an elegant Struts or JSTL equivalent for the above?
>
>
> Thankfully Kris Schneider demonstrated a good way to handle this...
>
> In your Constants class add a method similar to this:
>
> public static Map getConstantsMap() {
> Map propMap = null;
> try {
> Field[] allFields = Constants.class.getDeclaredFields();
> int numFields = allFields.length;
> propMap = new HashMap(numFields);
> for(int i = 0; i < numFields; i++) {
> Field f = allFields[i];
> int mods = f.getModifiers();
> if(Modifier.isPublic(mods) && Modifier.isStatic(mods) &&
> Modifier.isFinal(mods)) {
> String name = f.getName();
> Object value = f.get(null);
> propMap.put(name, value);
> }
> }
> } catch(IllegalAccessException ie) {
> log.error("Problem loading getConstantsMap " + ie);
> }
>
> return Collections.unmodifiableMap(propMap);
> }
>
>
> Then, I have a Servlet that runs on applications startup (such as a
> ServletContextListener) that will load the constants map into
> application scope:
>
> ServletContext context = contextEvent.getServletContext();
> try {...
> context.setAttribute("CONSTANTS", Constants.getConstantsMap());
>
> Now anywhere in your JSPs you can just use the Map
>
> ${CONSTANTS.SOME_CONSTANT}
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Struts or JSTL equivalent for this?
Posted by Rick Reumann <st...@reumann.net>.
Woodchuck wrote:
> ugh. i don't want to use scriptlets but it seems so much easier to.
> do you think it's bad to mix scriptlets with Struts and JSTL?
For the most part, I think so yes. I'd use the Unstandard Tag you posted
http://marc.theaimsgroup.com/?l=struts-user&m=109352654917026&w=2
(which, yes, I'm sure if we looked at the tag it's just doing some
introspection to pull out the public static property).
I still find it simple to add the one method I mentioned to your
Constants class... or you can even pull out the toMap method and add it
to a util class passing in an Object as an argument. I usually only have
one constants class for the Struts portion of my application so it's not
a big deal for me to just add that one toMap method.
I also always have one Servlet that initializes on application start up
so having it place that constants Map into application socpe is simple
also.
I like this approach a bit better than the unstandard bind, because I
don't have to call the un:bind (which I'm guessing acts like a set) in
order to use the constant on the page. All my Constants are loaded into
a Map put into application scope which is always available.
--
Rick
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Struts or JSTL equivalent for this?
Posted by Woodchuck <wo...@yahoo.com>.
hi Rick,
with the help of Paul McCulloch, the best i've found so far is this:
http://marc.theaimsgroup.com/?l=struts-user&m=109352654917026&w=2
it uses a tag library, which i suspect the tag is doing something
similar to what you suggested initially
the tag binds the static variable that you want to a variable that JSTL
can see/use.
ugh. i don't want to use scriptlets but it seems so much easier to.
do you think it's bad to mix scriptlets with Struts and JSTL? or do
all of your jsps follow a pure Struts/JSTL format with absolutely no
scriptlets?
woodchuck
--- Rick Reumann <st...@reumann.net> wrote:
> Woodchuck wrote:
>
> > before i use this suggestion, would anyone else like to challenge
> this
> > solution with a *more elegant* solution? ;)
>
> If you find one, let me know.
>
> --
> Rick
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
_______________________________
Do you Yahoo!?
Win 1 of 4,000 free domain names from Yahoo! Enter now.
http://promotions.yahoo.com/goldrush
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Struts or JSTL equivalent for this?
Posted by Rick Reumann <st...@reumann.net>.
Woodchuck wrote:
> before i use this suggestion, would anyone else like to challenge this
> solution with a *more elegant* solution? ;)
If you find one, let me know.
--
Rick
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Struts or JSTL equivalent for this?
Posted by Woodchuck <wo...@yahoo.com>.
lol! not what i was expecting, but very interesting! :)
before i use this suggestion, would anyone else like to challenge this
solution with a *more elegant* solution? ;)
and without making getters and setters!!
woodchuck
--- Rick Reumann <st...@reumann.net> wrote:
> Woodchuck wrote:
>
> > how can i do the following the non-scriptlet way on my jsp page?
> >
> > <%@ page import="MyPackage.Constants"%>
> >
> > <%= Constants.BUTTON__KEY %>
> >
> >
> > is there an elegant Struts or JSTL equivalent for the above?
>
> Thankfully Kris Schneider demonstrated a good way to handle this...
>
> In your Constants class add a method similar to this:
>
> public static Map getConstantsMap() {
> Map propMap = null;
> try {
> Field[] allFields = Constants.class.getDeclaredFields();
> int numFields = allFields.length;
> propMap = new HashMap(numFields);
> for(int i = 0; i < numFields; i++) {
> Field f = allFields[i];
> int mods = f.getModifiers();
> if(Modifier.isPublic(mods) && Modifier.isStatic(mods) &&
>
> Modifier.isFinal(mods)) {
> String name = f.getName();
> Object value = f.get(null);
> propMap.put(name, value);
> }
> }
> } catch(IllegalAccessException ie) {
> log.error("Problem loading getConstantsMap " + ie);
> }
>
> return Collections.unmodifiableMap(propMap);
> }
>
>
> Then, I have a Servlet that runs on applications startup (such as a
> ServletContextListener) that will load the constants map into
> application scope:
>
> ServletContext context = contextEvent.getServletContext();
> try {...
> context.setAttribute("CONSTANTS", Constants.getConstantsMap());
>
> Now anywhere in your JSPs you can just use the Map
>
> ${CONSTANTS.SOME_CONSTANT}
>
>
> --
> Rick
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
_______________________________
Do you Yahoo!?
Win 1 of 4,000 free domain names from Yahoo! Enter now.
http://promotions.yahoo.com/goldrush
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Struts or JSTL equivalent for this?
Posted by Rick Reumann <st...@reumann.net>.
Woodchuck wrote:
> how can i do the following the non-scriptlet way on my jsp page?
>
> <%@ page import="MyPackage.Constants"%>
>
> <%= Constants.BUTTON__KEY %>
>
>
> is there an elegant Struts or JSTL equivalent for the above?
Thankfully Kris Schneider demonstrated a good way to handle this...
In your Constants class add a method similar to this:
public static Map getConstantsMap() {
Map propMap = null;
try {
Field[] allFields = Constants.class.getDeclaredFields();
int numFields = allFields.length;
propMap = new HashMap(numFields);
for(int i = 0; i < numFields; i++) {
Field f = allFields[i];
int mods = f.getModifiers();
if(Modifier.isPublic(mods) && Modifier.isStatic(mods) &&
Modifier.isFinal(mods)) {
String name = f.getName();
Object value = f.get(null);
propMap.put(name, value);
}
}
} catch(IllegalAccessException ie) {
log.error("Problem loading getConstantsMap " + ie);
}
return Collections.unmodifiableMap(propMap);
}
Then, I have a Servlet that runs on applications startup (such as a
ServletContextListener) that will load the constants map into
application scope:
ServletContext context = contextEvent.getServletContext();
try {...
context.setAttribute("CONSTANTS", Constants.getConstantsMap());
Now anywhere in your JSPs you can just use the Map
${CONSTANTS.SOME_CONSTANT}
--
Rick
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org