The decorators Example: Decorating a Bean (original) (raw)
2. Using the Tutorial Examples
3. Getting Started with Web Applications
4. JavaServer Faces Technology
7. Using JavaServer Faces Technology in Web Pages
8. Using Converters, Listeners, and Validators
9. Developing with JavaServer Faces Technology
10. JavaServer Faces Technology: Advanced Concepts
11. Using Ajax with JavaServer Faces Technology
12. Composite Components: Advanced Topics and Example
13. Creating Custom UI Components and Other Custom Objects
14. Configuring JavaServer Faces Applications
16. Uploading Files with Java Servlet Technology
17. Internationalizing and Localizing Web Applications
18. Introduction to Web Services
19. Building Web Services with JAX-WS
20. Building RESTful Web Services with JAX-RS
21. JAX-RS: Advanced Topics and Example
23. Getting Started with Enterprise Beans
24. Running the Enterprise Bean Examples
25. A Message-Driven Bean Example
26. Using the Embedded Enterprise Bean Container
27. Using Asynchronous Method Invocation in Session Beans
Part V Contexts and Dependency Injection for the Java EE Platform
28. Introduction to Contexts and Dependency Injection for the Java EE Platform
29. Running the Basic Contexts and Dependency Injection Examples
30. Contexts and Dependency Injection for the Java EE Platform: Advanced Topics
31. Running the Advanced Contexts and Dependency Injection Examples
The encoder Example: Using Alternatives
The Coder Interface and Implementations
The encoder Facelets Page and Managed Bean
To Build, Package, and Deploy the encoder Example Using NetBeans IDE
To Run the encoder Example Using NetBeans IDE
To Build, Package, and Deploy the encoder Example Using Ant
To Run the encoder Example Using Ant
The producermethods Example: Using a Producer Method To Choose a Bean Implementation
Components of the producermethods Example
Running the producermethods Example
To Build, Package, and Deploy the producermethods Example Using NetBeans IDE
To Build, Package, and Deploy the producermethods Example Using Ant
To Run the producermethods Example
The producerfields Example: Using Producer Fields to Generate Resources
The Producer Field for the producerfields Example
The producerfields Entity and Session Bean
The producerfields Facelets Pages and Managed Bean
Running the producerfields Example
To Build, Package, and Deploy the producerfields Example Using NetBeans IDE
To Build, Package, and Deploy the producerfields Example Using Ant
To Run the producerfields Example
The billpayment Example: Using Events and Interceptors
The PaymentHandler Event Listener
The billpayment Facelets Pages and Managed Bean
The LoggedInterceptor Interceptor Class
Running the billpayment Example
To Build, Package, and Deploy the billpayment Example Using NetBeans IDE
To Build, Package, and Deploy the billpayment Example Using Ant
To Run the billpayment Example
32. Introduction to the Java Persistence API
33. Running the Persistence Examples
34. The Java Persistence Query Language
35. Using the Criteria API to Create Queries
36. Creating and Using String-Based Criteria Queries
37. Controlling Concurrent Access to Entity Data with Locking
38. Using a Second-Level Cache with Java Persistence API Applications
39. Introduction to Security in the Java EE Platform
40. Getting Started Securing Web Applications
41. Getting Started Securing Enterprise Applications
42. Java EE Security: Advanced Topics
Part VIII Java EE Supporting Technologies
43. Introduction to Java EE Supporting Technologies
45. Resources and Resource Adapters
46. The Resource Adapter Example
47. Java Message Service Concepts
48. Java Message Service Examples
49. Bean Validation: Advanced Topics
50. Using Java EE Interceptors
51. Duke's Bookstore Case Study Example
52. Duke's Tutoring Case Study Example
53. Duke's Forest Case Study Example
The decorators example, which is yet another variation on the encoder example, shows how to use a decorator to implement additional business logic for a bean. Instead of having the user choose between two alternative implementations of an interface at deployment time or runtime, a decorator adds some additional logic to a single implementation of the interface.
The example includes an interface, an implementation of it, a decorator, an interceptor, a managed bean, a Facelets page, and configuration files.
Components of the decorators Example
The decorators example is very similar to the encoder example described inThe encoder Example: Using Alternatives. Instead of providing two implementations of the Coder interface, however, this example provides only the CoderImpl class. The decorator class, CoderDecorator, rather than simply return the coded string, displays the input and output strings’ values and length.
The CoderDecorator class, like CoderImpl, implements the business method of the Coder interface,codeString:
@Decorator public abstract class CoderDecorator implements Coder {
@Inject
@Delegate
@Any
Coder coder;
@Override
public String codeString(String s, int tval) {
int len = s.length();
return "\"" + s + "\" becomes " + "\"" + coder.codeString(s, tval)
+ "\", " + len + " characters in length";
}
}
The decorator’s codeString method calls the delegate object’s codeString method to perform the actual encoding.
The decorators example includes the Logged interceptor binding and LoggedInterceptor class from thebillpayment example. For this example, the interceptor is set on the CoderBean.encodeString method and the CoderImpl.codeString method. The interceptor code is unchanged; interceptors are usually reusable for different applications.
Except for the interceptor annotations, the CoderBean and CoderImpl classes are identical to the versions in the encoder example.
The beans.xml file specifies both the decorator and the interceptor:
<decorators>
<class>decorators.CoderDecorator</class>
</decorators>
<interceptors>
<class>decorators.LoggedInterceptor</class>
</interceptors>
Running the decorators Example
You can use either NetBeans IDE or Ant to build, package, deploy, and run the decorators application.
To Build, Package, and Deploy the decorators Example Using NetBeans IDE
- From the File menu, choose Open Project.
- In the Open Project dialog, navigate to:
tut-install/examples/cdi/ - Select the decorators folder.
- Select the Open as Main Project check box.
- Click Open Project.
- In the Projects tab, right-click the decorators project and select Deploy.
To Build, Package, and Deploy the decorators Example Using Ant
- In a terminal window, go to:
tut-install/examples/cdi/decorators/ - Type the following command:
ant
This command calls the default target, which builds and packages the application into a WAR file, decorators.war, located in the dist directory. - Type the following command:
ant deploy
To Run the decorators Example
- In a web browser, type the following URL:
http://localhost:8080/decorators
The Decorated String Encoder page opens. - Type a string and the number of letters to shift by, then click Encode.
The output from the decorator method appears in blue on the Result line. For example, if you typed Java and 4, you would see the following:
"Java" becomes "Neze", 4 characters in length - Examine the server log output.
In NetBeans IDE, the output is visible in the GlassFish Server 3+ output window. Otherwise, view domain-dir/logs/server.log.
The output from the interceptors appears:
INFO: Entering method: encodeString in class decorators.CoderBean
INFO: Entering method: codeString in class decorators.CoderImpl
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices