DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Java
  4. 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 · Tutorial
Like (5)
Save
Tweet
Share
7.19K 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

  • Create a REST API in C# Using ChatGPT
  • When to Choose Redpanda Instead of Apache Kafka
  • 10 Best Ways to Level Up as a Developer
  • Secure APIs: Best Practices and Measures

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: