DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > MVC 1.0 in Java EE 8 - Handling Form Submits

MVC 1.0 in Java EE 8 - Handling Form Submits

Because of MVC's integration with JAX-RS it is pretty simple to handle form submits.

Bennet Schulz user avatar by
Bennet Schulz
·
Nov. 23, 15 · Java Zone · Tutorial
Like (5)
Save
Tweet
7.00K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Form (document) Java EE Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Introduction to Word Vectors
  • How to Get GDPR and Customer Communications Right
  • What I Miss in Java, the Perspective of a Kotlin Developer
  • Python 101: Equality vs. Identity

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo