InjectForm Interceptor

This interceptor validates the input values and sets the generated form object in the request scope.

This interceptor is enabled by configuring InjectForm for the business action method.

Module list

<dependency>
  <groupId>com.nablarch.framework</groupId>
  <artifactId>nablarch-fw-web</artifactId>
</dependency>

<!-- Only when BeanValidation is used for input value check -->
<dependency>
  <groupId>com.nablarch.framework</groupId>
  <artifactId>nablarch-core-validation-ee</artifactId>
</dependency>

<!-- Only when NablarchValidation is used for input value check -->
<dependency>
  <groupId>com.nablarch.framework</groupId>
  <artifactId>nablarch-core-validation</artifactId>
</dependency>

Using InjectForm

Configure the InjectForm annotation for the method that processes the business action request.

An implementation example is shown below.

HTML example of input screen
<!-- Validation is not covered-->
<input name="flag" type="hidden" />

<!-- Validation is covered -->
<input name="form.userId" type="text" />
<input name="form.password" type="password" />
Example of business action

In this example, validation is executed for the request parameters starting from the form sent from the screen. If there are no errors during validation, the object of the class specified by InjectForm#form is stored in the request scope.

Variable name used when storing the validated form in the request scope is specified in InjectForm#name. If the variable name is not specified, the form is stored with the variable name form.

When a business action is executed, an object can always be acquired from the request scope.

@InjectForm(form = UserForm.class, prefix = "form", validate = "register")
@OnError(type = ApplicationException.class, path = "forward://registerForm.jsp")
public HttpResponse handle(HttpRequest req, ExecutionContext ctx) {

  // Obtain the validated form from the request scope.
  UserForm form = ctx.getRequestScopedVar("form");

  // Perform the business process based on the form.
}

Tip

If Bean Validation is used for validation, it can be configured such that the objects can be fetched from the request scope even during validation errors. For details, see Request parameters are to be obtained from the request scope even when there is a validation error.

Specify the transition destination when a validation error occurs

The transition destination screen when a validation error occurs is configured using the OnError annotation.

Configure for OnError for the business action method to which InjectForm that has been configured for the business action method. Note that if OnError is not configured, validation error is handled as a system error.

To acquire data for display on the transition destination screen when a validation error occurs, see Acquire the data to be displayed on the transition destination screen when an error occurs.

Specify Groups of Bean Validation

When using Bean Validation for validation, groups can be specified in InjectForm#validationGroup .

An implementation example is shown below.

// Among the validation rules set within the UserForm class,
// only the rules belonging to the Create group are used for validation.
@InjectForm(form = UserForm.class, prefix = "form", validationGroup = Create.class)
public HttpResponse handle(HttpRequest req, ExecutionContext ctx) {

  // Obtain the validated form from the request scope.
  UserForm form = ctx.getRequestScopedVar("form");

  // Perform the business process based on the form.
}