稍后奉上翻译__==__
Action events are triggered when a user interacts with a control that represents a command,or a user gesture. Components that generate action events,also called action sources,include controls such as buttons or hyperlinks.Action events are handled by action listeners.
There are two types of action listeners:those that affect navigation,and those that don't.Action listeners that affect navigation typically perform some processing and then return a logical outcome that is used by JSF's navigation system to select the next page(which may actually be the same page that's currently being viewed). Listeners that don't affect navigation usually manipulate components in the current view,or perform some processing that changes model object or backing bean properties,but doesn't alter the current page the user is accessing.
Technically,all navigation is handled by a single action listener.This listener automaatically handles any action events fired by a component,so it doesn't need to be registered manually.By default,this action listener delegates all of its work to action methods in your backing beans,so you can have different action methods handle different parts of your application.Typically,most of your application logic will be located in these methods.(The action listener is pluggable,so it's possible to replace it with one that doesn't use action methods at all.)
When a component fires an action event,this default action listener determines its outcome string---"success"or"failure",for example.There are two basic types of outcomes:static and dynamic.Static outcomes are hardcoded string that are declared with the component or set in code:
<h:commondButton type="submit" value="Login" action="success" immediate="true"/>
In this example,the static outcome of "success" will be generated when the user clicks on this HtmlCommandButton and generates an action event--no action method will be called.
Dynamic outcomes are strings returned by action methods themselves---an action method might return a different outcome depending on whateever application logic it performs.The action listener looks for an action method whenever you use a JSF EL(Expression Language) expression in the action property.Here's an HtmlCommandButton that exectutes an action method instead:
<h:commandButton type="submit" value="Login" action="#{user.login}"/>
When a user clicks on this button,an action event is fired,and the follwing method is executed in response to the event:
2
3 public String login() {
4 if () { // successfully login
5 return " success " ;
6 } else {
7 return " failure " ;
8 }
9 }
10
11 }
Based on some voodoo application logic,this action method returns an outcome of either "success" or "failure" . User is a backing bean whose properties are wired up to input control values on the page,and is configured via the Managed Bean Creation facility.
My example has voodoo logic,but your action methods can manipulate JSF components,model objects,or add messages.They can also do other fun tasks,such sa performing a redirect,rendering a response(a graphic or some binary type of data),ading events,and talking to databases,EJB(Enterprise JavaBean) servers,or web services.The action listener uses the outcome of an action method to hook into the navigation system and determine what page to choose next.
When you need to execute application logic that is not associated with navigation you can associate an action listener method with a component.Unlike action methods,action listener methods have access to the component that fired the event as well.Take a look at this example:
<h:commandButton id="redisplayCommand" type="submit" value="Redisplay" actionListener="#{myForm.doIt}" />
Like the previous example , when a user clicks on this HtmlCommandButton , an action event is fired. This time,however,the action listener method is executed instead of the action method:
2 HtmlCommandButton button = (HtmlCommandButton)event.getCommponent();
3 button.setValue( " It's done! " );
4 }
5
This method changes the value(label) of the button that fired the event---not terribly useful.What's important,however,is its method signature.Instead of accepting an ActionEvent sa a parameter and don't return an outcome at all. After this method executes,the page will be redisplayed,incorporating any changes made by the method.
Usually,you use action listener methods for changes that affect the current view. Like value-change listeners,you can also implement an action listener using a Java interface,although in most cases using a method in an existing backing bean is sufficient.