0

I am quite new to JSF 2.0 so this may be a very simple question.

I would now like to pass a self-defined object from one page to another using h:inputHidden, so I can get it by using request.getParameter("obj01").

I have passed the whole object into the value attribute of the h:inputHidden,

however I have get the following errors:

Cannot convert com.project01.obj.web.obj01@10562017 of type class java.lang.String to class com.project01.obj.web.obj01

So I suppose I have done something wrong.

Could anyone give me some advice on it ?

Thank you very much.

willome
  • 3,062
  • 19
  • 32
Hei
  • 513
  • 3
  • 13
  • 29
  • See [this answer](http://stackoverflow.com/a/13332562/1530938) and [this](http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ProcessingGETRequestParameters) – kolossus Nov 12 '12 at 09:14

1 Answers1

1

You can only pass Strings via request. But there is a solution for that:

Write a converter. Some codeexample could be found here.

http://www.mkyong.com/jsf2/custom-converter-in-jsf-2-0/

EDIT:

For example I passed Objects via a SelectOneMenu.

<h:selectOneMenu id="inputX" value="#{someBean.someObject}" converter="someConverter">
    <f:selectItems value="#{someBean.someObjectList}"/>
</h:selectOneMenu>

Put your converter in your faces config.

<converter>
    <description>Converter - X</description>
    <converter-id>someConverter</converter-id>
    <converter-class>de.package.company.SomeConverter</converter-class> 
</converter>

Converter:

public class SomeConverter implements Converter
{

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

        if (value != null)
            return (YourBean) new YourBeanDAO().find(Long.parseLong(value));

        return null;
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {

        if (arg2 != null && arg2 instanceof YourBean)
            return Long.toString(((YourBean) arg2).getId());

        return null;
    }
}
Nils
  • 314
  • 3
  • 17
  • 3
    Tip: for a SelectOneMenu, you can take a look at the OmniFaces converter, which can automatically convert from String to Object without using an expensive DAO call (it's based on the list that's already there to render the menu). – Mike Braun Nov 12 '12 at 09:26