Hasmap is not being displayed on selectOneMenu - java

I've been strugling with selectOneMenu dinamically populated by a HashMap without success for a day now, and can´t find what´s going on.
Followed the steps on
How to populate options of h:selectOneMenu from database?
but still no luck
Here´s my bean:
private Paciente selectedPaciente;
private Map<String, String> itensPacientes;
#PostConstruct
public void init() {
itensPacientes = new LinkedHashMap<String, String>();
itensPacientes.put("1","teste1");
itensPacientes.put("2","teste1");
itensPacientes.put("3","teste1");
}
public Map<String, String> getItensPacientes() {
return itensPacientes;
}
public Paciente getSelectedPaciente(){
return selectedPaciente;
}
public void setSelectedPaciente(Paciente selectedPaciente){
this.selectedPaciente = selectedPaciente;
}
and here's the jsf portion
<h:selectOneMenu value="#{beanAgenda.selectedPaciente}" required="true">
<f:selectItem itemValue="#{null}" itemLabel="--select--" />
<f:selectItems value="#{beanAgenda.itensPacientes}"
itemValue="#{entry.key}" itemLabel="#{entry.value}"/>
</h:selectOneMenu>
But when I run the code, I can only see the "--select--" option on the combobx.
Is there something I'm overlooking?
Thanks in advance

Try this:
<f:selectItems value="#{beanAgenda.itensPacientes.entrySet()}" var="entry"
itemValue="#{entry.key}" itemLabel="#{entry.value}"/>
Answer and explanation from this post

Related

selectItems from PrimeFaces simply won't work

I'm trying to a populate a selectItems from a query and It simply won't WORK. I've tried with List list, List list, Map<Long,String> list... None of then works... There's no error, the prime faces won't show the list. Simply as that.
<p:selectOneMenu id="pessoa" value="#{matriculaController.pessoa}">
<f:selectItem itemLabel="Selecione uma Pessoa" itemValue="" />
<f:selectItems
value="#{selectOneMenuView.pessoaLista}"
var="pessoa"
itemLabel="#{pessoa.nome}"
itemValue="#"
/>
</p:selectOneMenu>
Controller:
#Getter
#Setter
#Autowired
private List<Person> pessoaLista;
public void findAllPersons() {
pessoaLista = matriculaService.carregarPessoas();
}
My query function is working, the pessoaLista is populated, the HTML doesn't break. It simply comes blank.
And I've copied from the PrimeFaces docs... (docs)
Use List<Person> and add itemValue="#{pessoa.id}" to your <f:selectItems>
<p:selectOneMenu id="pessoa"
value="#{matriculaController.pessoa}">
<f:selectItem itemLabel="Selecione uma Pessoa"
itemValue="" />
<f:selectItems value="#{selectOneMenuView.pessoaLista}"
var="pessoa"
itemLabel="#{pessoa.nome}"
itemValue="#{pessoa.id}"/>
</p:selectOneMenu>

primefaces selectOneMenu not sorted

I have a selectonemenu in my page and i get its contents from my backing bean. But contents of selectonemenu is not sorted.Here is the code...
<p:selectOneMenu id="Department" value="#{search.selectedDepartment}">
<f:selectItem itemLabel="Select Department" itemValue="" />
<f:selectItems value="#{search.departments}" />
</p:selectOneMenu>
and my bean...
public class SearchBean implements Serializable {
private Map<String, Map<String, String>> courseData = new HashMap<String, Map<String, String>>();
private Map<String, String> departments = new HashMap<String, String>();
private String selectedDepartment;
departments.put("department1",
"department1");
departments.put("department2",
"department2");
departments.put("department3", "department3");
departments.put("department4", "department4");
departments.put("department5", "department5");
//getters setters...
}
and contents of selectonemenu is not department1,department2,department3,department4,department5 respectively.It is not sorted.
It's not sorted because HashMap doesn't have a predictable iteration order. Try LinkedHashMap instead.
with LinkedHashMap it is ordered not sorted i.e you get it in order which you put the items, but if you use TreeMap then it is sorteted alphabetically

JSF primefaces showing list of object not list of string

I already tried to search through this site, but maybe I'm not getting understand well.
Please kindly advice for my cases..
I'm using Netbeans 7.3 + Primefaces + Hibernate.
I want to show a list from my query.
My query already in.. and there's no error showup, but the display is not what I want (I think it returning an object or something, not sure).
Please kindly correct me if I'm missed something.
Here's my PtlLovBean
#ManagedBean(name = "ptlLovBean")
#SessionScoped
public class PtlLovBean implements Serializable {
private static final String FLIGHT = "LOV_FLIGHT";
private List lovFlight;
public List getLovFlight() {
PtlLovDao ptlLovDao = new PtlLovDaoImpl();
return ptlLovDao.getByKey(FLIGHT);
}
}
Here's ptlLovDao
public interface PtlLovDao {
public List getByKey(String key);
}
Here's PtlLovDaoImpl
public class PtlLovDaoImpl implements PtlLovDao {
#Override
public List getByKey(String key) {
Session session = HibernateUtil.getSessionFactory().openSession();
Query query = session.createQuery("from PtlLov where LOV_KEY = :param");
query.setParameter("param", key);
return query.list();
}
}
Here's my JSF :
<p:selectOneMenu id="flightName" value="#{wizard.user.selectedFlightName}">
<f:selectItem itemLabel="Select Flight" itemValue="" />
<f:selectItems value="#{ptlLovBean.lovFlight}" />
</p:selectOneMenu>
Display after Code:
Sorry I'm not capable to insert picture, so here's the image link :
http://i117.photobucket.com/albums/o56/po_se_for/PIC_zps88ec4983.png
You can either override the toString method in your PtlLov class or define itemValue and itemLabel properties in your f:selectItems tag.
Something like this:
<f:selectItems value="#{ptlLovBean.lovFlight}" var="flight"
itemValue="#{flight}" itemLabel="#{flight.description}" />

JSF: <f:selectItems value="#{Bean.method([abc])}" />

I have a bean and there is cache and a method to get value from it.
public List<SelectItem> getSelectItemList(String key){
return cache.get(key).getValue();
}
May I know how can I get this list in JSF?
I have tried..
<f:selectItems value="#{Bean.getSelectItemList(abc)}" />
but it does not work, because it is expecting a property.
Note: I using JSF 1.2 , EHCache
JSF Code:
<t:selectOneMenu id="testId" value="#{testBean.selectedItem}" >
<f:selectItems value="#{testBean.selectItemList}" />
</t:selectOneMenu>
Manage Bean code:
private String selectedItem;
private List selectItemList;
public List<SelectItem> getselectItemList() {
if(null == selectItemList || selectItemList.isEmpty()){
selectUserList = new ArrayList<SelectItem>();
selectItemList.add(new SelectItem(this.selectedItem, cache.get(this.selectedItem).getValue()));
}
return selectItemList;
}

JSF - UISelectItems problem

Strange error i received from compiler:
Expected a child component type of UISelectItem/UISelectItems for component type javax.faces.SelectOne(siachoice). Found javax.faces.component.UISelectItems.
So, if he was expecting UISelectItems, and found UISelectItems, then where is the error?
My JSP implementation:
<h:selectOneMenu id="siachoice" value="#{dbSelectBean.currentOption}">
<f:selectItems value="#{dbSelectBean.dbs}" />
</h:selectOneMenu>
Method, where i am setting UISelectItem to UISelectItems:
private UISelectItems populateDatabases(String databaseString) {
UISelectItems selects = new UISelectItems();
List<UISelectItem> result = new ArrayList<UISelectItem>();
StringTokenizer tokeniz = new StringTokenizer(databaseString, GlobalConstants.DELIMITER);
while(tokeniz.hasMoreTokens()){
String tempDB = tokeniz.nextToken();
UISelectItem item = new UISelectItem();
item.setItemValue(tempDB);
item.setItemLabel(tempDB);
result.add(item);
}
selects.setValue(result);
return selects;
}
Then, of course, i am setting it to the bean variable dbs.
Help?
You must return a Collection of javax.faces.model.SelectItem
List list = new ArrayList();
list.add(new SelectItem(value, label));
return list;
The <f:selectItems value="#{bean.items}" /> expects one of the following values:
public SelectItem[] getItems() {}
public List<SelectItem> getItems() {}
public Map<String, Object> getItems() {}
The commonly used one is indeed the List<SelectItem>.
Edit: as response to the comment: UISelectItem represents the <f:selectItem> component. The same applies to UISelectItems and <f:selectItems>. E.g.
<f:selectItem binding="#{bean.selectItem}" />
<f:selectItems binding="#{bean.selectItems}" />
which are bound as
private UISelectItem selectItem;
private UISelectItems selectItems;
// +getter +setter
this way you can control the components programmatically -as for every other UIComponent.
<h:form>
<h:selectOneListbox size="5" >
<f:selectItems value="#{userManager.Test()}" />
</h:selectOneListbox>
</h:form>
import javax.faces.model.SelectItem;
import tn.com.ttnproject.entity.*;
#Name("userManager")
#Scope(ScopeType.CONVERSATION)
public class UserManager {
public List <SelectItem> listAllUsersNames;
SelectItem element;
public List<SelectItem> Test(){
listAllUsersNames = new ArrayList<SelectItem>();
for (int i=1;i<=10;i++)
{
element=new SelectItem(
new Integer(i),
i+".00 euros",
"Produit à "+i+".00 euros");
listAllUsersNames.add(element);
}
return listAllUsersNames;
}
}
The problem is UISelectItem is a component clas so it has to be paired with jsf tag by binding attribute. If you want to have pure values you have to use SelectItem(s) classes.

Categories

Resources