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 viagetRequestParameterMap() 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
Reference
转自:http://www.mkyong.com/jsf2/how-to-pass-new-hidden-value-to-backing-bean-in-jsf/
相关推荐
部门人员一对多的CRUD操作 hibernate: 管理实体关系映射 spring:数据源配置与获得session IOC依赖注入 和 AOP ...jsf : backing bean的操作 jsf 标签应用 jsf 信息国际化以资源绑定 jsf 表单验证 jsf 导航设定
由于backing bean添加了一个属性:myParam,这样,前台的jsp还要加上一个inputHidden。这里用一般的JSF模型:前台的jsp跟只引用一个backing bean,这样jsp提交后,backing bean才能restore。显然这种方式比较麻烦,...
JSF入门如果您是从使用的角度来看JSF,则您不用理会HTTP、数据转换等细节,JSF将细节都隐藏起来了,无论您是网页设计人员或是应用程序设计人员,都可以使用自己熟悉的方式来看JSF。 入门 藉由以下的几个主题,可以...
1、在backing bean的方法中调用函数1即可。如Abean中download方法调用了该方法,前台可以这样调用: <h:commandButton value="download" action="#{aBean.download}"> 或者 <h:commandLink value="download" ...
JSF live demo的实例很丰富,但是没有backing bean的源码,所下载的压缩包里面包含了所有需要的库以及backing bean的源码,图片等等,20080730获取
JSF框架中文教程,包括JSF入门、Backing Beans、Beans 上的 List, Map、JSF Expression Language、JSF 生命周期、Phase 事件、TableModel 类别、等有关jsf的知识。
Use Syngress' proven "How to Cheat" methodology to configure and build VMware's ESX Server version 3. This clear, concise guide provides all the information you need to become a virtual whiz! This ...
How to do it 216 How it works 217 There's more... 217 Managing who Sees Ads 218 Getting ready 218 How to do it 219 How it works 220 There's more... 220 Advanced conditions 221 Inserting ads ...
Once you know how to manage your SQL Server, you can learn in Chapter 5 how to automate many of the redundant monitoring and maintenance tasks. This chapter also discusses best practices on ...
Chapter 1, Game Development Basics, explains the basics of C++ programming, writing small programs to be used in games, and how to handle memory in games. Chapter 2, Object-Oriented Approach and ...
快速JSF Web开发,每页具有单个Backing bean,并且直接进行干净的导航。 2015年3月20日:发布了2.0.2版,并在Tomcat,JBoss和Weblogic上进行了测试。 包含了用Ant生成的简单Tomcat示例代码。 Asterion for JSF是JSF ...
create a new Network Interface Card and connect it to VLAN 'n' -net user[,vlan=n][,name=str][,net=addr[/mask]][,host=addr][,restrict=y|n] [,hostname=host][,dhcpstart=addr][,dns=addr][,tftp=dir][,...
2.1 Backing Beans 2.2 Beans的配置与设定 2.3 Beans上的List, Map 3. 数据转换与验证 3.1 标准转换器 3.2自定义转换器 3.3标准验证器 3.4自定义验证器 3.5错误信息处理 3.6 自定义转换, 验证标签 4. 事件处理 4.1 ...
shows you how to use the graphical MySQL Workbench to work with a MySQL database, and teaches you to code SQL queries to retrieve, add, update, and delete data in a MySQL database. Section 2 goes ...
shows you how to use the graphical MySQL Workbench to work with a MySQL database, and teaches you to code SQL queries to retrieve, add, update, and delete data in a MySQL database. Section 2 goes ...
难得的JSF学习资料,欢迎下载。 --------------------------------------- JSF入门 BACKING BEANS JSF生命周期 动作事件 ......
都隐藏起来了,无论您是网页设计人员或是应用程序设计人员,都可以使用自己熟悉的方式 来看JSF。 • 入门 藉由以下的几个主题,可以大致了解JSF 的轮廓与特性,我们来看看网页设计人员 与应用程序设计人员各负责什么...
The plan should minimally cover the ‘what and how’ in backing up critical data to local disk, onsite/offsite tape, or other tertiary storage: • What? -- what are the recovery time objectives (RTO)...
to pass stringent tests by platform holders to be allowed access to their development hardware. Today, anyone with a mobile phone or a tablet and a computer, even a laptop, can build a game and have ...
This book begins with a brief look at Moodle's background...Finally, you'll learn Moodle's security options, along with performance testing, and how to use the built-in Moodle performance testing script.