selectOneMenu Conversion Error

I'm trying to create a drop down box with a list of security levels for a user.

The problem I'm having is my selectOneMenu box is creating a Conversion Error when I submit the form (to create a new user).

I've been reading a lot of different posts about this problem and tried their suggestions but I'm still stuck.

Here is my code for the selectOneMenu

JSP

<h:selectOneMenu id="security" value="#{Security.currentSecurity}" >

<f:selectItems value="#{Security.listSecuritysTest3}" />

</h:selectOneMenu>

Here is my current Test code from my Security class

public List getListSecuritysTest3(){

if(selectItems ==null){

selectItems =new ArrayList();

for (Iterator iter = getListSecuritys().iterator(); iter.hasNext();){// Get a list of all Security Levels

SecurityBean securityBean = (SecurityBean) iter.next();// Get the next SecurityBean from results

selectItems.add(new SelectItem(securityBean, securityBean.getSecurity()));// Add to the list

}

}

System.out.println("getListSecuritysTest3");

return selectItems;

}

The error I'm getting when submitting this form is:

Conversion Error setting value 'pdfcat.model.SecurityBean@1917b9e' for 'null Converter'.

For currentSecurity I just have standard Getter/Setters in my Security class file, like so

private SecurityBean currentSecurity;

publicvoid setCurrentSecurity(SecurityBean currentSecurity){

this.currentSecurity=currentSecurity;

}

public SecurityBean getCurrentSecurity(){

return currentSecurity;

}

It seems to be trying to convert a string to an object, is there a way to pass the selected Security Level as an object and not a string?

[2674 byte] By [freebsdmap7a] at [2007-11-27 9:12:14]
# 1

Hi ,

Put in save State value of currrentsecurity.

And i hope u have getter and setter method for "currentSecurity" in bean class.

Add this tag in ur Jsp page .

<t:saveState value="#{Security.currentSecurity}" />

Try this. I hope this will work out for u .

Roshu_Roshua at 2007-7-12 21:58:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

That looks like a 'MyFaces' command, I'm not using MyFaces, just the JSF RI 1.1, with Java 1.4EE, Hibernate 3, within Netbeans 5.5 IDE with database Postgres 7.4.

Here are some other things I've tried to get this simple drop down box to work:

I've tried to write a custom converter which converts an ID to an Object, but this fails due to the fact it cannot validate this object.

I also tried setting up a dumb int variable which when set will also set the securityBean object to the same ID and store this in the current user bean. This didn't work also, it was always set to one for some reason, no matter what I did.

I think i'm going at this totally the wrong way, should I go and learn MyFaces (yet another thing to learn :() or is there a simple solution to create a drop down box with objects in it?

freebsdmap7a at 2007-7-12 21:58:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

In your code you have added a new SelectItem(securityBean, securityBean.getSecurity());. Is securityBean an Integer object? And issecurityBean.getSecurity()) an String object? Try converting them to Integer and String objects (in your f:selectItems) and map them to a int (in h:selectOne). This might solve the error.

Dilip

dilip_jsfa at 2007-7-12 21:58:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Huh, you're asking if SecurityBean is an Integer?

Anyway .. There are 2 general solutions for this problem: 1) maintain a backing map, Map<String, SecurityBean>, and use the String keys as selectitems and selecteditem, or 2) write a converter between String and SecurityBean and define it in the h:selectOneMenu.

BalusCa at 2007-7-12 21:58:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
What i meant was that we should be adding an ID and a value. but he was adding two custom objects as such. Isnt that an error by itself?
dilip_jsfa at 2007-7-12 21:58:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Hi,Yes this is possible, I do it almost on every page of my application, you only need to write a converter that converts your selectItem values to a String.Pieter
PieterPareita at 2007-7-12 21:58:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

I solved this in the end by sending the ID and using the standard set/get methods for that property to lookup the field in the table.

This works

Here is my getSecurityById(int id) method

// Get a single Security Level, return the bean.

public static SecurityBean getSecurityById(int id){

Session session = HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

// Search for the given id.

List result = session.createQuery("from SecurityBean where securityid=" + id).list();

SecurityBean securitybean = (SecurityBean) result.get(0);// Get the one result.

return securitybean;

}

Here is the getListSecuritySelect method which gets all the records from the Security table and puts them into a SelectItem object ready for use in the selectOneMenu component.

// Get a list of all Security Levels

public List getListSecuritysSelect(){

// If the select box hasn't already been filled out, create it

if(selectSecuritys == null){

selectSecuritys = new ArrayList();

for (Iterator iter = getListSecuritys().iterator(); iter.hasNext();){// Get a list of all Security Levels

SecurityBean securityBean = (SecurityBean) iter.next(); // Get the next SecurityBean from results

// Add to the list, make sure it's Value (String), Name (String to display in the drop down)

selectSecuritys.add(new SelectItem(Integer.toString(securityBean.getId()), securityBean.getSecurity()));

}

}

return selectSecuritys;

}

Note that I'm using the securityBean.getId() value now. I found trouble if I use this as an integer, so I had to convert it to a String. Then in my get/set methods I have:

public String getSecurityBeanId() {

String answer = new String();

if (securityBean != null){

answer = Integer.toString(securityBean.getId());// If we are editing an object this will be useful.

}

return answer;

}

public void setSecurityBeanId(String securityBeanId) {

securityBean = security.getSecurityById(Integer.parseInt(securityBeanId)); // Get the selected Security and set the object

this.securityBeanId = securityBeanId;

}

freebsdmap7a at 2007-7-12 21:58:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Is this alright to do it this way?
freebsdmap7a at 2007-7-12 21:58:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
You can.
BalusCa at 2007-7-12 21:58:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
No, you beter use a converter.Here is an examplehttps://javaserverfaces.dev.java.net/issues/show_bug.cgi?id=600(It will work when you use JSF1.1x)
PieterPareita at 2007-7-12 21:58:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...