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

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;
}

Related

selectOneMenu value null when using selectItems from a different backing bean

I have a form where I create a user. In my form, I have multiple properties for that user (I actually use a User object for the retainment of data on submit to the backing bean)
create.xhtml
<h:form>
<h:outputLabel for="user_name" value="Name:" />
<h:inputText id="user_name" value="#{createUserView.newUser.username}" />
<br/><br/>
<h:outputLabel for="user_password" value="Default Password*:" />
<h:inputSecret id="user_password" value="#{createUserView.newUser.password}"></h:inputSecret><br/><br/>
<h:outputLabel for="user_organization" value="Organization:" />
<h:selectOneMenu id="user_organization" disabled="true" value="#{createUserView.newUser.organizationId}">
<f:selectItems
value="#{organizationBean.allOrganizations}"
var="org"
itemLabel="#{org.organizationName}"
itemValue="#{org.id}" />
</h:selectOneMenu><br/><br/>
<h:commandButton value="Create" action="#{createUserView.createNewUser}" />
</h:form>
CreateUserView
#ManagedBean(name = "createUserView")
#RequestScoped
public class CreateUserView {
private UserServices userSerivces;
private User newUser;
#ManagedProperty(value="#{organizationBean}")
private OrganizationBean organizationBean;
public CreateUserView() {
newUser = new User();
userSerivces = new UserServices();
}
public void createNewUser() {
userSerivces.createNewUser(newUser);
}
// Getters and Setters
}
OrganizationBean
#ManagedBean(name = "organizationBean")
#RequestScoped
public class OrganizationBean {
private List<Organization> allOrganizations;
private OrganizationServices orgServices;
public OrganizationBean() {
orgServices = new OrganizationServices();
allOrganizations = orgServices.retrieveAllOrganizations();
}
// Getters and Setters
}
The issue here is that when I reference the newUser object in the backing bean, the organizationId value is null.
I assume this is because OrganizationBean (excuse the confusing in naming, refactoring) is either not rendered for my current view or I need to somehow inject.
I've tried a managed property in the CreateUserView backing bean that references the OrganizationBean, but no luck. The organizationID value in the newUser object is null.
Do I need to populate a list in the CreateUserView bean using the OrganizationBean injection, so that it has it's own list it can render?
What am I missing? Feeling foolish.
JSF 2.0
The problem, as stated in the comments is that you don't have a Converter for your Organization class.
You must have it in order to know what Organization matches every SelectItem. The converter must be something like:
#FacesConverter(forClass = Organization.class, value = "organizationConverter")
public class OrganizationConverter implements Converter
{
#Override
public Object getAsObject(FacesContext fc, UIComponent uic, String id)
{
if (StringUtils.isEmpty(id))
{
return null;
}
// Convert id to an Organizacion
return organization;
}
#Override
public String getAsString(FacesContext fc, UIComponent uic, Object o)
{
if (o instanceof Organization)
{
return ...;//Convert organization to id
}
return null;
}
}
And then in your selectonemenu:
<h:selectOneMenu id="user_organization" disabled="true" value="#{createUserView.newUser.organizationId}"
converter="organizationConverter">

Hasmap is not being displayed on selectOneMenu

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

JSF 2.0: <f:ajax>'s listener is not triggered

In my code, I have a PrimeFaces' wizard component with several tabs as following:
<h:form id="myForm">
<p:wizard flowListener="#{mrBean.flowControl}" widgetVar="wiz">
<p:tab id="tab1"></p:tab>
<p:tab id="tab2"></p:tab>
<p:tab id="tab3">
<h:selectOneMenu id="couponList" value="#{mrBean.coupon}"
converter="#{codeToCouponConverter}" >
<f:ajax listener="#{mrBean.doSomething}" execute="#this"/>
<f:selectItem noSelectionOption="true" itemLabel="Choose one..." />
<f:selectItems value="#{mrBean.coupons}" var="c"
itemValue="#{c}" itemLabel="#{c.name} - $ #{c.discount}" />
</h:selectOneMenu>
</p:tab>
</p:wizard>
</h:form>
This is the code for the managed bean:
#ManagedBean(name = "mrBean")
#ViewScoped
public class MrBean {
private List<Coupon> coupons;
private Coupon coupon;
public void doSomething() {
System.out.println("DONE");
}
public String flowControl(FlowEvent event) {
...
}
// Getters and Setters
}
In 1 of the tab, I have a <h:selectOneMenu> component which contains a <f:ajax> tag. I have no idea why the listener is only triggered when I choose the Choose one... option. When I choose any other options from the mrBean.coupons list, the listener is never triggered. In other words, I never saw any DONE printed on the console.
*UPDATE***: The problem turns out to be coming from the following Converter:
#RequestScoped
#ManagedBean
public class CodeToCouponConverter implements Converter {
#EJB
private MrsBeanInterface mrsBean;
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
String couponCode = value;
if (value != null) return mrsBean.getCoupon(couponCode);
else return null;
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value != null) {
Coupon c = (Coupon) value;
return c.getId();
} else return null;
}
// Getters and Setters
public MrsBeanInterface getMrsBean() {
return mrsBean;
}
public void setMrsBean(MrsBeanInterface mrsBean) {
this.mrsBean = mrsBean;
}
}
If I change the <h:selectOneMenu> as following:
<h:selectOneMenu id="couponList" value="#{mrBean.couponCode}" >
<f:ajax listener="#{mrBean.doSomething}" execute="#this"/>
<f:selectItem noSelectionOption="true" itemLabel="Choose one..." />
<f:selectItems value="#{mrBean.coupons}" var="c"
itemValue="#{c.id}" itemLabel="#{c.name} - $ #{c.discount}" />
</h:selectOneMenu>
and update the mrBean.doSomething function as following:
#EJB
private MrsBeanInterface mrsBean;
private String couponCode;
private Coupon coupon;
public void doSomething() {
this.coupon = mrsBean.getCoupon(couponCode);
System.out.println("DONE");
}
everything works perfectly.
I would be very grateful if you could give me an explanation of what I have done wrong with the Converter.
Best regards,
James Tran
Use #{mrBean.doSomething()} with the braces or add the event parameter to the method.

Display values in drill-down SelectOneMenus (Parent-Child)

I have irritating problem when I populate a drill-down selection of two selectOneMenu. The first menu is Sector which controls the other one Categorty. I load the selection for the Category depending on Sector and all of this is working fine.
But when I edit a business object that has a Category (has one) Sector attached to it. The Category doesn't get display when first loading the edit.xhtml page. I know the value is correct in the SessionScoped backing bean. If I select another Sector-Category and then go back to the initial Sector, the Category get properly set at it was persisted in the first place.
The POJO classes (Sector-Category) has the hashCode() and equals(Object object) functions.
I have CRUD generated JSF converters for the POJO object, but I don't think this is causing the problem. I think the second selectOneMenu don't display its value as it should for some reason. If I flip the parent (Sector) to something else and the back to the initial state, the correct value from the manged bean get displayed.
Ho can I make the Category selectOneMenu component display the value in the managed bean?
Greetings Chris
Faces Error Message
FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=null[severity=(ERROR 2), summary=(No activity selected), detail=(No activity selected)]
Edit.xhtml
...
<h:outputLabel value="Sector:" />
<h:selectOneMenu id="sectorSelector" value="#{activityController.selectedSector}" title="#{bundle.CreateSectorLabel_sectorName}" required="false" requiredMessage="#{bundle.CreateSectorRequiredMessage_sectorName}"
valueChangeListener="#{activityController.changeSectorMenu}"
disabled="#{activityController.activityStatusOngoing or activityController.activityStatusComplete}">
<f:ajax event="change" execute="#this" render="categoryMenu"/>
<f:selectItems value="#{sectorController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="Category:" />
<h:selectOneMenu id="categoryMenu" value="#{activityController.selectedCategory}" title="#{bundle.CreateSectorLabel_sectorName}"
required="true" requiredMessage="#{bundle.CreateCategoryRequiredMessage_sector}"
disabled="#{activityController.activityStatusOngoing}" rendered="true">
<f:selectItems value="#{activityController.categorySelection}"/>
</h:selectOneMenu>
...
Controller bean for Category
#ManagedBean(name = "categoryController")
#SessionScoped
public class CategoryController implements Serializable{
....
#FacesConverter(forClass = Category.class)
public static class CategoryControllerConverter implements Converter {
#Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
CategoryController controller = (CategoryController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "categoryController");
return controller.ejbFacade.find(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuffer sb = new StringBuffer();
sb.append(value);
return sb.toString();
}
#Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Category) {
Category o = (Category) object;
return getStringKey(o.getIdCategory());
}
else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + CategoryController.class.getName());
}
}
}
Part of POJO object
...
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "idCategory")
private Integer idCategory;
...
Option (not so difficult as I recently made a switch) is to include Seam Faces and Persistence modules. Seam Faces module enables ViewScope without #ManagedBean annotation.
But SeamFaces forces us (which is justified) to use CDI annotations (#Named instead of #ManagedBean etc).
Please refer this post. (You can use #EJB and also Inject any bean annotated with #Named)
It was not the Converters as I first thought. It was the rendering of the two selectOneMenu. This is the changes I did to the xhml file. Thank you for your time and effort, greetings Chris.
Edit.xhml
<h:outputLabel value="Sector:" />
<h:selectOneMenu id="sectorSelector" value="#{activityController.selectedSector}" title="#{bundle.CreateSectorLabel_sectorName}" required="false" requiredMessage="#{bundle.CreateSectorRequiredMessage_sectorName}"
valueChangeListener="#{activityController.changeSectorMenu}" immediate="true"
disabled="#{activityController.activityStatusOngoing or activityController.activityStatusComplete}">
<a4j:ajax event="change" execute="#this categoryMenu" render="categoryMenu"/>
<f:selectItems value="#{sectorController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="Category:" />
<h:selectOneMenu id="categoryMenu" value="#{activityController.selectedCategory}" title="#{bundle.CreateSectorLabel_sectorName}"
binding="#{activityController.categoryMenu}"
required="true" requiredMessage="#{bundle.CreateCategoryRequiredMessage_sector}"
disabled="#{activityController.activityStatusOngoing}">
<f:selectItems value="#{activityController.categorySelection}"/>
</h:selectOneMenu>

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