tina tina - 1 year ago 96
Java Question

Building a Spring Boot project using Maven

I'm studying in order to create a web application using Spring Boot and JDBC (without JPA); I'm having several problems doing it.
I don't understand how to correctly create the controller, and how to make it return the .jsp pages that I want.
I searched lots and lots solutions, but just can't find the right one.
This is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


<description>Demo project for Spring Boot</description>

<relativePath /> <!-- lookup parent from repository -->







My application.properties:

> spring.h2.console.enabled=true

My ServetInizializer:

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(TesieasyApplication.class);


My entity:

import java.util.Date;
import java.text.SimpleDateFormat;

public class Journal {

private Long id;
private String title;
private Date created;
private String summary;
private SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

/** costruttori**/
public Journal() {}

public Journal(Long id, String title, String summary, Date date){
this.id = id;
this.title = title;
this.summary = summary;
this.created = date;

/** getters and setters omitted **/

My service:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import name of my package-.entity.Journal;

public class JournalService {
private static final Logger log = LoggerFactory.getLogger(JournalService.class);

JdbcTemplate jdbcTemplate;

public void insertData() {
log.info("> Table creation");
jdbcTemplate.execute("DROP TABLE JOURNAL IF EXISTS");
.execute("-cut for brevity-");
log.info("> Done.");

public List<Journal> findAll() {
List<Journal> entries = new ArrayList<>();
new Object[] {}, (rs, row) -> new Journal(rs.getLong("id"), rs.getString("title"),
rs.getString("summary"), new Date(rs.getTimestamp("created").getTime())))
.forEach(entry -> entries.add(entry));
return entries;


public int countEntries() {
int rowCount = this.jdbcTemplate.queryForObject("SELECT count(*) FROM JOURNAL", Integer.class);
return rowCount;


public Journal insertJournal() {
Journal journal = null;
-cut for brevity-");
return journal;


One of my .jsp pages:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>A title</title></head>
Hello! JSP is working fine!

And finally my controller:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import *my package name.entity.Journal;
import *my package name.service.JournalService;

public class HelloController {

private JournalService journalService;

public String home() {
return "home";

public String greeting() {
return "greeting";

public String index() {
List<Journal> all;
all = journalService.findAll();
return all.toString();

public int countElements() {
int count;
count = journalService.countEntries();
return count;

public Journal insertElements() {
Journal insert;
insert = journalService.insertJournal();
return insert;


What I understood:

  • I don't need a web.xml file

  • How to connect the service with the controller (even if my findAll method return my data into {}, and I have to understand how to write it properly)

What I don't understand:

  • Why it's not linking correctly to my JSP pages - what do I need?

  • Do I need a Model and View?

I hope you can give me some advice and/or some examples right for my scope.
Thanks in advance and best regards!

Answer Source


Step1: change the @RestController to @Controller

Step2: for URL that you want to return a view (e.g. greeting.jsp)

public String greeting() {
    return "greeting";

Step3: for URL that you want to return a JSON object, no view (perhaps an AJAX call)

public List<Journal> index() {
    List<Journal> all = journalService.findAll()
    return all;

OR this

public Model index(Model model) {
    List<Journal> all =journalService.findAll();
    model.addAttribute("journalList", all);
    return model;

Step4: for URL that you want the view and the model (e.g. a java object)

ref Rob Baily sample code

public String index(Model model) {
    List<Journal> all;
    all = journalService.findAll();
    model.addAttribute("journalList", all);
    return "listall";

and you can access the object in your listall.jsp by using ${journalList}

Explanation: When you use @RestController on your class, it is equivalent to adding @ResponseBody for every method in your class. Thus, it will always return an object.toString() on the page and not linking to your JSP.

This code uses Spring 4’s new @RestController annotation, which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for @Controller and @ResponseBody rolled together. ref: https://spring.io/guides/gs/rest-service/

jsp sample reference: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download