Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

JSF component

  Asked By: Harry    Date: Mar 09    Category: Java    Views: 2132
  

I have 2 relevant dropdown list and 3 textbox in JSf page. and use hibernate in project.
1. when load page, dropdown lists get values of 2 relevant tables of database.
2. I want : when select a value from dropdown list1, the relevat values show in dropdown list2 and the selested value of dorpdown list1 be selected.
2 . and when select a value from dropdown list2 show relevant values of table 1 and table 2 of DB in textboxes.

but when the page load after every dropdown list selection lose the selected value of dropdown list.and show the first value of DB'thable .

please guide me.
If i must use javascript code? how?
how can write javascript code for dropdown list of JSf page in sessionBean page?

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Chad Bradley     Answered On: Mar 09

Set the value property of the SelectItem to the ID of your entity not its instance.

List<SelectItem> listForYourCombo = new ArrayList<SelectItem>
for(YourEntity entity : list) {
SelectItem si = new SelectItem();
si.setLabel(entity.toString());
si.setValue(entity.getId()); // <==== The point is here DO NOT USE si.setValue(entity)
}

The you can bind the value property of selectOneMenu to an Integer represented the selected  item using the ID of your entity in your back bean.

<f:selectOneMenu value="#{yourbackbean.selectedEntityID}" />

if you do not underestand the case, download NetBeans IDE you can find wonderful samples there to work with JSF

 
Answer #2    Answered By: Laurel Collins     Answered On: Mar 09

At first you should have an action (Managed Bean) object with one of these scopes :
1- conversation
2- session
3- application

and then you could use ajax base thechnology for better performance and efficiency for Rerendering page  without refresh it like Richfaces.

So then you should bind a field or variable from that action (Managed Bean) with EL to JSF page at combo box that you use like this :

<h:selectOneMenu id="dropdown-1" value="#{action.field}">
<f:selectItems value="#{helper.items}"/>
</h:selectOneMenu>

and in this case value of selected  will survive till action is live at those scope.

 
Answer #3    Answered By: Orville Rodriguez     Answered On: Mar 09

I think the problem is that you set your Backing Bean Scope to Request. Try to
change it to Session and you'll see it saves the states of your DropDownLists
after submitting the page. Here is a very simple code  I use in my projects. As I
mentioned the BackingBean has Sessio scope:

<h:selectOneMenu binding="#{editExpertAddressInfoBackingBean.province}"
style="width: 160px"
valueChangeListener="#{editExpertAddressInfoBackingBean.provinceChanged}"
onchange="submit();">
<f:selectItem itemValue="" itemLabel="" />
<f:selectItems value="#{editExpertAddressInfoBackingBean.provinceItems}"/>
</h:selectOneMenu>


<h:selectOneMenu binding="#{editExpertAddressInfoBackingBean.city}"
style="width: 160px">
<f:selectItem itemValue="" itemLabel=""/>
<f:selectItems value="#{editExpertAddressInfoBackingBean.cityItems}"/>
</h:selectOneMenu>





public void provinceChanged(ValueChangeEvent e) throws AbortProcessingException
{
if (province.getValue() != null &&
!"".equals(String.valueOf(province.getValue()))){
Long id = Long.valueOf(String.valueOf(province.getValue()));
cityItems = loadCities(id);
} else{
cityItems.clear();
}
}

 
Answer #4    Answered By: Bonifaco Garcia     Answered On: Mar 09

No the problem is that you must
override toString() method of your entity because the selected  
value key is the string value of your object.

In HTML elements the java objects are not known, and they are string, array of string.

For more information refer to JSF specs, and for beginners samples in NetBeans IDE.

 
Answer #5    Answered By: Estella Mitchell     Answered On: Mar 09

I think that it's so usual to lost values  when you submit your form and change request .
Maybe it's better , use of Ajax submit, instead of submitting form when you change the value of dropdown  . it cause to send a request to server and change a part of page  which you want but not your whole page . for doing this that's enough to use form tag of Ajax for JSF lib(a4j) and add
" ajaxSubmit="true" "
to your form tag in the page.
for more information refer to A4J lib refrence.

 
Didn't find what you were looking for? Find more on JSF component Or get search suggestion and latest updates.




Tagged: