Java EE 7 SDK

Samples Main Page

# The Bean Validation CDI Sample Application

This sample application demonstrates how to inject beans with validation constraints.

## Description

This sample application injects a bean in a servlet with annotations for validation constraints. The constraints are validated at run time using the input/output provided.

    @Named(value="Bean1")
    public class Bean1 {

        public @NotNull String sayHello(@NotNull @Size(max=5) String arg) {
            return "Hello, " + arg + " !";
        }
    }

The example is divided into 3 scenarios:

- Scenario 1 runs successfully: the value of `arg` is `test`, which passes both constraints placed on the input.
- Scenario 2 causes a failure: the value of `arg` is `null`, which violates the `@NotNull` constraint.
- Scenario 3 causes a failure: the value of `arg` is `testxyz`, which violates the `@Size(max=5)` constraint.

User can click on the submit button in provided index.html or invoke servlet at `http://<host>:<port>/<context-root>/ValidationServlet` to run these three scenarios and get the results as HTML output.

## Building, Deploying, and Running the Application

Perform the following steps to build, deploy, and run the application:

1. Set up your build environment and configure the application server with which the build system has to work by following the common build instructions.
2. `app_dir` is the sample application base directory: `samples_install_dir/javaee7/cdi/bean-validation`.
3. Change directory to `app_dir.`
4. Build, deploy, and run the sample application using the `run` outcome.

   `mvn clean verify cargo:run`

5. Use the `clean` outcome to undeploy the sample application and to remove the temporary directories such as `build` and `dist`.

   `mvn clean`

## Troubleshooting

If you have problems when running the application, refer to the troubleshooting document.

Copyright © 1997-2013 Oracle and/or its affiliates. All rights reserved.