Java EE 8 MVC: Working With Form Parameters

DZone 's Guide to

Java EE 8 MVC: Working With Form Parameters

How to use form parameters in applications that require a user submit a web form.

· Java Zone ·
Free Resource

In the previous two posts we saw how to work with query and path parameters in the upcoming Java EE MVC framework. This post focuses on form parameters.

When you submit a web form using a post request, the form values are send as part of the request body. The media type (or content type) defines the format that is used to store the values inside the request body. Web forms usually use the media type application/x-www-form-urlencoded. Parameters that are send using this media type can be accessed using the @FormParam annotation.

Using Form Parameters

Assume we have the following simple HTML form, containing two text input fields and a select menu:

<form action="submit" method="post">
  <input type="text" name="id" />

  <input type="text" name="name" />

  <select name="role">
    <option value="admin">Admin</option>
    <option value="reporter">Reporter</option>
    <option value="accountant">Accountant</option>

  <input type="submit" />

To process the form values, we can use the following controller method:

public enum Role {
 admin, reporter, accountant
public class FormParamsController {

 public String submitParams(
  @FormParam("id") long id,
  @FormParam("name") String name,
  @FormParam("role") Role role) {


With @FormParam form parameters can automatically be mapped to method arguments. Form parameters use the same type conversion rules as query or path parameters (described here). This makes it easy to convert form parameters to primitives, enums or objects.

Using @FormParam On Fields and Methods

Like other parameter annotations @FormParam can be used to annotate fields and methods.

For example:

public class FormParamsFieldController {

 private long id;

 private Role role;

 private String name;

 public void setNameField(String name) {
  this.name = name;

 public String submitParams() {
  // use id, role, name

In this example the submitted form parameters are mapped to fields (id, role) and a setter (setNameField()). Inside submitParams() we can then simply access the fields to obtain the form parameters.

Quick Summary

Parameters submitted via HTML forms can be obtained with @FormParam. Like @QueryParam and @PathParam it is possible to annotate fields, methods and method parameters with @FormParam.

You can find the source code for all examples on GitHub.

java, java 8, java mvc

Published at DZone with permission of Michael Scharhag , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}