Over a million developers have joined DZone.

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

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

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">
  <label>ID:</label>
  <input type="text" name="id" />

  <label>Name:</label>
  <input type="text" name="name" />

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

  <input type="submit" />
</form>

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

public enum Role {
 admin, reporter, accountant
}
@Controller
@Path("form-params")
public class FormParamsController {

 @POST
 @Path("submit")
 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:

@Controller
@Path("form-params-fields")
public class FormParamsFieldController {

 @FormParam("id")
 private long id;

 @FormParam("role")
 private Role role;

 private String name;

 @FormParam("name")
 public void setNameField(String name) {
  this.name = name;
 }

 @POST
 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.

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:
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.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}