Main Tutorials

How to pass new hidden value to backing bean in JSF

In some cases, you may need to pass a new hidden value to a backing bean. Generally, there are two ways :

1. HTML Tag + getRequestParameterMap()

Render hidden field with plain HTML input, hard-coded new hidden value and access in backing bean via getRequestParameterMap() method.

JSF…


<h:form id="myForm">
    <input type="hidden" name="hidden1" value="this is hidden2" />
    <h:commandButton value="submit" action="#{user.action}" />
</h:form>

Managed bean…


@ManagedBean(name="user")
@SessionScoped
public class UserBean
{
	public String action(){
	   String value = FacesContext.getCurrentInstance().
		getExternalContext().getRequestParameterMap().get("hidden1");
	}
}

2. JSF Tag + JavaScript

Render hidden field via “h:inputHidden” tag, assign new value via JavaScript.

JSF…


<script type="text/javascript">
   function setHiddenValue(new_value){
			
	document.getElementById('myForm:hidden2').value = new_value;
			
   }
</script>
<h:form id="myForm">		    
   <h:inputHidden id="hidden2" value="#{user.hidden2}" />
   <h:commandButton value="submit" action="..." onclick="setHiddenValue('this is hidden2');" />
</h:form>

Managed bean…


@ManagedBean(name="user")
@SessionScoped
public class UserBean
{
	public String hidden2;
	
	public void setHidden2(String hidden2) {
		this.hidden2 = hidden2;
	}
}

JSF 2.0 new hidden value example

A JSF 2.0 example, to demonstrate the use of above two methods to pass a new hidden value to a backing bean.

1. Managed Bean

A simple managed bean, assign name as “user”.


package com.mkyong.form;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import java.io.Serializable;

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

	public String hidden1;
	public String hidden2;
	
	public String getHidden2() {
		return hidden2;
	}

	public void setHidden2(String hidden2) {
		this.hidden2 = hidden2;
	}

	public String getHidden1() {
		return hidden1;
	}

	public void setHidden1(String hidden1) {
		this.hidden1 = hidden1;
	}

	public String action(){
		
	    String value = FacesContext.getCurrentInstance().
		getExternalContext().getRequestParameterMap().get("hidden1");
	    setHidden1(value);
    
	    return "start";
	}	
}

2. View Page

Two pages for demonstration.

demo.xhtml – two ways to pass a new hidden value.


<?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 setHiddenValue(new_value){
			
	     document.getElementById('myForm:hidden2').value = new_value;
			
	   }
	</script>
	</h:head>
    <h:body>
     <h1>JSF 2 pass new hidden value to backing bean</h1>
 
     <h:form id="myForm">

       <input type="hidden" name="hidden1" value="this is hidden2" />

       <h:inputHidden id="hidden2" value="#{user.hidden2}" />

       <h:commandButton value="submit" action="#{user.action}" 
                               onclick="setHiddenValue('this is hidden2');" />
     </h:form>
		
    </h:body>
</html>

start.xhtml – display hidden value via “h:outputText” tag.


<?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:body>
    	<h1>JSF 2 pass new hidden value to backing bean</h1>
 	<ol>
 	  <li>Hidden1 = <h:outputText value="#{user.hidden1}" /></li>
 	  <li>Hidden2 = <h:outputText value="#{user.hidden2}" /></li>
	</ol>
    </h:body>
</html>

3. Demo

URL : http://localhost:8080/JavaServerFaces/

jsf2-new-hidden-example-1
jsf2-new-hidden-example-2

Download Source Code

Download It – JSF-2-New-HiddenValue-Example.zip (10KB)

Reference

  1. JSF <h:inputHidden /> JavaDoc

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
14 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
wondyfraw
5 years ago

Thanks a lot man, you save time

Rafael Pedraza
1 year ago

Thanks for sharing your knowledge with us

Sergio Enriquez
7 years ago

Hey how can i do this with Prime faces tables wich would be the format??

mario
7 years ago

Thanks a lot.

Ganteppa
8 years ago

Thanks a lot.

Jayul
10 years ago

Thanks for this Example.But i still not get value in JSF bean

Here is my Code….

JavaScript Function…

function getScreenSize(){
alert(“Call”);
var screenWidth = screen.width;
var screenHeight = screen.height;
alert(screenHeight);
alert(screenWidth);
document.getElementById(“heightid”).value = screenWidth;
document.getElementById(“widthid”).value = screenHeight;
}

icefaces Components…

MyBean.java

public void clickMethod(){
JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(),”getScreenSize();”);
String value = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(“heightid”);
System.out.println(“Hello !! “+value);
}

Please help me out….

John M
10 years ago
Reply to  Jayul

I think while effective, this process can be thrown off if the javascript code takes awhile to be processed. By the time the javascript is done, the action=#{bean.method} has already executed and doesn’t see the new value in the hidden component.

I’m experiencing the same thing, I’ll just have to keep looking.

Adolfo C
3 years ago
Reply to  John M

I had the same problem and in my case it was the way to retrieve the info in the Bean. You have to call the property with the form name too –>

String value = FacesContext.getCurrentInstance().
getExternalContext().getRequestParameterMap().get(“hidden1”);

to:

String value = FacesContext.getCurrentInstance().
getExternalContext().getRequestParameterMap().get(“myForm:hidden1”);

Now it is working for me. I guess perhaps if you have only one form it would work without proceeding the forms name?

Jayul
10 years ago

Thanks for this Example.But i still not get value in JSF bean

Here is my Code….

MyBean.java

public void clickMethod(){
JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), “getScreenSize();”);
String value = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(“heightid”);
System.out.println(“Hello !! “+value);
}

View.jspx

function getScreenSize(){
alert(“Call”);
var screenWidth = screen.width;
var screenHeight = screen.height;
alert(screenHeight);
alert(screenWidth);
document.getElementById(“heightid”).value = screenWidth;
document.getElementById(“widthid”).value = screenHeight;
}

Please help me out….

Sai
11 years ago

Thanks a lot !! was stuck with the problem for couple of days and finally was able to get it !!

A.C
11 years ago

If I have input text and submit button in different forms, in that case in the actionlistener of the button the backing bean value of the input text is coming as null.Please help me how to overcome this problem

mad_tortle
11 years ago

thank you very much, I’ve been looking for this for days
🙂

sdds
13 years ago

what library stands “h” for? (in <h:inputHidden)

Mortros
13 years ago

Thanks, I could not find how to do that, I’m a noob with JSF xD