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 Application phase. For example, say we have a User entity/bean:public class User implements Serializable { @NotBlank @Length(max = 50) private String firstName; @NotBlank @Length(max = 50) private String lastName; /* Snip constructors, getters/setters, a nice toString() method, etc */ }
And a UserManager to serve as our managed bean:
@SessionScoped @ManagedBean public class UserManager { private User newUser; /* Snip some general page logic... */ public String addUser() { //Snip logic to persist newUser FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("User " + newUser.toString() + " added")); return "/home.xhtml"; }
And a basic Facelets page, newUser.xhtml, to render the view:
<h:form> <h:panelGrid columns="2"> <h:outputText value="First Name: " /> <h:panelGroup> <h:inputText id="firstName" value="#{userManager.newUser.firstName}" /> <h:message for="firstName" /> </h:panelGroup> <h:outputText value="Last Name: " /> <h:panelGroup> <h:inputText id="lastName" value="#{userManager.newUser.lastName}" /> <h:message for="lastName" /> </h:panelGroup> </h:panelGrid> <h:commandButton value="Add User" action="#{userManager.addUser()}" /> </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:
<h:form> <!-- Snip Input components --> <h:commandButton value="Add User" action="#{userManager.addUser()}" /> <h:commandButton value="Cancel" action="#{userManager.cancel()}" /> </h:form>
And the cancel() method to UserManager:
public String cancel() { newUser = new User(); FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Cancelled new user")); return "/home.xhtml"; }
Looks good, right? But when we actually try to use the cancel button, we get errors complaining that first and last name are required:
We certainly don't want to require the end user to enter a valid user before cancelling! Fortunately, JSF provides the immediate 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
<h:form> <!-- Snip Input components --> <h:commandButton value="Add User" action="#{userManager.addUser()}" /> <h:commandButton value="Cancel" action="#{userManager.cancel()}" immediate="true" /> </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!
Hi Jerry,
ReplyDeleteI ran across your blog while looking for content for DZone's Javalobby (java.dzone.com) and was wondering if you would be interested in having some of your recent posts republished on our site. If so, please send me an email so I can provide some more information.
Thanks,
Chris Smith
Great article.
ReplyDeleteWaiting for the next part!
Great job
ReplyDeleteThanks John.
please can you share the link to all your blogs.
ReplyDelete.
Thanks
John.
Great Article
ReplyDeleteJava Projects for Engineering Students of Computer Science
Java Training in Chennai
Project Centers in Chennai for CSE
Java Training in Chennai
Đặt mua vé rẻ Aivivu, tham khảo
ReplyDeletevé máy bay tết
vé máy bay đi Mỹ tháng nào rẻ nhất
vé máy bay đi Pháp giá bao nhiêu
vé máy bay đi hàn quốc vietnam airline
săn vé máy bay đi nhật giá rẻ
mua vé máy bay đi Anh
vé máy bay giá rẻ
ve may bay di san francisco
giá vé máy bay đi Los Angesles
Your post is very good and unique. I am eagerly waiting for your new post.
ReplyDeleteimpact of social media marketing
artificial intelligence uses
artificial intelligence technology today
use of php language
rpa developer jobs
advanced php interview questions