The immediate attribute in JSF is commonly misunderstood. If you don’t believe me, check out Stack Overflow. Part of the confusion is likely due to immediate being available on both input (i.e.. <h:inputText />) and command (i.e. <h:commandButton />) components, each of which affects the JSF lifecycle differently.
Here is the standard JSF lifecycle:
For the purposes of this article, I’ll assume you are familiar with the basics of the JSF lifecycle. If you need an introduction or a memory refresher, check out the Java EE 6 Tutorial – The Lifecycle of a JavaServer Faces Application.
Note: the code examples in this article are for JSF 2 (Java EE 6), but the principals are the same for JSF 1.2 (Java EE 5).
immediate=true on Command components
In the standard JSF lifecycle, the action attribute on an Command component is evaluated in the Invoke Applicationphase. For example, say we have a User entity/bean:
01 |
public class User implements Serializable { |
04 |
private String firstName; |
08 |
private String lastName; |
10 |
/* Snip constructors, getters/setters, a nice toString() method, etc */ |
And a UserManager to serve as our managed bean:
03 |
public class UserManager { |
06 |
/* Snip some general page logic... */ |
08 |
public String addUser() { |
09 |
//Snip logic to persist newUser |
11 |
FacesContext.getCurrentInstance().addMessage(null, |
12 |
new FacesMessage("User " + newUser.toString() + " added")); |
And a basic Facelets page, newUser.xhtml, to render the view:
02 |
<h:panelGrid columns="2"> |
04 |
<h:outputText value="First Name: " /> |
06 |
<h:inputText id="firstName" |
07 |
value="#{userManager.newUser.firstName}" /> |
08 |
<h:message for="firstName" /> |
11 |
<h:outputText value="Last Name: " /> |
13 |
<h:inputText id="lastName" value="#{userManager.newUser.lastName}" /> |
14 |
<h:message for="lastName" /> |
19 |
<h:commandButton value="Add User" action="#{userManager.addUser()}" /> |
Which all combine to produce this lovely form:
When the user clicks on the Add User button, #{userManager.addUser} will be called in the Invoke Application phase; this makes sense, because we want the input fields to be validated, converted, and applied to newUser before it is persisted.
Now let’s add a “cancel” button to the page, in case the user changes his/her mind. We’ll add another <h:commandButton /> to the page:
2 |
<!-- Snip Input components --> |
4 |
<h:commandButton value="Add User" action="#{userManager.addUser()}" /> |
5 |
<h:commandButton value="Cancel" action="#{userManager.cancel()}" /> |
And the cancel() method to UserManager:
1 |
public String cancel() { |
4 |
FacesContext.getCurrentInstance().addMessage(null, |
5 |
new FacesMessage("Cancelled new user")); |
Looks good, right? But when we actually try to use the cancel button, we get errors complaining that first and last name are required:
This is because #{userManager.cancel} isn’t called until the Invoke Application phase, which occurs after the Process Validations phase; since we didn’t enter a first and last name, the validations failed before #{userManager.cancel} is called, and the response is rendered after the Process Validations phase.
We certainly don’t want to require the end user to enter a valid user before cancelling! Fortunately, JSF provides theimmediate attribute on Command components. When immediate is set to true on an Command component, the action is invoked in the Apply Request Values phase:
This is perfect for our Cancel use case. If we add immediate=true to the Cancel , #{userManager.cancel} will be called in the Apply Request Values phase, before any validation occurs.
2 |
<!-- Snip Input components --> |
4 |
<h:commandButton value="Add User" action="#{userManager.addUser()}" /> |
5 |
<h:commandButton value="Cancel" action="#{userManager.cancel()}" immediate="true"/> |
So now when we click cancel, #{userManager.cancel} is called in the Apply Request Values phase, and we are directed back to the home page with the expected cancellation message; no validation errors!
What about Input components?
Input components have the immediate attribute as well, which also moves all their logic into the Apply Request Valuesphase. However, the behavior is slightly different from Command components, especially depending on whether or not the validation on the Input component succeeds. My next article will address immediate=true on Input components. For now, here’s a preview of how the JSF lifecycle is affected:
摘自:https://www.javacodegeeks.com/2012/01/jsf-and-immediate-attribute-command.html
- 关于JSF中immediate属性的总结(三)
Okay, when should I use the immediate attribute? If it isn't entirely clear yet, here's a summary, c ...
- 关于JSF中immediate属性的总结(一)
Purpose The immediate attribute can be used to achieve the following effects: Allow a commandLink or ...
- 【Spring源码解读】bean标签中的属性(二)你可能还不够了解的 abstract 属性和 parent 属性
abstract 属性说明 abstract 在java的语义里是代表抽象的意思,用来说明被修饰的类是抽象类.在Spring中bean标签里的 abstract 的含义其实也差不多,表示当前bean是 ...
- 前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型
前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型 前言(题外话): 有人说拖延症是一个绝症,哎呀治不好了.先不说这是一个每个人都多多少少会有的,也不管它究竟对生活有多么大的 ...
- 使用虚幻引擎中的C++导论(二-UE4基类)
使用虚幻引擎中的C++导论(二) 第一,这篇是我翻译的虚幻4官网的新手编程教程,原文传送门,有的翻译不太好,但大体意思差不多,请支持我O(∩_∩)O谢谢. 第二,某些细节操作,这篇文章省略了,如果有不 ...
- Javascript中length属性的总结
Javascript中length属性的总结 一.StringObject中的length length属性是返回字符串的字符数目. 例如: // 普通字符串 var str = " ...
- OC中@property属性关键字的使用(assign/weak/strong/copy)
OC中@property属性关键字的使用(assign/weak/strong/copy) 一.assign 用于 ‘基本数据类型’.‘枚举’.‘结构体’ 等非OC对象类型 eg:int.bool等 ...
- CSS3中动画属性transform、transition 和 animation
CSS3中和动画有关的属性有三个 transform.transition 和 animation.下面来一一说明: transform 从字面来看transform的释义为改变,使 ...
- 【转】WPF中的Binding技巧(二)
WPF中的Binding技巧(二) 接上篇, 我们来看一看Elementname,Source,RelativeSource 三种绑定的方式 1.ElementName顾名思义就是根据Ui元素 ...
随机推荐
- MySql索引总结
索引概念 B+树索引分为聚集索引和非聚集索引(辅助索引),但是两者的数据结构都和B+树一样,区别是存放的内容. 可以说数据库必须有索引,没有索引则检索过程变成了顺序查找,O(n)的时间复杂度几乎是不能 ...
- Select into 的特点
使用 Select * into NewTable From OldTable 来生成新表的技能已经使用得好熟练了~但是有些东西还是需要注意一下.下面我就来分享几个栗子 使用select into ...
- mysql的缓冲查询和非缓冲查询
最近在开发一个PHP程序时遇到了下面的错误: PHP Fatal error: Allowed memory size of 268 435 456 bytes exhausted 错误信息显示允许的 ...
- 【原】pageResponse - 让H5适配移动设备全家(移动端适配)
上一篇文章<为什么选择iPhone5的分辨率作为H5视觉稿尺寸>最后留下了问题:是否需要视觉设计师设计多套的视觉稿供给前端工程师做页面适配呢?按照自己以前的方法,通常会要求设计师设计2套的 ...
- [html/css]清除浮动的相关技巧
以前只了解得很浅显,转载了一篇不错的文,学习参考 浮动会使当前标签产生向上浮的效果,同时会影响到前后标签.父级标签的位置及 width height 属性.而且同样的代码,在各种浏览器中显示效果也有可 ...
- 虚拟机+apache+php+mysql 环境安装配置
虚拟机的安装:直接下一步即可,注意修改路径. 安装完成后新建虚拟机,直接下一步.如果选择镜像文件后出现错误,可以试着去修改电脑bios中的虚拟化设置,改为enable,如下图: apache安装: 1 ...
- PAT 1044. 火星数字(20)
火星人是以13进制计数的: 地球人的0被火星人称为tret. 地球人数字1到12的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, no ...
- web前端开发中常用的尺寸和位置
我们在日常web前端开发过程中,会经常用到各种尺寸和位置.通常是js做动画的时候.轮播图,滚屏动画,粒子,碰撞检测,拖拽,滚动加载等等.这里我将常用的尺寸和位置的获取进行总结,不包括canvas,SV ...
- Winscp开源的SSH|SFTP
WinSCP 主要功能 图形用户界面 多语言与 Windows 完美集成(拖拽, URL, 快捷方式) 支持所有常用文件操作,支持基于 SSH-1.SSH-2 的 SFTP 和 SCP 协议 支持批处 ...
- ActiveMQ与spring集成实现Queue模式
ActiveMQ可以和spring很好的集成,下面我们来看看,如何做个集成的demo. (1)pom.xml引入相关jar <!-- spring相关 begin --> <depe ...