Thodoris Bais

About Thodoris Bais

Thodoris Bais is currently working as a Junior Java Developer while on parallel, pursuing a diploma at Informatics & Telecommunications Engineering. He specializes in Java Core, Android and J2EE and looks forward to being a professional Software Engineer. He is also a quick learner and curious about new technologies.

Hidden Value Example with JSF 2.0

In this example, we are going to demonstrate a simple way to implement a hidden value field in JSF. Hidden value fields, are used to submit data that are not inserted and authorised from the user. Some years ago (J2EE 1.4), a hidden value field was one of the three techniques that were used to implement web sessions.

In JSF, we can use the <h:inputHidden /> tag, in order to render an HTML hidden value field. For example, the JSF tag <h:inputHidden value="sample text" /> will render back, this HTML tag expression:
<input type="hidden" name="random value" value="some text" />. By the end of this example, you will be able to render hidden value fields through <h:inputHidden /> tag and to also access the hidden values of them, with Javascript.

1. Managed Bean

Our usual, simple Managed Bean, declared as “user”.

UserBean.java

package com.javacodegeeks.enterprise.jsf.hiddenvalue;

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

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

	private static final long serialVersionUID = 7134492943336358840L;
	String answer = "Hi from hidden value!";

	public String getAnswer() {
		return answer;
	}

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

2. Our Page

Its scope is to render a hidden value through the fore-mentioned JSF tag and if a button event occurs, then it has to print the value of our hidden field; as we already said, this will be implemented with Javascript.

index.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>
    	<title>JSF Hidden Value Example</title>
    	<script type="text/javascript">
    		function printHiddenValue()
    		{
    			alert(document.getElementById('simpleform:hiddenID').value);
    		}
    	</script>
    </h:head>
    <h:body> 
    	<h1>JSF 2.0 Hidden Value Example</h1>
    	<h:form id="simpleform">
    		<h:inputHidden value="#{user.answer}" id="hiddenID" />
    		<h:commandButton value="Click Me!" onclick="printHiddenValue()" />
    	</h:form>
    </h:body>
</html>

3. Demo

Another very simple example, let’s try to run our application by accessing http://localhost:8080/HiddenValueJSF ; if everything goes right, after clicking the button under our page’s heading, we should get something like this:

image1

This was an example of Hidden Value in JSF 2.0. You can also download the source code for this example: HiddenValueJSF

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books