Spring Boot and JSP not rendering

Viewed 4065

I've been working on this for days trying to get it to work. Trying to use JSP with Spring Boot. I've been following the example at github. So many examples on the web have a web.xml file, yet the example on github, which is referenced many times on this forum, doesn't even have a web.xml. Also, when creating a Spring Boot Project, STS doesn't create a "webapp" directory with subdirectory "WEB-INF". I had to create those manually in Windows Explorer then refresh Project Explorer in STS to make them appear.

Regardless, I have followed the example as best I can but still can't get my index.jsp file to render in the browser when I type in http://localhost:8080 in the address field.

Any guidance would be appreciated.

This is what I have so far.
Directory structure:

enter image description here

application.properties:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/jeddatae_editor
spring.datasource.username=webapp
spring.datasource.password=secret

index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%
  String name = request.getParameter("name");
%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JED Login</title>
</head>
<body>
<form action="/index" method="post">
<p>Insert ID here: <input type="text" id="id" name="id"></p>
<input type=submit>
</form>
<%
  if(!name.equals(""))
  {
%>
<div class="welcome">
  <p>Welcome <%=name %> </p><p>If you are not <%=name %>, then try a different id.</p>
</div>
<%
  }
%>

hello.jsp:

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

GreetingController.java

package com.anypackage.jedcontext;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;
import com.tacticalenterprisesltd.Database;

@Controller
public class GreetingController {

    @RequestMapping("/")
    public RedirectView index(@RequestParam(value="id", required=false) String id, RedirectAttributes attribs) {        
        if (id != null)
        {
          Database db = new Database("jeddatae_editor");
          String[][] result = db.executeSelect("SELECT CONCAT(first_name, ' ', last_name) FROM employees WHERE id=" + id + ";"); 
          if(result.length != 0){
            attribs.addAttribute("name", result[0][0]);
          }        
        }        

        return new RedirectView("index");
    }

    @RequestMapping("/hello")
    public String hello() {

        return "hello";
    } 
}

pom.xml:

<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.anypackage</groupId>
    <artifactId>jedtest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>jedtest</name>
    <description>An application context test</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>         
        </dependency>        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Tomcat embedded container-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- Need this to compile JSP -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>            
        </dependency>

        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>           
        </dependency>

    </dependencies> 

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
3 Answers
<dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>
<dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>servlet-api</artifactId>
                    <groupId>javax.servlet</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jsp-api</artifactId>
                    <groupId>javax.servlet.jsp</groupId>
                </exclusion>
            </exclusions>
        </dependency>
add this dependency to the pom.xml in spring boot and also place jsp files in src/main/resources/webapp/WEB-INF/pages/index.jsp.... like this and configure it in application.properties as 
                             spring.mvc.view.prefix=/WEB-INF/WEB-INF/pages/index.jsp
                             spring.mvc.view.suffix=.jsp
It was solved for me.
Related