简体   繁体   中英

JSF 2.0 object value in h:inputHidden?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM