JSF Tutorial - JSF Hidden Value Example








The h:inputHidden tag renders an HTML input element of the type "hidden".

The following JSF tag

<h:inputHidden value="Hello World" id="hiddenField" />

is rendered to the following HTML tag.

<input id="jsfForm:hiddenField" type="hidden" name="jsfForm:hiddenField" 
   value="Hello World" />

Tag Attributes

AttributeDescription
idid for the tag
bindingReference to the component used in a backing bean
valuevalue binding
valueChangeListenerA method binding that responds to value changes
converterConverter class name
acceptComma-separated list of content types for a form
accept-charsetComma- or space-separated list of character encodings for a form.
borderPixel value for an element's border width
immediateProcess validation early in the life cycle




Example

The following code is from UserBean.java.

package com.java2s.common;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;

@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable {

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

  public String getAnswer() {
    return answer;
  }

  public void setAnswer(String answer) {
    this.answer = answer;
  }

  
}

The following code is from demo.xhtml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html">

  <h:head>
  <script type="text/javascript">
    function printHiddenValue(){
      try{
        console.log(document.getElementById('myform:hiddenId').value);  
      }catch(e){
        console.log(e);
      }
    }
  </script>
  </h:head>
    <h:body>
      <h1>JSF 2 hidden value example</h1>
 
    <h:form id="myform">
        <h:inputHidden value="#{user.answer}" id="hiddenId" />
        <h:commandButton type="button" value="ClickMe" onclick="printHiddenValue()" />
      </h:form>
 
    </h:body>
</html>


Download Hidden_Value.zip





To RUN

Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.

After Tomcat finish starting, type the following URL in the browser address bar.

http://localhost:8080/simple-webapp/demo.xhtml