The encoder Example: Using Alternatives (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 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
The decorators Example: Decorating a Bean
Components of the decorators Example
Running the decorators Example
To Build, Package, and Deploy the decorators Example Using NetBeans IDE
To Build, Package, and Deploy the decorators Example Using Ant
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 encoder example shows how to use alternatives to choose between two beans at deployment time, as described in Using Alternatives in CDI Applications. The example includes an interface and two implementations of it, a managed bean, a Facelets page, and configuration files.
The Coder Interface and Implementations
The Coder interface contains just one method, codeString, that takes two arguments: a string, and an integer value that specifies how the letters in the string should be transposed.
public interface Coder {
public String codeString(String s, int tval);
}
The interface has two implementation classes, CoderImpl and TestCoderImpl. The implementation ofcodeString in CoderImpl shifts the string argument forward in the alphabet by the number of letters specified in the second argument; any characters that are not letters are left unchanged. (This simple shift code is known as a Caesar cipher, for Julius Caesar, who reportedly used it to communicate with his generals.) The implementation in TestCoderImpl merely displays the values of the arguments. The TestCoderImplimplementation is annotated @Alternative:
import javax.enterprise.inject.Alternative;
@Alternative public class TestCoderImpl implements Coder {
public String codeString(String s, int tval) {
return ("input string is " + s + ", shift value is " + tval);
}
}
The beans.xml file for the encoder example contains an alternatives element for theTestCoderImpl class, but by default the element is commented out:
<beans ... >
This means that by default, the TestCoderImpl class, annotated @Alternative, will not be used. Instead, the CoderImpl class will be used.
The encoder Facelets Page and Managed Bean
The simple Facelets page for the encoder example, index.xhtml, asks the user to type the string and integer values and passes them to the managed bean, CoderBean, as coderBean.inputString and coderBean.transVal:
String EncoderString Encoder
Type a string and an integer, then click Encode.
Depending on which alternative is enabled, the coder bean will either display the argument values or return a string that shifts the letters in the original string by the value you specify. The value must be between 0 and 26.
...
When the user clicks the Encode button, the page invokes the managed bean’sencodeString method and displays the result, coderBean.codedString, in blue. The page also has a Reset button that clears the fields.
The managed bean, CoderBean, is a @RequestScoped bean that declares its input and output properties. The transVal property has three Bean Validation constraints that enforce limits on the integer value, so that if the user types an invalid value, a default error message appears on the Facelets page. The bean also injects an instance of the Coder interface:
@Named @RequestScoped public class CoderBean {
private String inputString;
private String codedString;
@Max(26)
@Min(0)
@NotNull
private int transVal;
@Inject
Coder coder;
...
In addition to simple getter and setter methods for the three properties, the bean defines the encodeString action method called by the Facelets page. This method sets the codedString property to the value returned by a call to thecodeString method of the Coder implementation:
public void encodeString() {
setCodedString(coder.codeString(inputString, transVal));
}
Finally, the bean defines the reset method to empty the fields of the Facelets page:
public void reset() {
setInputString("");
setTransVal(0);
}
Running the encoder Example
You can use either NetBeans IDE or Ant to build, package, deploy, and run the encoder application.
To Build, Package, and Deploy the encoder Example Using NetBeans IDE
- From the File menu, choose Open Project.
- In the Open Project dialog, navigate to:
tut-install/examples/cdi/ - Select the encoder folder.
- Select the Open as Main Project check box.
- Click Open Project.
- In the Projects tab, right-click the encoder project and select Deploy.
To Run the encoder Example Using NetBeans IDE
- In a web browser, type the following URL:
http://localhost:8080/encoder
The String Encoder page opens. - Type a string and the number of letters to shift by, then click Encode.
The encoded string appears in blue on the Result line. For example, if you type Java and 4, the result is Neze. - Now, edit the beans.xml file to enable the alternative implementation of Coder.
- In the Projects tab, under the encoder project, expand the Web Pages node, then the WEB-INF node.
- Double-click the beans.xml file to open it.
- Remove the comment characters that surround the alternatives element, so that it looks like this: encoder.TestCoderImpl
- Save the file.
- Right-click the encoder project and select Deploy.
- In the web browser, retype the URL to show the String Encoder page for the redeployed project:
http://localhost:8080/encoder/ - Type a string and the number of letters to shift by, then click Encode.
This time, the Result line displays your arguments. For example, if you typeJava and 4, the result is:
Result: input string is Java, shift value is 4
To Build, Package, and Deploy the encoder Example Using Ant
- In a terminal window, go to:
tut-install/examples/cdi/encoder/ - Type the following command:
ant
This command calls the default target, which builds and packages the application into a WAR file, encoder.war, located in the dist directory. - Type the following command:
ant deploy
To Run the encoder Example Using Ant
- In a web browser, type the following URL:
http://localhost:8080/encoder/
The String Encoder page opens. - Type a string and the number of letters to shift by, then click Encode.
The encoded string appears in blue on the Result line. For example, if you type Java and 4, the result is Neze. - Now, edit the beans.xml file to enable the alternative implementation of Coder.
- In a text editor, open the following file:
tut-install/examples/cdi/encoder/web/WEB-INF/beans.xml - Remove the comment characters that surround the alternatives element, so that it looks like this: encoder.TestCoderImpl
- Save and close the file.
- In a text editor, open the following file:
- Type the following commands:
ant undeploy
ant
ant deploy - In the web browser, retype the URL to show the String Encoder page for the redeployed project:
http://localhost:8080/encoder - Type a string and the number of letters to shift by, then click Encode.
This time, the Result line displays your arguments. For example, if you typeJava and 4, the result is:
Result: input string is Java, shift value is 4
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices