12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, 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.

C H A P T E R 12■ ■ ■Processing and NavigationIn the previous chapter, I showed you how to create web pages using different technologies (HTML, JSP,JSTL, etc.), knowing that JSF is the preferred <strong>Java</strong> <strong>EE</strong> specification for writing modern web applications.However, drawing pages <strong>with</strong> graphical components is not enough; these pages need to interact <strong>with</strong> aback-end system, navigate through pages, and validate and convert data. JSF is a rich specification:managed beans allow you to invoke the business tier and to navigate in your application, and a set ofclasses allow you to convert component values to and from a corresponding type or validate them toconform to business rules. With the use of annotations, it is now easy to develop custom converters andvalidators.JSF 2.0 brings simplicity and richness in terms of dynamic user interfaces. It natively supports Ajaxcalls in a simple manner. The specification comes <strong>with</strong> a <strong>Java</strong>Script library, allowing asynchronous callsto the server and refreshing small portions of the page.Creating user interfaces, controlling navigation in the entire application, and calling business logicsynchronously or asynchronously are possible because JSF is built on the Model-View-Controller (MVC)design pattern. Each part is separate from the other, allowing the UI to change <strong>with</strong>out impacting thebusiness logic and vice versa.This chapter will present all these concepts, complementing the previous chapters.The MVC PatternJSF, and most web frameworks, encourage separation of concerns by using variations of the MVC designpattern. MVC is an architectural pattern used to isolate business logic from user interface. Business logicdoesn’t mix well <strong>with</strong> user interface code. When the two are mixed, applications are much harder tomaintain and less scalable. In the “<strong>Java</strong>Server Pages” section in Chapter 11, I showed you a JSP <strong>with</strong> <strong>Java</strong>code and SQL statements. Though technically correct, you can imagine how difficult it would get tomaintain <strong>with</strong> time. The page mixes two developer roles (graphic designer and business logic developer),and it could end up using many more APIs (database access, EJB invocation, and so on), handlingexceptions, or processing complex business logic. All that would result in a very complex web page.When MVC is applied, it results in a loosely coupled application; <strong>with</strong> this type of application, it is easierto modify either the visual appearance of the application or the underlying business rules <strong>with</strong>out oneaffecting the other.In MVC, the model represents the data of the application, the view corresponds to the userinterface, and the controller manages the communication between both (see Figure 12-1).345

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

Saved successfully!

Ooh no, something went wrong!