Setting list items in ice:SelectOneMenu [duplicate] - jsf

This question already has answers here:
How to populate options of h:selectOneMenu from database?
(5 answers)
Closed 2 years ago.
I wish to set items from a list to the selectonemenu in icefaces.
But when I do the same I get the following error:
java.lang.ClassCastException: cannot be cast to javax.faces.model.SelectItem
The is an entity class.
Please Help.

The normal way of creating and populating the selectOneMenu items would be the following:
private String selectedItem; // +getter +setter
private List<SelectItem> selectItems; // +getter
public Bean() {
selectItems = new ArrayList<SelectItem>();
for (Entity entity : getYourEntities()) {
selectItems.add(new SelectItem(entity.getValue(), entity.getLabel()));
}
}
With the following in the view (you can easily subsitite <h: with <ice:):
<h:selectOneMenu value="#{bean.selectedItem}">
<f:selectItems value="#{bean.selectItems}" />
</h:selectOneMenu>
Instead of a String value, you can also use any Number (Integer, Long, etc) since JSF has builtin converters for this. But if you want to use whole objects as item value, then you need to create a Converter. This is described in detail in this article.

Related

Dropdown list is not populating in JSF

I am working on Managedbeans and JSF. As shown below that my ManagedBean contains all the requirements that are required for the JSF to get the value. I have initialised my dropdown list as below. In selectOneMenu, I have chosen the country as a string where it will store the value selected by the dropdown list and the dropdown list will bring up the list that I declared in the Beans.
Unfortunately, it is not happening like that. Every time dropdown renders it gives me an empty value. I have spent days on it but cannot figure out the exact solution to it. I have cleaned my server, build workspace and also change servers but nothing is working.
** ManagedBean_list **
private List<String> listCountry;
private String country;
public void tada(){
listCountry=Arrays.asList("India", "pakisatan","America");
}
public List<String> getListCountry() {
return listCountry;
}
public void setListCountry(List<String> listCountry) {
this.listCountry = listCountry;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
JSF
<p:selectOneMenu id="country" value="#{loginBeans.country}">
<f:selectItems value="#{loginBeans.listCountry}" />
</p:selectOneMenu>
Your help is appreciated. Empty dropdown list image
enter image description here
Which bean annotation are you using? You say "Managedbeans", but the source you posted does not show the entire bean, or does it? Check to make sure you are not mixing old style JSF managed bean annotations with CDI annotations.
The issue is that on initialization, the list is not being called up. I resolved it by including the list function inside the constructor of managed beans class. so that when the constructor fired up. It also generates the dropdown list.
Either convert your listCountry to a
private Map<String, String> listCountry = new HashMap<>();
listCountry.put("India", "India");
listCountry.put("Pakistan", "Pakistan");
listCountry.put("America", "America");
or
private List<SelectItem> listCountry = new ArrayList<>();
listCountry.add(new SelectItem("India", "India"));
listCountry.add(new SelectItem("Pakistan", "Pakistan"));
listCountry.add(new SelectItem("America","America"));

How to set defalult selected items in selectOneRadio/selectCheckboxMenu by beans in primefaces

I want to create selectOneRadio list and selectCheckboxMenu which items get from related bean objects.
For non selected list it is working well.But how can i provide these lists with some item(s) selected by default.
My current selectCheckboxMenu code is like this.
<p:selectCheckboxMenu id="trdays"
value="#{mybean.selectedDay}"
label="Select Days">
<f:selectItems value="#{mybean.dayList}" var="day"
itemValue="#{day.value}" itemLabel="#{day.name}"/>
</p:selectCheckboxMenu>
it should look like this when page loaded and user haven't done anything yet.
EDIT
Day Class :-
public class Day{
private String name;
private String value;
//getters and setters
}
Value attribute of selectCheckboxMenu component should get the your default values.
On bean side you should write a getter for selectedDayList and all daylist.
Example:
#PostConstruct
public void init() {
dayList= new ArrayList<String>();
dayList.add("Mon");
dayList.add("Tue");
dayList.add("Wed");
dayList.add("Thu");
dayList.add("Fri");
dayList.add("Sat");
dayList.add("Sun");
selectedDayList= new ArrayList<String>();
selectedDayList.add("Tue");
selectedDayList.add("Wed");
}
public List<String> getDayList()
{
return dayList;
}
public List<String> getSelectedDaylist()
{
return selectedDayList;
}
.xhtml page should be like this.
<p:selectCheckboxMenu id="trdays"
value="#{mybean.selectedDaylist}"
label="Select Days">
<f:selectItems value="#{mybean.dayList}" var="day"
itemValue="#{day.value}" itemLabel="#{day.name}"/>
</p:selectCheckboxMenu>
Good Luck!
Just add default values in the selectedDay list (or array) in init method (with #PostConstruct annotation). These values should have same value as corresponding itemValue attribute (in your case this is day.value).

f:selectItems and POJOs - once more

All the questions asking: how can I bind POJOs to h:selectXX with f:selectItems end up with answer "use a converter". However, it seems it is possible to go without the converter - see:
Facelet:
<h:selectManyListbox value="#{pojoBean.selected}">
<f:selectItems value="#{pojoBean.allItems}" var="i" itemValue="#{i}" itemLabel="#{i.txt}" />
</h:selectManyListbox>
Bean:
public class PojoBean {
List<MyItem> selected;
List<MyItem> allItems;
POJO:
public class MyItem {
private String txt;
...}
Note that this seems to work only with h:selectManyListbox, when the value/s being selected end up in a list, not in a single property.
Question - why doesn't it work with h:selectOneMenu and etc?
Likely your MyItem class has already the toString() overridden which returns the txt and you were plain printing selected as follows to determine the selected values:
System.out.println(selected);
Try to cast each item of selected back to MyItem:
for (MyItem myItem : selected) {
System.out.println(myItem);
}
You'll see that it fails with ClassCastException, because it's actually a String. So yes, you still need a converter here.
See also:
How do I get selectManyCheckBox to return something other than List<String>?

JSF converter Validation Error: value is not valid for SelectOneMenu UIComponent [duplicate]

This question already has answers here:
Validation Error: Value is not valid
(3 answers)
Closed 6 years ago.
I am using the managedBean userHome in requestScope, in which the entity 'user' is going to be persist.
The user has the leader column which is mapped in ManyToOne relation.My Code looks like this
#ManagedBean
#RequestScoped
public class UserHome {
private User user = new User();
// Getters and Setters
private List<SelectItem> selectItems = new ArrayList<SelectItem>();
public UserHome() {
for(User user: availableLeaders) {
selectItems.add(new SelectItem(user.getName(), user));
}
}
public void persis();
}
User.java
public class User {
#Id
#Column
private Integer id;
#Column
privat String name;
#ManyToOne
private User leader;
}
I am trying to get the value of this leader through h:selectOneMenu like this
<h:selectOneMenu value="#{userHome.user.leader}" converter="userConverter">
<f:selectItems value="#{userHome.selectItems}"/>
</h:selectOneMenu>
My converter looks like this
#FacesConverter(forClass = User.class, value="userConverter")
public class UserConverter implements Converter {
private Map<String, User> userValues = new HashMap<String, User>();
public UserConverter() {
init();
}
#Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
return userValues.get(value);
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
System.out.println("RAJASEKARAN "+value);
return ((User)value).getName();
}
public void init() {
UserHome userHome = new UserHome();
for(User user:userHome.availableLeaders()) {
userValues.put(user.getName(), user);
}
}
}
While try to save the user I am getting the error UserEdit:j_idt18: Validation Error: Value is not valid
Adding to BalusC's answer: after the postback, you need to make sure that the User instances are either exactly the same ones as you used for rendering the select items, or that you implement equals for your User class.
The code doesn't show where availableLeaders comes from, but if this is fetched from a DB on-demand, then the converter will not convert to the exact same object instance that's in the list that JSF resolves via #{userHome.selectItems}.
After the conversion, JSF will check whether the converted instance can be found in that list using the equals() method.
You've constructed the SelectItem the wrong way. As per the class' documentation, the 1st argument should represent the item value (which is to be converted and submitted) and the 2nd argument should represent the item label (which is to be displayed in list). But you specified them the other way round.
Fix it accordingly:
selectItems.add(new SelectItem(user, user.getName()));
If that still doesn't fix the problem, then it means that the equals() method of User class is not (properly) implemented. JSF will use it to validate the selected User against any of the item values of the list after conversion.
Unrelated to the concrete problem, it may be useful to know that <f:selectItems> in JSF2 offers you the possibility to build the list without the need to build a list of SelectItem manually. Here's an example which achieves exactly the same:
<f:selectItems value="#{userHome.availableLeaders}" var="user"
itemValue="#{user}" itemLabel="#{user.name}" />
This allows you to get rid of the additional selectItems property and the loop in the bean constructor.

JSF preload list for datatable in page [duplicate]

This question already has an answer here:
How and when should I load the model from database for JSF dataTable
(1 answer)
Closed 7 years ago.
I'm using EJB and JSF. I made a jsp simple page with a button "get list". When it's clicked, a managed bean method is called that sets the list, which is then displayed in the jsp with the dataTable tag.
The question is, how can I pre load this list/dataTable on page load without having to click the button?
This is the method that's called through the button action on the jsp:
public String retrieveList() {
items = facade.getAllItem();
return "";
}
this is the part of the jsp:
<h:dataTable value="#{mybean.items}" var="sup"
binding="#{mybean.dataTable}"
rowClasses="oddRow, evenRow"
styleClass="tableStyle"
headerClass="tableHeader"
columnClasses="column1, column2, column1, column1, column1, column1">
You can add a method init with #postConstruct
#PostConstruct
public void init(){
items = facade.getAllItem();
}
This will return the items only on bean creation ,
Annotate the method with #PostConstruct and get rid of return value.
#PostConstruct
public void retrieveList() {
items = facade.getAllItem();
}
This way the method will be executed immediately after construction of the bean and injection of all #EJB dependencies. In the JSF page you just have to bind to #{bean.items} the usual way.

Resources