Java and PostgreSQL Web Application Part 6

Introduction

Welcome to Part 6 in the multiple-series that explains how to build a Java PostgreSQL web application from scratch with ECLIPSE, Spring Data, Tomcat, and other databases. Part 5 went over the code for the web application’s repository interface. Today, in Part 6, you’ll learn about the handler methods to link the database information and display it in your web application.

Prerequisites

Coding the Java and PostgreSQL Web Application Feature

We’ll begin this lesson by constructing the code for the features of the web application.

Retrieve All Records in Java and PostgreSQL Web Application

Start writing code that will show every record in your PostgreSQL database on your home page. To add this functionality, use a handler. Your goal is to have your web page resemble one like this.

image shows the home page of the Java and PostgreSQL web application

  • Access the CarsController.java class controller.

  • Next, add this code below:

@Autowired
private CarsService service;

@RequestMapping("/")
public ModelAndView home() {
ModelAndView modelView = new ModelAndView("index");
List<Cars> listCars = service.listAll();

modelView.addObject("listCars", listCars);
return modelView;
}

Here’s the breakdown of the code you just added:

  • @Autowired inserts the CarsService instance.

  • @RequestMapping takes care of the client request mapping to the method or handler class.

  • List<Cars> listCars = service.listAll(); calls the CarsService class service.listAll(); function.

  • List<Cars> passes as a collection the listCars after the object is returned.

  • ModelAndView both the ListCars model and the index view are provided by the controller. They are contained inside the Web MVC framework.

Implementing the Retrieve all Feature of Java and PostgreSQL Web App

The index.jsp file is the controller view because the ModelAndView("index"); is to where it was passed.

  • Therefore, this is the code for your index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>


<title>Cars Manager</title>
</head>

<div align="center" class="table-responsive">
<h1>Cars Manager</h1>
<form method="get" action="search" class="px-2 py-3">
<div class="input-group col-md-4">

<div class="input-group-append">

</div>
</div>
</form>
</div>
</body>
</html>

The HTML formatting for your home page is updated now. It’s not quite ready though. If you attempt to execute your app, the exception: "org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.chinito.carsmanager.dao.CarsRepository' available:" is going to be raised because a bean is undefined according to the BeanFactory method.

To fix it, you’ll need to make a JpaTransactionManager and LocalEntityManagerFactoryBean class reconfiguration. Here’s how:

  • Create a new ConfigJPA class under the com.chinito.carsmanager.config package, and then give it the name ConfigJPA. Use the following code:
package com.chinito.carsmanager.config;

import javax.persistence.EntityManagerFactory;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;

@Configuration
@EnableJpaRepositories (basePackages = {"com.chinito.carsmanager"})
public class JpaConfig {

@Bean
public LocalEntityManagerFactoryBean entityManagerFactory() {
LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
factoryBean.setPersistenceUnitName("CarsDB");

return factoryBean;
}

@Bean
public JpaTransactionManager transactionManager (EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);

return transactionManager;

}
}

Here’s the explanation for the code you used above:

  • Hibernate referenced the annotation @Configuration.

  • The Spring Data JPA was faciliated by the annotation @EnableJpaRepositories (basePackages = {"com.chinito.carsmanager"}).

  • The base packages for the implementation of the repository that was made by the Spring Data JPA were configured.

  • The LocalEntityManagerFactoryBean public class was configured.

  • The PersistenceUnitName was set to (“CarsDB”) which was characterized in Java and PostgreSQL web Application PART 4.

  • Finally, a JpaTransactionManager transaction manager object was constructed and then passed to the entityManagerFactory object.

  • Now you can save he index.jsp file.

  • Execute the application to test it.

  • The raised exception was resolved and the browser should show the index.jsp` like this:

image shows the index.jsp in the browser

  • Now that the exception is no more, use this HTML code to update your index.jsp file.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="t" %>
<!DOCTYPE html>
<html>
<head>


<title>Cars Manager</title>
</head>

<div align="center" class="table-responsive">
<h1>Cars Manager</h1>
<form method="get" action="search" class="px-2 py-3">
<!--
-->

<div class="input-group col-md-4">

<div class="input-group-append">

</div>
</div>
</form>

<table border="1" cellpadding="5" class="table w-auto table-striped">
<tr>
<td colspan="5">
<a href="#" class="badge badge-dark">Add New Car</a>
</td>
</tr>
<tr>
<th>ID</th>
<th>Car Name</th>
<th>Car Brand</th>
<th>Review</th>
<th>Action</th>
</tr>
<t:forEach items="${listCars}" var="cars">
<tr>
<td>${cars.id}</td>
<td>${cars.carname}</td>
<td>${cars.brand}</td>
<td>${cars.review}</td>
<td>
<a href="#" class="badge badge-dark">Edit</a>

<a href="#}" class="badge badge-dark">Delete</a>
</td>
</tr>
</t:forEach>
</table>
</div>
</body>
</html>
  • The t prefix is used along with the core JSTL tags for iteration into the ${listCars} collection. Notice the variable cars is where you’ll appoint each item like this:
<td>${cars.id}</td>
<td>${cars.carname}</td>
<td>${cars.brand}</td>
<td>${cars.review}</td
  • Save the changes.

  • Restart the server.

  • The result should look similar to this one below:

image shows the home page of the Java and PostgreSQL web application

Conclusion

This concludes Part 6 of the multiple-series that explains how to construct a Java PostgreSQL web application from scratch with ECLIPSE, Spring Data, Tomcat, and other databases. This lesson covered how to use the handler to retrieve and display database table records in your web application. Reference these steps here in Part 6 and the previous lessons, Parts 1 through 5, for a straightforward way to code your 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.