How to pass new hidden value to backing bean in JSF
2016-10-31 15:32
1701 查看
https://www.mkyong.com/jsf2/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 :
Render hidden field with plain HTML input, hard-coded new hidden value and access in backing bean via getRequestParameterMap() method.
JSF…
Managed bean…
Render hidden field via “h:inputHidden” tag, assign new value via JavaScript.
JSF…
Managed bean…
A JSF 2.0 example, to demonstrate the use of above two methods to pass a new hidden value to a backing bean.
A simple managed bean, assign name as “user”.
Two pages for demonstration.
demo.xhtml – two ways to pass a new hidden value.
start.xhtml – display hidden value via “h:outputText” tag.
URL : http://localhost:8080/JavaServerFaces/
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/
Download Source Code
相关文章推荐
- How to get Hidden Column Value in GridView(ASP.net GridView如何获取隐藏列的值)
- 4 ways to pass parameter from JSF page to backing bean
- flex/in the datagrid,how to get the new value of cell in the datagrid after edit
- ADF HOW TO: How to refresh page/region in backing bean
- How to set a hidden value in Razor
- ADF HOW TO: How to refresh page/region in backing bean
- How to invoke the method of managed bean and render view in JSF when we are outside the lifecycle of JSF
- How to inject value into bean properties in Spring
- How To Use The New Multivalue Field In Access 2007/2010 Sunday, June 19th, 2011
- How to pass NULL value to a Multi value parameter in SSRS?
- How to add a new "CustomAction" in ECB for a specific list
- whatever how, this should be record and be researched later. ---- about how to enable gpio value set in mach_smdk6410.c
- (C#) A demo on how to auto fill out the "account" value in log in web page in "WebBrowser" control.
- How to use the new CSS syntax in Flex 4
- How to pass in/out return VB Byte array from a COM Component written in C#
- How to chage the default opstion value when adding new products?
- How do I pass a CLOB value to a stored procedure
- How to clear a TextBox value in a single click in C# .NET
- How to set the sequence value while saving the record in OA Framework
- How to add a new keycode in android