Mixing JSF and JSTL

Hi,

I'm having a little issue with combining JSTL and JSF.

I have a jsf selectOneMenu drop-down that I would like to fill with values stored in a bean. The bean is a Vector that contains objects of a certain type. The vector object is called Languages which carries objects of type Language. Each language object has an id, name and description.

I tried finding out by using only JSF tags how to loop through the Languages bean, but I found out that that isn't possible. I searched on the net and found an example that combines JSF and JSTL for looping and displaying the looped values in a selectOneMenu . I cusomized it to my own purposes which results in the following code:

// define the languages bean

<jsp:useBean id="LanguagesBean" class="MyPackage.Languages" scope="session">

<jsp:setProperty name="LanguagesBean" property= "initiated" value="true" />

</jsp:useBean>

// the actual code

<h:selectOneMenu id="lang" value="Languages" title="select a language"> <c:forEach var="languages" items="${LanguagesBean}">

<c:set var="languagesVar" value="${languages}" scope="request" /><f:selectItem itemValue="#{languagesVar.id}" itemLabel="#{languagesVar.description}" />

</c:forEach>

</h:selectOneMenu>

Somehow the output for each selectItem is the description of the last Language object in the Languages bean.

So, I tried to display the ouptut of it by using <c:out ../>. When using this way of displaying all thelanguage descriptions are displayed.

Is there a bug in using the var that is set in<c set../>? Is there an alternative way of looping through the languages object by using JSF and JSTL tags?

I'm using Sun's JavaServer Faces implementation (1.2_04-b07-FCS).

Hope someone can help. Thanks.

[1883 byte] By [newbyfacesdevelopera] at [2007-11-27 9:30:58]
# 1
Why don't you use the JSF's managed bean facility?
BalusCa at 2007-7-12 22:44:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hi,Because I only need the Languages object in some cases, f.e. management.
newbyfacesdevelopera at 2007-7-12 22:44:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Hi BalusC ,Can you explain how I will be able to loop through the Languages object (vector) and retrieving each language object from it in order to put the description of each language as a selectItem in the <h:selectOneMenu ..> tag?Thanks.
newbyfacesdevelopera at 2007-7-12 22:44:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

JSF<h:selectOneMenu value="#{myBean.selectedLanguage}">

<f:selectItems value="#{myBean.selectLanguages}" />

</h:selectOneMenu>

MyBeanprivate Integer selectedLanguage; // + getter + setter

private List<SelectItem> selectLanguages; // + getter

// Eventually make the selectLanguages static.

public MyBean() {

fillSelectLanguages(); // Eventually invoke it in a static initialization block.

}

private void fillSelectLanguages() {

selectLanguages = new ArrayList<SelectItem>();

Vector<Language> languages = getLanguagesSomehow();

for (Language language : languages) {

selectLanguages.add(new SelectItem(language.getId(), language.getDescription());

}

}

BalusCa at 2007-7-12 22:44:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Hi BalusC,I found something aliketo you sample code, but yours explains it better than the examples I found. Thanks a lot! It works now.Regards,Newwby.
newbyfacesdevelopera at 2007-7-12 22:44:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...