This tutorial will demonstrate how to use JSTL tag library in combination with Spring Boot MVC applications.

First off, in order to use JSTL tag library in combination with Spring Boot MVC applications you need to include the following dependency set in your application:

  <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

Now, in order to configure JSP and JSTL usage in your Spring Boot application, add the following configuration class in your web application:

package com.example.cruddemo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan
public class Config implements WebMvcConfigurer {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
    }

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}

Within this configuration class, we have configured:

  • A default View Controller for the index.html page which captures all requests for the root ("/") context
  • The org.springframework.web.servlet.view.JstlView Class as standard View class
  • The project path where JSP pages are searched in the webapp/WEB-INF/pages folder
  • The suffix for our views to be ".jsp"

With that in place, just add JSP pages with JSTL tags in the right project path, as in this example:

src
├── main
│   ├── java
│   │   └── com
│   │       └── example
│   │           └── cruddemo
│   │               ├── Config.java
│   │               ├── controllers
│   │               │   └── CustomerController.java
│   │               ├── DemoApplication.java
│   │               ├── entities
│   │               │   └── Customer.java
│   │               └── repositories
│   │                   └── CustomerRepository.java
│   ├── resources
│   │   ├── application.properties
│   │   ├── data.sql
│   └── webapp
│       ├── index.html
│       ├── stylesheet.css
│       └── WEB-INF
│           └── pages
│               └── index.jsp

Now you can call your JSP pages either directly from the root Web context (ex. http://localhost:8080/index.jsp), or you can also reference them from a Controller:

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView listAction() {

        List<String> data = new ArrayList<>();
        data.add("Hello!");

        ModelAndView mv = new ModelAndView();

        mv.setViewName("index");
        mv.addObject("data", data);

        return mv;

    }

And finally, here is the index.jsp page which iterates over a list of Customer objects, passed as a Model from the Controller:

<%@ page isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>
<body>

  <h2>How to iterate list on JSP in Spring MVC</h2>

    <div align="left">
        <table border="1" cellpadding="5">
            <caption><h2>List of Customers</h2></caption>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Surname</th>
            </tr>
            <c:forEach var="user" items="${listCustomers}">
                <tr>
                    <td><c:out value="${user.id}" /></td>
                    <td><c:out value="${user.name}" /></td>
                    <td><c:out value="${user.surname}" /></td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>

Here is your application in action:

spring boot jstl   tutorial spring boot jstl tutorial

Source code for this tutorial: https://github.com/fmarchioni/masterspringboot/tree/master/mvc/mvc-basic

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials