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
The @MaxNumber and @Random Qualifier Interfaces
The UserNumberBean Managed Bean
Running the guessnumber Example
To Build, Package, and Deploy the guessnumber Example Using NetBeans IDE
To Build, Package, and Deploy the guessnumber Example Using Ant
To Run the guessnumber Example
30. Contexts and Dependency Injection for the Java EE Platform: Advanced Topics
31. Running the Advanced Contexts and Dependency Injection Examples
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 simplegreeting example illustrates some of the most basic features of CDI: scopes, qualifiers, bean injection, and accessing a managed bean in a JavaServer Faces application. When you run the example, you click a button that presents either a formal or an informal greeting, depending on how you edited one of the classes. The example includes four source files, a Facelets page and template, and configuration files.
The simplegreeting Source FilesThe four source files for the simplegreeting example are
The default Greeting class, shown in Beans as Injectable Objects
The @Informal qualifier interface definition and the InformalGreeting class that implements the interface, both shown in Using Qualifiers
The Printer managed bean class, which injects one of the two interfaces, shown in full in Adding Setter and Getter Methods
The source files are located in the tut-install/examples/cdi/simplegreeting/src/java/greetings/ directory.
The Facelets Template and PageTo use the managed bean in a simple Facelets application, you can use a very simple template file and index.xhtml page. The template page, template.xhtml, looks like this:
<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets"> <h:head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <h:outputStylesheet library="css" name="default.css"/> <title> <ui:insert name="title">Default Title</ui:insert> </title> </h:head> <body> <div id="container"> <div id="header"> <h2><ui:insert name="head">Head</ui:insert></h2> </div> <div id="space"> <p></p> </div> <div id="content"> <ui:insert name="content"/> </div> </div> </body> </html>
To create the Facelets page, you can redefine the title and head, then add a small form to the content:
<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"> <ui:composition template="/template.xhtml"> <ui:define name="title">Simple Greeting</ui:define> <ui:define name="head">Simple Greeting</ui:define> <ui:define name="content"> <h:form id="greetme"> <p><h:outputLabel value="Enter your name: " for="name"/> <h:inputText id="name" value="#{printer.name}"/></p> <p><h:commandButton value="Say Hello" action="#{printer.createSalutation}"/></p> <p><h:outputText value="#{printer.salutation}"/> </p> </h:form> </ui:define> </ui:composition> </html>
The form asks the user to type a name. The button is labeled Say Hello, and the action defined for it is to call the createSalutation method of the Printer managed bean. This method in turn calls the greet method of the defined Greeting class.
The output text for the form is the value of the greeting returned by the setter method. Depending on whether the default or the @Informal version of the greeting is injected, this is one of the following, where name is the name typed by the user:
Hello, name.
Hi, name!
The Facelets page and template are located in the tut-install/examples/cdi/simplegreeting/web/ directory.
The simple CSS file that is used by the Facelets page is in the following location:
tut-install/examples/cdi/simplegreeting/web/resources/css/default.cssConfiguration Files
You must create an empty beans.xml file to indicate to GlassFish Server that your application is a CDI application. This file can have content in some situations, but not in simple applications like this one.
Your application also needs the basic web application deployment descriptors web.xml and glassfish-web.xml. These configuration files are located in the tut-install/examples/cdi/simplegreeting/web/WEB-INF/ directory.
Running the simplegreeting ExampleYou can use either NetBeans IDE or Ant to build, package, deploy, and run the simplegreeting application.
To Build, Package, and Deploy the simplegreeting Example Using NetBeans IDEThis procedure builds the application into the tut-install/examples/cdi/simplegreeting/build/web/ directory. The contents of this directory are deployed to the GlassFish Server.
tut-install/examples/cdi/
@Inject //@Informal Greeting greeting;
tut-install/examples/cdi/simplegreeting/
ant
This command calls the default target, which builds and packages the application into a WAR file, simplegreeting.war, located in the dist directory.
ant deploy
Typing this command deploys simplegreeting.war to the GlassFish Server.
http://localhost:8080/simplegreeting
The Simple Greeting page opens.
For example, suppose that you type Duke.
If you did not modify the Printer.java file, the following text string appears below the button:
Hi, Duke!
If you commented out the @Informal annotation in the Printer.java file, the following text string appears below the button:
Hello, Duke.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4