• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

f:selectItems can't set value

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I got an issue, jsf code is:
<h:selectOneMenu id="selOrg" value="#{Bean1.selOrg}" >
<f:selectItem itemLabel="Select..." itemValue=""/>
<f:selectItems value="#{Bean2.allOrgSelectItems}" />
<a4j:support event="onchange" reRender="Org_usergroup_list" limitToList="true"></a4j:support>
</h:selectOneMenu>

Bean1 code segment:
private String selOrg;

public String getSelOrg() {
return selOrg;
}

public void setSelOrg(String selOrg) {
this.selOrg = selOrg;
}

The page looks good, but when I change the selectItems, the setSelOrg method didn't run, so getSelOrg will return null, who can help me? thanks a lot.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
write below code in jsp page


<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="https://ajax4jsf.dev.java.net/ajax" prefix="a4j"%>

<%String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>
My JSF 'index.jsp' starting page
</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<f:view>
<h:panelGrid id="BatchesList" rendered="true">
<h:form>
<h:outputText value="Select Batch" />
<h:selectOneMenu value="#{dbl.selectedCategory}">
<f:selectItems value="#{dbl.items}" />
</h:selectOneMenu>


</h:form>


</h:panelGrid>
</f:view>
</body>
</html>
write below code in bean class


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.model.SelectItem;

public class DynamicListBean {

private ArrayList dynList = new ArrayList();
private SelectItem[]items;
private List dList = new ArrayList();
private String selectedCategory;

public DynamicListBean()
{
items=populateSelectOneMenu();
}


public SelectItem[] getItems() {
return items;
}

public void setItems(SelectItem[] items) {
this.items = items;
}

public String getSelectedCategory() {
return selectedCategory;
}

public void setSelectedCategory(String selectedCategory) {
this.selectedCategory = selectedCategory;
}

public List getDList() {
return dList;
}

public void setDList(List list) {
dList = list;
}

public ArrayList getDynList() {
return dynList;
}

public void setDynList(ArrayList dynList) {
this.dynList = dynList;
}

public SelectItem[] populateSelectOneMenu() {
String a = "B";
List selectItems = new ArrayList();
for (int i = 1; i < 5; i++) {
dList.add(a + i);

}
Iterator it = dList.listIterator();
while (it.hasNext()) {
String label = (String) it.next();

selectItems.add(new SelectItem(label));
}
return (SelectItem[]) selectItems.toArray(new SelectItem[0]);
}

}
 
ya ji
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bean2.allOrgSelectItems is:
public List getAllOrgSelectItems(){
try {
List selectItems= new ArrayList();
Port port = getPortService();
List<Organization> orgList = port.getOrganizations();
for(Organization org: orgList){
selectItems.add(new SelectItem(org.getId(), org.getName()));
}
return selectItems;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}

It can generate dropdown list well.

My problem is why the setSelOrg() method doesn't run, when change selection?
 
ya ji
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fixed by another solution.

In page, add valueChangeListener to h:selectOneMenu component, then add a method in backing bean, like this:
public void changeOrg(ValueChangeEvent event) {
selOrg = event.getNewValue().toString();
}
 
I didn't say it. I'm just telling you what this tiny ad said.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic