Description of Action listener in assumerver faces in action

Source: Internet
Author: User

When a user interacts with a control such as a command or an operation (gesture), an action event is triggered ). Components that generate action events are also called Action sources, including buttons, hyperlinks, and other components. Action events are handled by Action listeners.

There are two typical listeners: one will affect the navigation between pages, and the other will not affect the navigation. The listener that affects the navigation between pages mainly processes some business logic and returns several business logic results. The JSF navigation system selects the right next page based on this: it is not the traditional "True/false" results. Here we talk about "business logic", which can be any representative results .) Event Listeners that do not affect navigation are used to operate components on the current page, make changes to some business models, or modify backing bean attributes. They do not jump between pages.

Technically, navigation between all pages is handled by a single action listener. This listener automatically processes all events triggered by the component. Therefore, the listener does not need to register/BIND (Register) manually ). By default, this listener submits all the work to a method in your backing bean. Therefore, you can use different methods to process various business components in your program. Typically, most of your business logic will be handled by these methods.

When a component triggers an event, the default listener determines the event processing result. The result is represented by a string, for example: "success", "failure", etc. The results can also be divided into static and dynamic types. Static is defined during encoding, for example:
<H: commandbutton type = "Submit" value = "login" Action = "success" immadiate = "true"/>
In this example, When you click the button marked as login, the "success" service result is generated and the button is also triggered to be clicked. However, there is no corresponding method to handle this event.

The dynamic result is a string returned by the method used to process the event. A method may return multiple different results, depending on the business logic. The action listener automatically finds and executes the method you specified in the Action attribute of the component. We use el (Expression Language) to specify the attribute value. The following is an actual example of htmlcommandbutton:
<H: commandbutton type = "Submit" value = "login" Action = "# {user. login}"/>
When you click this button, an event that indicates the button is clicked is generated and the following event processing method is executed:

1 public class user {
2
3 Public String login (){
4 If () {// login successful
5 return "success ";
6} else {// Login Failed
7 Return "failure ";
8}
9}
10
11}

The returned value of the login method depends on the different business logic, which is not discussed here. The class user is a backing bean. Some of its property values are associated with the values of some input controls on the page. This class will have a corresponding definition in the faces-config.xml.

In the above example, there are only return values such as "success" and "failure". However, in actual applications, your program may require you to manipulate JSF components, business model objects, and add messages. It even implements complex functions such as direct page Jump, outputting an appropriate response, and implementing interaction with component services such as database EJB. The action listener determines the page Jump Based on the return value of the method and the definition in the navigation system.

When you need to execute business logic without page Jump, you can bind an action listener Method to the component, which is different from the action method ), in addition to binding events, this method can also manipulate components. Let's look at the following example:
<H: commandbutton id = "redisplaybutton" type = "Submit" value = "redisplay" actionlistener = "# {loginform. doit ()}"/>
As in the preceding example, an event is generated when you click this button. However, this time, the action listener method is executed instead of the previous action method.

1 Public void doit (actionevent event ){
2 htmlcommandbutton button = (htmlcommandbutton) event. getcomponent ();
3 button. setvalue ("Try again! ");
4}

This method changes the title of the previously clicked button, although this method is of little use. But it is important to sign this method. It accepts an actionevent class parameter and does not return a value. This is the main difference between action listener method and action method! After the preceding method is executed, the page is refreshed to display the effect.

Generally, the action listener method is used to change the current page. For example, you can use an interface to implement the value-changing event. However, in most cases, it is enough to use the method in the backing bean.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.