user303451
user303451

Reputation: 41

How to read a value from a h:inputHidden in the managed bean

I have a JSF page which has a h:inputHidden component and I am setting a hardcoded value. I have set the id, name and value attributes. But when I access the value from the managed bean, I am getting null.

I also noticed that, during runtime, the name is changing to some auto generated id.

Appreciate any help.

Upvotes: 2

Views: 7837

Answers (2)

Samuel Bedassa
Samuel Bedassa

Reputation: 1

You can use this:

@ManagedBean(name="myBean")
@SessionScoped
public class MyBean implements Serializable {

    String myValue= "I'm Hidden value!";

    public String getMyValue() {
        return myValue;
    }

    public void setMyValue(String myValue) {
        this.myValue = myValue;
    }   

}

Upvotes: 0

Romain Linsolas
Romain Linsolas

Reputation: 81577

The behavior of the <h:inputHidden> is the same as for a <h:inputText> component for example:

<h:inputHidden id="myHiddenField" value="#{myBean.myValue}"/>

will refer to the property myValue of the bean myBean. So you will have to provide both getMyValue() and setMyValue(String) in this bean.

So if you change the value of this hidden field on the client side (using Javascript), then the new value will be updated on the bean side once the form is submitted.

Regarding the ID, you must specify the id attribute, otherwise JSF will generate one for you (something like j_id123 for example). If you specify a value for this attribute, the ID of the HTML tag will be the one you specified, prefixed by your form ID. So in the following example:

<h:form id="myForm">
    <h:inputHidden id="myField" .../>

the HTML <input> tag will have the id myForm:myField (note the : used as a separator of ids).

Upvotes: 5

Related Questions