SelectOneMenu and converters do not work

Hi all,

I am developing a weblog platform. Users can have many blogs. I wrote an edit.jsp page where users add or edit articles. Here users can choose on which blog they want to publish their articles.

entity bean for user

publicclass Userimplements Serializable{

...

@Transient

public List<SelectItem> getBlogItems(){

List<SelectItem> blogItems =new LinkedList<SelectItem>();

for (Blog blog: this.getBlogs()){

blogItems.add(new SelectItem(blog, blog.getAbout()));

}

return blogItems;

}

...

}

converter

publicclass BlogConverterimplements Converter{

public Object getAsObject(FacesContext context, UIComponent component, String value){

Integer id =new Integer(value);

Blog blog = JLogPlatform.getInstance().

getBlogs().getBlog(id.intValue());

return blog;

}

public String getAsString(FacesContext context, UIComponent component, Object value){

return ((Blog)value).getId()+"";

}

}

Piece of code in page edit.jsp

<h:selectOneMenu id="selectBlog" required="true"

value="#{articleBean.blog}"

converter="BlogConverter" >

<f:selectItems value="#{articleBean.user.blogItems}" />

</h:selectOneMenu>

...

<h:commandButton value="Save"

action="listArticles"

actionListener="#{articleBean.saveArticle}" />

Here articleBean.blog is a field of type Blog and articleBean is a managed backing bean.

I can view page edit.jsp, however when I click on button "Save" to update or add an article, I get the following error:

java.lang.IllegalArgumentException: Value binding'#{articleBean.user.blogItems}'of UISelectItems with component-path{Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /protected/editArticle.jsp][Class: javax.faces.component.html.HtmlForm,Id: _id0][Class: javax.faces.component.html.HtmlPanelGrid,Id: _id1][Class: javax.faces.component.html.HtmlSelectOneRadio,Id: selectBlog][Class: javax.faces.component.UISelectItems,Id: _id4]} does not reference an Object of type SelectItem, SelectItem[], Collection or Map but of type :null

The following code is the saveArticle method of articleBean bean

publicvoid saveArticle(ActionEvent event){

this.setModificationDate(new Date());

JLogPlatform.getInstance().

getArticles().

addArticle(this);

}

any idea?

Thanks,

Marco

[3911 byte] By [emptyseta] at [2007-10-2 8:30:42]
# 1

It seems that articleBean.user is null.

Do you put articleBean in session scope?

I think the problem occurs in UISelectOne.validateValue().

The javadoc says:

In addition to the standard validation behavior inherited from UIInput, ensure that

any specified value is equal to one of the available options. If it is not, enqueue

an error message and set the valid property to false.

In addition, I guess you will need to define Blog.equals() because validateValue() uses it.

yuki.yoshidaa at 2007-7-16 22:31:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...