20.04.2013 Views

Practical RichFaces, Second Edition

Practical RichFaces, Second Edition

Practical RichFaces, Second Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 2 GETTING STARTED<br />

• deepMarine<br />

• NULL<br />

Ajax in JSF 2<br />

JSF 2 comes with basic Ajax functionality built in. If you have used <strong>RichFaces</strong> 3, then you will see that JSF<br />

2 Ajax was greatly inspired by the popular <strong>RichFaces</strong> tag. You will see that the concepts<br />

are the same, with a few things changed, such as attribute names. If you are new to <strong>RichFaces</strong>, don’t<br />

worry, in this chapter we will cover everything from the beginning.<br />

Ajax in JSF 2 comes in a form behavior. Notice that we call it a behavior instead of a<br />

component. A behavior is a new concept in JSF 2. A behavior in JSF 2 is a client-side behavior<br />

(functionality) that is added to a component. Let’s see how to use the standard Ajax functionality in JSF 2<br />

before we get to <strong>RichFaces</strong>.<br />

When working with Ajax in the context of JSF, you need to keep three things in mind: First, how to<br />

send an Ajax request; second, what to process on the server (partial view processing); and third, what to<br />

render (partial view rendering).<br />

Sending an Ajax Request<br />

Sending an Ajax request is pretty straight forward: you really just have one option and that’s using<br />

behavior. This behavior is always attached to another UI component on a page; it’s never used<br />

by itself. Listing 2-9 shows an example.<br />

Listing 2-9. Sending an Ajax Request<br />

<br />

<br />

<br />

That’s pretty simple, right? We just added the ability to fire an Ajax request when the value inside the<br />

input field changes. The Ajax request is fired from the client (browser) and always on some browser<br />

event, such as click, change, and so forth. To be more precise, it can be any event supported by the<br />

parent UI component. Components represent HTML elements in the browser, so it’s all the events that<br />

are supported by that particular HTML element.<br />

The managed bean is shown in Listing 2-10.<br />

Listing 2-10. Managed bean<br />

@ManagedBean<br />

@RequestScoped<br />

public class Bean {<br />

}<br />

private String text;<br />

// getter and setter<br />

You probably noticed that we didn’t actually specify the event, but said that the Ajax request would<br />

be fired when the value in the input field changes. The reason the Ajax request would fire on value<br />

change is because the default valuechange event is used. Every UI component in JSF 2 has a standard

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!