Reputation: 165
hi i am creating a JSF application . In fact I made a dropdown list and want to display the results according to the value selected from the dropdown. if somebody can help.... thanks
here is my drop down
<h:form>
<h:commandButton action="sample?faces-redirect=true" value="submit">
<h:selectOneMenu id="sampleSearch" value="#{cBean.id}">
<f:selectItem id="id" itemLable="idText" itemValue="By Text" />
<f:selectItem id="idnumeric" itemLable="idNumeric" itemValue="Number" />
<f:selectItem id="product" itemLable="Product" itemValue="Main Product" />
<f:selectItem id="lonumber" itemLable="loNumber" itemValue="LoNumber" />
<f:selectItem id="formula" itemLable="formula" itemValue="By Formula" />
</h:selectOneMenu>
</h:commandButton>
</h:form>
Upvotes: 0
Views: 2882
Reputation: 19796
I advice you to read a book about JSF or at least some more tutorials about it. Your code makes not much sense. You nest a selectOneMenu
inside of a commandButton
. And if you want to display some values from your database, you should also call a method in your managed bean to do the query and retrieve the results.
That's how you should do it instead:
<h:form>
<h:selectOneMenu id="sampleSearch" value="#{cBean.id}">
<f:selectItem id="id" itemLable="idText" itemValue="By Text" />
<f:selectItem id="idnumeric" itemLable="idNumeric" itemValue="Number" />
<f:selectItem id="product" itemLable="Product" itemValue="Main Product" />
<f:selectItem id="lonumber" itemLable="loNumber" itemValue="LoNumber" />
<f:selectItem id="formula" itemLable="formula" itemValue="By Formula" />
</h:selectOneMenu>
<h:commandButton action="#{cBean.search}" value="submit" />
</h:form>
And your bean would look like this:
public class CBean {
private String id; // getter+setter
public String search() {
// perform your query based on the id value
return "sample?faces-redirect=true";
}
}
Upvotes: 1
Reputation: 62884
First, you're not allowed to nest <h:selectOneMenu>
component(s) within <h:commandButton>
!
Here's a proper structure of your <h:form>
<h:form>
<h:commandButton action="sample?faces-redirect=true" value="submit" />
<h:selectOneMenu id="sampleSearch" value="#{cBean.id}">
<f:selectItem id="id" itemLable="idText" itemValue="By Text" />
<f:selectItem id="idnumeric" itemLable="idNumeric" itemValue="Number" />
<f:selectItem id="product" itemLable="Product" itemValue="Main Product" />
<f:selectItem id="lonumber" itemLable="loNumber" itemValue="LoNumber" />
<f:selectItem id="formula" itemLable="formula" itemValue="By Formula" />
</h:selectOneMenu>
</h:form>
Then, in order to get the dropdown list options from the database, you can consider using the <f:selectItems>
component (and get rid of those <f:selectItem>
s) and pass a List<T>
from the managed bean to the components value
property.
The selectOneMenu
would then look like this:
<h:selectOneMenu value="#{cBean.id}">
<f:selectItems value="#{cBean.values}"
var="item"
itemLabel="#{item.label}"
itemValue="#{item.value}"/>
</h:selectOneMenu>
As for the managed-bean, it's now supposed to provide a public List<T> getValues()
method, which will return a list with the objects that will populate the dropdown.
When T
is a complex Java object, such as Item
which has a String property of label
and value
, then you could use the var
attribute to get hold of the iteration variable which you in turn can use in itemValue
and/or itemLabel
attribtues (if you omit the itemLabel
, then the label becomes the same as the value).
Let's say:
@ManagedBean
@RequestScoped
public class CBean {
public List<Item> getValues() {
List<Item> result = new ArrayList<Item>();
//..call-back to web-service, db, etc. and populate the result variable.
return result;
}
}
The Item
class would look like this:
public class Item {
private String label;
private String value;
//getters, setters.
}
You can read more here:
Upvotes: 1