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 {
02  @NotBlank
03  @Length(max = 50)
04  private String firstName;
05  
06  @NotBlank
07  @Length(max = 50)
08  private String lastName;
09  
10  /* Snip constructors, getters/setters, a nice toString() method, etc */
11 }

And a UserManager to serve as our managed bean:

01 @SessionScoped
02 @ManagedBean
03 public class UserManager {
04  private User newUser;
05  
06  /* Snip some general page logic... */
07  
08  public String addUser() {
09   //Snip logic to persist newUser
10  
11   FacesContext.getCurrentInstance().addMessage(null,
12     new FacesMessage("User " + newUser.toString() + " added"));
13  
14   return "/home.xhtml";
15  }

And a basic Facelets page, newUser.xhtml, to render the view:

01 <h:form>
02  <h:panelGrid columns="2">
03  
04   <h:outputText value="First Name: " />
05   <h:panelGroup>
06    <h:inputText id="firstName"
07     value="#{userManager.newUser.firstName}" />
08    <h:message for="firstName" />
09   </h:panelGroup>
10  
11   <h:outputText value="Last Name: " />
12   <h:panelGroup>
13    <h:inputText id="lastName" value="#{userManager.newUser.lastName}" />
14    <h:message for="lastName" />
15   </h:panelGroup>
16  
17  </h:panelGrid>
18  
19  <h:commandButton value="Add User" action="#{userManager.addUser()}" />
20 </h:form>

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:

1 <h:form>
2  <!-- Snip Input components -->
3  
4  <h:commandButton value="Add User" action="#{userManager.addUser()}" />
5  <h:commandButton value="Cancel" action="#{userManager.cancel()}" />
6 </h:form>

And the cancel() method to UserManager:

1 public String cancel() {
2  newUser = new User();
3  
4  FacesContext.getCurrentInstance().addMessage(null,
5    new FacesMessage("Cancelled new user"));
6  
7  return "/home.xhtml";
8 }

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.

1 <h:form
2  <!-- Snip Input components -->
3  
4  <h:commandButton value="Add User" action="#{userManager.addUser()}" />
5  <h:commandButton value="Cancel" action="#{userManager.cancel()}" immediate="true"/>
6 </h:form>

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属性的总结(二)的更多相关文章

  1. 关于JSF中immediate属性的总结(三)

    Okay, when should I use the immediate attribute? If it isn't entirely clear yet, here's a summary, c ...

  2. 关于JSF中immediate属性的总结(一)

    Purpose The immediate attribute can be used to achieve the following effects: Allow a commandLink or ...

  3. 【Spring源码解读】bean标签中的属性(二)你可能还不够了解的 abstract 属性和 parent 属性

    abstract 属性说明 abstract 在java的语义里是代表抽象的意思,用来说明被修饰的类是抽象类.在Spring中bean标签里的 abstract 的含义其实也差不多,表示当前bean是 ...

  4. 前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型

    前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型 前言(题外话): 有人说拖延症是一个绝症,哎呀治不好了.先不说这是一个每个人都多多少少会有的,也不管它究竟对生活有多么大的 ...

  5. 使用虚幻引擎中的C++导论(二-UE4基类)

    使用虚幻引擎中的C++导论(二) 第一,这篇是我翻译的虚幻4官网的新手编程教程,原文传送门,有的翻译不太好,但大体意思差不多,请支持我O(∩_∩)O谢谢. 第二,某些细节操作,这篇文章省略了,如果有不 ...

  6. Javascript中length属性的总结

    Javascript中length属性的总结 一.StringObject中的length     length属性是返回字符串的字符数目. 例如: // 普通字符串 var str = " ...

  7. OC中@property属性关键字的使用(assign/weak/strong/copy)

    OC中@property属性关键字的使用(assign/weak/strong/copy) 一.assign 用于 ‘基本数据类型’.‘枚举’.‘结构体’ 等非OC对象类型 eg:int.bool等 ...

  8. CSS3中动画属性transform、transition 和 animation

    CSS3中和动画有关的属性有三个 transform.transition 和 animation.下面来一一说明:        transform   从字面来看transform的释义为改变,使 ...

  9. 【转】WPF中的Binding技巧(二)

    WPF中的Binding技巧(二)     接上篇, 我们来看一看Elementname,Source,RelativeSource 三种绑定的方式 1.ElementName顾名思义就是根据Ui元素 ...

随机推荐

  1. xml类型使用注意事项

    xml 的数据类型在平常的开发也是很常用的,燃鹅.也是有一些地方需要留意.今天我就分享几个测试的例子. 使用 xquery.exist (有但不仅仅限于)的注意事项.通常使用来判断节点是否存在,值是否 ...

  2. PHP中的数据库三、redis

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  3. ubuntu下修改键位

    尴尬的背景: 服役5年的笔记本,最近键盘失灵,部分键位彻底失去响应.最蛋疼的是左右方向键都不能用了 ○| ̄|_ 解决方案是,通过xmodmap命令,用其他相对鸡肋些的键位替代方向键. 1 查看各个键位 ...

  4. 3-EM的安装和使用

    EM的安装和使用 一.EM工具的安装和使用 1.保证数据库启动 2.保证侦听启用 3.通过这个命令可以查看到侦听端口1521的状态信息 4.启动em工具 5.查看em工具是否运行 6.登陆网站并进行操 ...

  5. [python]爬虫学习(三)糗事百科

    import requestsimport osfrom bs4 import BeautifulSoupimport timepage=2url='http://www.qiushibaike.co ...

  6. Everything(文件搜索神器)

    前言 Everything官网: http://www.voidtools.com/ 软件版本: V1.3.4.686 (x64) 操作系统: windows 7/10 搜索FTP(内网)资源 比如内 ...

  7. oracle add_months函数

    oracle add_months函数 add_months 函数主要是对日期函数进行操作,举例子进行说明 add_months 有两个参数,第一个参数是日期,第二个参数是对日期进行加减的数字(以月为 ...

  8. SQL用法操作合集

    SQL用法操作合集   一.表的创建 1.创建表 格式: 1 CREATE TABLE 表名 2 (列名 数据类型(宽度)[DEFAULT 表达式][COLUMN CONSTRAINT], 3 ... ...

  9. [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  10. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...