You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Glanville, Jay" <Ja...@NaturalConvergence.com> on 2004/02/18 15:06:19 UTC
can I use resource keys in html:optionsCollection?
I've looked in the documentation and I can't find my answer, so I'm
assuming that the answer is "no". However, I'm posing the question to
this list in case I've missed something.
I'm using an <html:optionsCollection> tag to display a list of options
inside my <html:select> tag. However, I want the labels not to be
directly populated from collection.label, but from my resource bundle
through keys. In other words, I don't want my collection to be a set of
label - value pairs, but to be a set of label key - value pairs.
Is there a way to have optionsCollection use label keys as opposed to
labels?
I've thought of two ways that I can work around this limitation, but I'd
prefer to use the simpler html:optionsCollection method. My workarounds
are:
1) to use <logic:iterate> to iterate over the collection, and then use
individual <html:option> tags using the key attribute.
2) when creating my collection, resolve my bundle key into a bundle
value for the collection's value attribute.
So, suggestions? Feedback? Help?
Thanks
JDG
--
Jay Glanville
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
Re: can I use resource keys in html:optionsCollection?
Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
No you can't use <html:optionsCollection> tag to display labels.
I also can't think why this would be necessary - if you define your options
in your resource bundle, you know how many there are going to be and so you
can just use straight <html:option> tags. For example:
In your resource bundle:
order.status.placed=Order Placed
order.status.delivered=Goods Deleivered
order.status.cancelled=Cancelled
In yout jsp:
<html:option value="1" key="order.status.placed"/>
<html:option value="2" key="order.status.delivered"/>
<html:option value="3" key="order.status.cancelled"/>
If you do have a good reason for doing this however, an alternative to the
two solutions you are considering would be to have your own custom tag based
on the <html:optionsCollection> tag - which I think could be done very
easily, by overriding the addOption() method:
public class KeyOptionsCollectionTag extends OptionsCollectionTag {
protected String bundle = Globals.MESSAGES_KEY;
protected String locale = Globals.LOCALE_KEY;
protected void addOption(StringBuffer sb, String key, String value,
boolean matched) {
String label = TagUtils.getInstance().message(pageContext, bundle,
locale, key);
super.addOption(sb, label, value, matched);
}
public String getBundle() {
return (this.bundle);
}
public void setBundle(String bundle) {
this.bundle = bundle;
}
public String getLocale() {
return (this.locale);
}
public void setLocale(String locale) {
this.locale = locale;
}
}
Niall
----- Original Message -----
From: "Glanville, Jay" <Ja...@NaturalConvergence.com>
To: <st...@jakarta.apache.org>
Sent: Wednesday, February 18, 2004 2:06 PM
Subject: can I use resource keys in html:optionsCollection?
I've looked in the documentation and I can't find my answer, so I'm
assuming that the answer is "no". However, I'm posing the question to
this list in case I've missed something.
I'm using an <html:optionsCollection> tag to display a list of options
inside my <html:select> tag. However, I want the labels not to be
directly populated from collection.label, but from my resource bundle
through keys. In other words, I don't want my collection to be a set of
label - value pairs, but to be a set of label key - value pairs.
Is there a way to have optionsCollection use label keys as opposed to
labels?
I've thought of two ways that I can work around this limitation, but I'd
prefer to use the simpler html:optionsCollection method. My workarounds
are:
1) to use <logic:iterate> to iterate over the collection, and then use
individual <html:option> tags using the key attribute.
2) when creating my collection, resolve my bundle key into a bundle
value for the collection's value attribute.
So, suggestions? Feedback? Help?
Thanks
JDG
--
Jay Glanville
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org