Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.
See all our reports

Submitting Forms with Spring and JRebel

chuck_norris_form

Form submission is a pretty standard thing that every Java dev will have to deal with at some point, and having to do a full build/redeploy cycle to see the changes on your form can be frustrating. Follow along with this guide and I’ll show you how you can extend your Spring MVC form application with JRebel enabled and view code changes instantly.

FOLLOW ALONG WITH JREBEL NOW

For this guide, we’re going to start with the completed version of the spring.io guide for handling form submissions with Spring. The application uses Boot and the MVC bits of Spring. The app has a single controller with one request mapping and two templated views using Thymeleaf (my new favorite view template). Whether you’re using Spring or any of the other 80+ frameworks that JRebel supports, everything should work just a treat.

First, let’s start our app and play around with it. Open up a browser tab and go to http://localhost:8080/greeting. We can verify that the form works and the values are being submitted through the controller and then rendered with Thymeleaf on the response page. Now, because we have JRebel, we can just leave our application running throughout the rest of this guide.

spring_forms_original

Currently our form has two fields, one for Id and one for content, let’s get a little more personal and ask for our submitter’s name as well. Let’s modify our Greeting class by adding a new private String field name and generate its getter and setter methods.

private String name;
...
public String getName() { return name; }
public void setName(String name) { this.name = name; }

Our binding should pick up the new field for the Greeting object, now we need to modify our views, let’s start with the greeting.html.

We need to add a new row to our form and then make sure to map it to the new name field in our Greeting. This should be quick:

<p>Id: <input type="text" th:field="*{id}" /></p>
<p>Name: <input type="text" th:field="*{name}" /></p>
<p>Message: <input type="text" th:field="*{content}" /></p>

spring_forms_updated
Simple right? Templates make this kind of thing much easier than it used to be. Our form view is ready, since we’re using JRebel, we can just compile the changed classes, hit refresh in our browser, and see that our form is updated.

We also can see in Spring Boot’s console that JRebel reloaded the class that was changed:

2014-12-11 15:00:09 JRebel: Reloading class 'hello.Greeting'.

But, if we use the form now, we see that we have to update our result view as well.
spring_forms_original_results
Simple enough, in our result.html file, we’ll reference the new field like the existing ones:

<p th:text="'id: ' + ${greeting.id}" />
<p th:text="'name: ' + ${greeting.name}" />
<p th:text="'content: ' + ${greeting.content}" />

Once again, save/compile the changed files, and hit refresh in your browser, and this time, we have a more personalized greeting page:
spring_forms_updated_results
If you followed along, you should now have a more friendly greeting form. To recap, we modified our Thymeleaf views and modified our entity class and didn’t have to rebuild our entire app or even restart it thanks to JRebel.

If you have any questions you can find me on twitter at @akoblentz or leave a comment below!


SET UP JREBEL IN THE NEXT 10 MIN!