Default Action for a JSF Form
Description
The current JSF-RI implementation does not allow to you to define the default action in case the user submits the form by pressing the Enter key instead of clicking the action button explicitly. As a result, the form action becomes undefined and the current page is just reloaded. Developers have to write additional javascript code to prevent such undesirable behavior.
The j4j:defaultAction custom component included in the j4j tag libraries allows you to define the default action for a form by adding as a child tag to the commandButton tag.
PS: This component was writen before even first version fo Firefox is released. This is a code that works also in FireFox. function trapEnter(evt) { var keycode; if (evt) ; else if (window.event) evt = window.event; else if (event) evt = event; else return true; if (evt.charCode) keycode = evt.charCode; else if (evt.keyCode) keycode = evt.keyCode; else if (evt.which) keycode = evt.which; else keycode = 0; if (keycode == 13) { document.getElementById('form:save').click(); return false; } else return true; }
How to Download
Download the compiled code using this URL: j4j.jar (4 K)
How to Use
1. Put the j4j.jar file in the WEB-INF/lib folder of your project.
2. Add the following declaration at the top of the page:
... taglib uri="http://javascript4jsf.dev.java.net/" prefix="j4j" ...
3. Add the j4j:defaultAction component as an empty child element of the commandButton that you want to make a default button.
Source Code
Here is the part of the library structure in j4j.jar that relates to this component:
The boldfaced code shows how to insert a reference to the proxy component in the faces-config.xml file:
org.j4j.defaultAction org.j4j.components.UIDefaultAction
Here is the content of the j4j.tld file:
retirados de http://www.jsftutorials.net/defaultActionTag.html
Nenhum comentário:
Postar um comentário