Java and PostgreSQL Web Application Part 7

Introduction

Welcome to Part 7 of the multiple-series that explains how to build a Java PostgreSQL web application from scratch with ECLIPSE, Spring Data, Tomcat, and other databases. Part 6 discussed how to use the handler to link information in a database and then display table records on the home page of your web application. Let’s proceed with using handlers to add different features to your web application.

Prerequisites

Coding the Java and PostgreSQL Web Application’s Feature

Here in Part 7, you’ll continue to learn how to use handlers to add features. Specifically, you’ll find out how to create and insert records into the database for your web application.

Creating New Record in Java and PostgreSQL Web Application

Enter the controller class CarsController to make a view and a couple of handlers. They’ handlers will be used to generate a new database table record.

Use this code here:

@RequestMapping("/new")
public String createCustomer(Map<String, Object> model) {

model.put("cars", new Cars());
return "create_car";
}

Based on the code above, the _createcar name for your application’s view will be returned.

You also added the object Cars that is empty.

Also, we put the empty Cars object because it’s new.

  • Now that you have a view file, create a file JSP to go with it. Call it “create_car.jsp”. Use this code to accomplish that:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%> <%@ taglib prefix="form"
uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>



<title>New Car</title>
</head>

<div class="row" id="formEntry">
<div
class="col-lg-4 col-md-4 col-sm-4 container justify-content-center"
id="divi"
>
<h2>New Car Details</h2>
<form:form action="save" method="post" modelAttribute="cars">
<div class="mb-3 input-group">
<div class="input-group-prepend">
<span class="input-group-text">Car Name:</span>
</div>
<form:input
path="carname"
type="text"
class="form-control col-sm-10"
placeholder="Enter Car Name"
aria-label="Enter Car Name"
aria-describedby="basic-addon2"
/>
</div>
<div class="mb-3 input-group">
<div class="input-group-prepend">
<span class="input-group-text">Brand Name:</span>
</div>
<form:input
path="brand"
type="text"
class="form-control col-sm-10"
placeholder="Enter Brand"
aria-label="Enter Brand"
aria-describedby="basic-addon2"
/>
</div>
<div class="mb-3 input-group">
<div class="input-group-prepend">
<span class="input-group-text">Review:</span>
</div>
<form:input
path="review"
type="text"
class="form-control col-sm-10"
placeholder="Enter Review"
aria-label="Enter Review"
aria-describedby="basic-addon2"
/>
</div>

<div class="form-group">


</div>
</form:form>
</div>
</div>
</body>
</html>
  • You can handle the values in forms automatically with the <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> Spring framework tag for your forms. Use the code below to get that done:
<form:form action="save" method="post" modelAttribute="cars">
  <div class="mb-3 input-group">
    <div class="input-group-prepend">
      <span class="input-group-text">Car Name:</span>
    </div>
    <form:input
     path="carname"
     type="text"
     class="form-control col-sm-10"
     placeholder="Enter Car Name"
     aria-label="Enter Car Name"
     aria-describedby="basic-addon2"
   />
  </div>
  <div class="mb-3 input-group">
    <div class="input-group-prepend">
      <span class="input-group-text">Brand Name:</span>
    </div>
    <form:input
     path="brand"
     type="text"
     class="form-control col-sm-10"
     placeholder="Enter Brand"
     aria-label="Enter Brand"
     aria-describedby="basic-addon2"
   />
  </div>
  <div class="mb-3 input-group">
    <div class="input-group-prepend">
      <span class="input-group-text">Review:</span>
    </div>
    <form:input
     path="remarks"
     type="text"
     class="form-control col-sm-10"
     placeholder="Enter Review"
     aria-label="Enter Review"
     aria-describedby="basic-addon2"
   />
  </div>

  <div class="form-group"> </div>
</form:form>

Here’s some more information about what you just achieved with the above code:

  • The object now has the values saved to it since the ModelAttribute specifically states the name of the attribute that is to be saved.

  • You’re all prepared to finish adding the functionality to create a new record because “save” is the setting for the action of the form. A handler must be created for it now. Earlier in today’s lesson, you already created one handler for your application, so this would be the second one.

  • Access the controller class CarsController again, and add this code:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveCar(@ModelAttribute("cars") Cars cars) {
service.save(cars);

return "redirect:/";
}
  • The above code sets the annotation @RequestMapping like this @RequestMapping(value = "/save", method = RequestMethod.POST) because the form has the value.

  • In addition, don’t forget that the “cars” annotation for @ModelAttribute has to be specified in your form.

  • Next, the Spring model-view-controller (MVC) is able to read the object values of Cars cars instantly when the object Cars cars is indicated.

NOTE: A variety of methods have been specified in Java and PostgreSQL web Application PART 5.

  • Here’s a method that was reviewed in Part 5 of this multiple-series tutorial:
public void save(Cars cars) {
carsRepo.save(cars);
}
  • Use this code: service.save(cars); to make a function call in the controller class.

  • Save the database record.

  • Input the code: return "redirect:/"; to get back to your index.jsp home page.

  • Save it.

  • Execute the application to test it out.

  • Your result should resemble this one below:

Image shows the form for adding new car details

  • To carry out the database function INSERT, and then redirect immediately to the home page and show the updated records, press the button Save.

  • The result should be something like this:

image shows updated records in index.jsp

  • Confirm the success of the action with the operation SELECT while in a PostgreSQL shell.

  • Your result should resemble this:

cars=# SELECT * FROM cars;
id | brand | carname | remarks
----+--------+-----------+------------------------------
1 | toyota | fortuner | big engine AND nice body
2 | toyota | wigo | small AND cute
3 | suzuki | ertiga | small but economical
4 | honda | city | elegant
5 | Ford | Eco Sport | Classy AND great performance

Conclusion

This completes Part 7 of PostgreSQL web application that explains how to create a web application from scratch with Java using ECLIPSE IDE, Spring Data JPA, Spring MVC, Tomcat and PostgreSQL as the database. In this tutorial, you learned how to configure the class controller to create a new record and insert it into a PostgreSQL database. You used handlers to add this feature. As a refresher, refer to the Prerequisites section for this multiple-series tutorial, which includes links to Parts 1 through 6. Also, review this lesson again any time you want, and then proceed to Part 8 of Java and PostgreSQL Web Application.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.