MVC 1.0 in Java EE 8 - Handling Form Submits

3 min read
After introducing the core features of MVC 1.0 like Controllers, Models and how to get started with MVC this post of my MVC 1.0 in Java EE 8 series is about handling Form Submits.

In MVC 1.0 there are two ways to post form input to controllers.
The first one is to use the JAX-RS @FormParam annotation and the second one is to aggregate such parameters by using the @BeanParam annotation. This blogpost starts with a sample html form and its presentation in a browser. After that is shows how to implement form inputs with MVC.


The Form

This sample mini-application used in this blog post to show the differences between the FormParam and BeanParam approach is a simple form with two inputs, Firstname and Lastname and a submit button. In the browser it looks like:

The HTML/JSP which results in this inputs and submitbutton looks like:
This HTML file is used by the following two ways to handle form submits in controller methods.

FormParam

The easiest way to handle form submissions is by using the JAX-RS @FormParam annotation. The following snippet shows the html input for two textfields.
The previous html form sends a post request to the mvc/hello path which is the path of the responsible controller. The controller method which is invoked when submitting the form looks like:

This is completely the same as with plain JAX-RS. It's pretty simple and easy, but in case of multiple parameters like an address for example, this gets ugly and confusing. Aggregating related parameters solves this problem. The next section explains how to aggregate parameters by using JAX-RS @BeanParam annotation which is available since JAX-RS 2.0.

BeanParam

In case of multiple related parameters an object can be used to aggregate these parameters. In this form submit sample this object can be a Person class as method parameter instead of the firstname and lastname. The following snippet shows this aggregated Person object:











This Person class has the previous controller method parameters as fields. The previous controller method changed from multiple @FormParam parameters to a single @BeanParam one like it is visible in the following snippet:
When comparing both ways, the second one is more clear and readable and should be used whereever parameters can be aggregated to an object.
Nevertheless this parameter aggregation results in lots of annotations within the object class when adding validations to controller methods. Validation annotations like @NotNull, @NotEmpty and all the other annotations have to be added to the fields as well.

Note: It's also possible to use different @BeanParam Objects as method parameters.The implementation of multiple @BeanParam objects is the same as with a single one.

Conclusion

Because of MVC's integration with JAX-RS it is pretty simple to handle form submits. MVC 1.0 uses the well known JAX-RS @FormParam and @BeanParam annotations to handle form parameters in a controller and by using the @BeamParam it is possible to aggregate multiple parameters into objects.


Bye,
Bennet


Be Social, Share!