Document Information

Preface

Part I Introduction

1.  Overview

2.  Using the Tutorial Examples

Part II The Web Tier

3.  Getting Started with Web Applications

4.  JavaServer Faces Technology

5.  Introduction to Facelets

6.  Expression Language

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

Attributes of a Composite Component

Invoking a Managed Bean

Validating Composite Component Values

13.  Creating Custom UI Components and Other Custom Objects

14.  Configuring JavaServer Faces Applications

15.  Java Servlet Technology

16.  Uploading Files with Java Servlet Technology

17.  Internationalizing and Localizing Web Applications

Part III Web Services

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

Part IV Enterprise Beans

22.  Enterprise Beans

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

Part VI Persistence

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

Part VII Security

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

44.  Transactions

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

Part IX Case Studies

51.  Duke's Bookstore Case Study Example

52.  Duke's Tutoring Case Study Example

53.  Duke's Forest Case Study Example

Index

 

The compositecomponentlogin Example Application

The compositecomponentlogin application creates a composite component that accepts a user name and a password. The component interacts with a managed bean. The component stores the user name and password in the managed bean, retrieves the values from the bean, and displays these values on the Login page.

The compositecomponentlogin application has a composite component file, a using page, and a managed bean.

The source code for this application is in the tut-install/examples/web/compositecomponentlogin/ directory.

The Composite Component File

The composite component file is an XHTML file, /web/resources/ezcomp/LoginPanel.xhtml. It has a composite:interface section that declares the labels for the user name, password, and login button. It also declares a managed bean, which defines properties for the user name and password.

<composite:interface>
    <composite:attribute name="namePrompt" default="User Name: "/>
    <composite:attribute name="passwordPrompt" default="Password: "/>

    <composite:attribute name="loginButtonText" default="Log In"/>
    <composite:attribute name="loginAction" 
                         method-signature="java.lang.String action()"/>
    <composite:attribute name="myLoginBean"/>
    <composite:editableValueHolder name="vals" targets="form:name"/>
    <composite:editableValueHolder name="passwordVal" targets="form:password"/>
</composite:interface>

The composite component implementation accepts input values for the user name and password properties of the managed bean.

<composite:implementation>
    <h:form id="form">
        <table columns="2" role="presentation">
            <tr>
                <td><h:outputLabel for="name" 
                                   value="#{cc.attrs.namePrompt}"/></td>
                <td><h:inputText id="name" 
                                 value="#{cc.attrs.myLoginBean.name}" 
                                 required="true"/></td>
            </tr>
            <tr>
                <td><h:outputLabel for="password" 
                                   value="#{cc.attrs.passwordPrompt}"/></td>
                <td><h:inputSecret id="password" 
                                   value="#{cc.attrs.myLoginBean.password}" 
                                   required="true"/></td>
            </tr>
        </table>
        <p>
            <h:commandButton id="loginButton" 
                             value="#{cc.attrs.loginButtonText}" 
                             action="#{cc.attrs.loginAction}"/>
        </p>
    </h:form>
    ...
</composite:implementation>

The Using Page

The using page in this example application, web/index.xhtml, is an XHTML file that invokes the login composite component file along with the managed bean. It validates the user’s input.

<div id="compositecomponent">
    <ez:LoginPanel myLoginBean="#{myLoginBean}"  
                   loginAction="#{myLoginBean.login}">
         <f:validateLength maximum="10" minimum="4" for="vals" />
         <f:validateRegex pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,10})"
                          for="passwordVal"/>
    </ez:LoginPanel>
</div>

The f:validateLength tag requires the user name to have from 4 to 10 characters.

The f:validateRegex tag requires the password to have from 4 to 10 characters and to contain at least one digit, one lowercase letter, and one uppercase letter.

The Managed Bean

The managed bean, src/java/compositecomponentlogin/MyLoginBean.java, defines a method called login, which retrieves the values of the user name and password.

@ManagedBean
@RequestScoped
public class MyLoginBean {

    private String name;
    private String password;

    public MyLoginBean() {
    }

    public myloginBean(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String newValue) {
        password = newValue;
    }

    public String getName() {
        return name;
    }

    public void setName(String newValue) {
        name = newValue;
    }

    public String login() {
        if (getName().equals("javaee")) {
            String msg = "Success.  Your user name is " + getName()
                    + ", and your password is " + getPassword();
            FacesMessage facesMsg = new FacesMessage(msg, msg);
            FacesContext.getCurrentInstance().addMessage(null, facesMsg);
            return "index";
        } else {
            String msg = "Failure. Your user name is " + getName()
                    + ", and your password is " + getPassword();
            FacesMessage facesMsg = 
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
            FacesContext.getCurrentInstance().addMessage(null, facesMsg);
            return "index";
        }
    }
}

Running the compositecomponentlogin Example

You can use either NetBeans IDE or Ant to build, package, deploy, and run the compositecomponentlogin example.

To Build, Package, and Deploy the compositecomponentlogin Example Using NetBeans IDE

  1. From the File menu, choose Open Project.
  2. In the Open Project dialog, navigate to:
    tut-install/examples/web/
  3. Select the compositecomponentlogin folder.
  4. Select the Open as Main Project checkbox.
  5. Click Open Project.
  6. In the Projects tab, right-click compositecomponentlogin and select Deploy.

To Build, Package, and Deploy the compositecomponentlogin Example Using Ant

  1. In a terminal window, go to:
    tut-install/examples/web/compositecomponentlogin/
  2. Type the following command:
    ant
  3. Type the following command:
    ant deploy

To Run the compositecomponentlogin Example

  1. In a web browser, type the following URL:
    http://localhost:8080/compositecomponentlogin/

    The Login Component page opens.

  2. Type values in the User Name and Password fields, then click the Log In button.

    Because of the way the login method is coded, the login succeeds only if the user name is javaee.

    Because of the f:validateLength tag, if the user name has fewer than 4 characters or more than 10 characters, a validation error message appears.

    Because of the f:validateRegex tag, if the password has fewer than 4 characters or more than 10 characters or does not contain at least one digit, one lowercase letter, and one uppercase letter, a “Regex Pattern not matched” error message appears.