How to set a hidden value to jsf backing bean from view

jsf
14,013

Option 1.

Have your property initialized to your hardcoded value. JSF will auto-update this property on form submit. So, in case it has changed you will end up with a renewed property value in your action method.

String fApproverEmail = "default";

<h:inputHidden id="app" value="#{leaveBean.fApproverEmail}" />

Option 2.

Have a plain HTML <input type="hidden"> or valueless <h:inputHidden>. This way the submitted value is available in request parameter map. So you'll be able to grab it from ExternalContext#getRequestParameterMap() with its name as the key. But beware that in case your object's not a string you'll have to do conversion/validation on your own and action method is a wrong place to put that logic.

String fApproverEmail;
public void action() {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    String s1 = ec.getRequestParameterMap().get("plain");
    String s2 = ec.getRequestParameterMap().get("form:jsf");
    fApproverEmail = ...;//and-or other logic
}

<h:form id="form">
    <h:inputHidden id="jsf" />
    <input type="hidden" id="plain" name="plain" value="#{backingBean.fApproverEmail}"/>
    ...
</h:form>
Share:
14,013
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin almost 2 years

    I am having a hard-coded value that needs to be set to the jsf backing bean on form submit.

    Can any one tell this please.

     <h:inputHidden value="#{leaveBean.fApproverEmail}"></h:inputHidden>
    

    but i want to send a hard coded value inplace of "#{leaveBean.fApproverEmail}" and set it to a property of backing bean..

  • skuntsel
    skuntsel over 10 years
    Please rollback your edit and press Ask question button to ask a new question. Things are not to be done this way.