9

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>
Alan
  • 822
  • 1
  • 16
  • 39
  • spring-boot projects never creates a webapp directory by default. do you get any error message? – Patrick Jul 13 '17 at 06:31
  • JSPs are packaged into a "war" not "jar". You didn't follow at all the example in github – akuma8 Jul 13 '17 at 07:37
  • You spotted the problem in the pom.xml file. Great! I changed the packaging type to war, but doing so generated an error message which I have no idea how to fix. Error: Failure to transfer org.apache.maven.plugins:maven-war-plugin:pom:2.6 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.plugins:maven-war-plugin:pom:2.6 from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org – Alan Jul 14 '17 at 00:47
  • I did a search on the error and followed instructions at https://stackoverflow.com/questions/5074063/maven-error-failure-to-transfer That seemed to help the project compile properly, however, when running the app, changing the packaging from jar to war didn't make any difference. I'm still getting a status 404 in the browser. – Alan Jul 14 '17 at 17:58
  • One other thing happened when I change the packaging to type war. As you can see from the Directory Structure picture, it now expects a web.xml file. So now I'm left scratching my head. The github example specifically shows no web.xml, yet my project is now expecting me to include one. Please advise. – Alan Jul 14 '17 at 22:02
  • I added another webpage named hello.jsp. In the browser, if I type http://localhost:8080/hello then the hello.jsp page renders properly. So why doesn't index.jsp not render??? – Alan Jul 15 '17 at 00:04
  • Why do you use RedirectView? I mean, simply use String as a method return type, in your parameters use (Model model), add your attributes via model.addAttribute("stg", someobject); and return "index" – Leah Jul 18 '17 at 08:52

3 Answers3

2

It depends upon how you package your artifact as a war/jar file.

War File Package following rules should be followed.

  1. Your main class should extend SpringBootServletInitializer
  2. You have to create a custom error.jsp page won’t override the default view for error handling, custom error pages should be used instead.
  3. You run these ordinary maven goals like mvn clean install package -e and deploy it your in tomcat web server.

JAR file Package

  1. It won't work as your embedded tomcat is using hard coded file pattern in tomcat.

  2. Exactly follow this sample and refactor your code accordingly.

    Please refer this JSP Limitations with Spring Boot.

Community
  • 1
  • 1
Praveen Kumar K S
  • 3,024
  • 1
  • 24
  • 31
1

I think you first need to handle redirect. Because in your case new RedirectView("index") redirects to for example /index?name=Alan

Add method:

    @GetMapping("/index")
    public String handleRedirect() {
        return "index";
    }

It should work if you run it as simple spring boot app:

mvn spring-boot:run

But if you are going to deploy war file on app server don't forget to configure like this:

@SpringBootApplication
public class JedtestApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(JedtestApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(JedtestApplication.class, args);
    }
}
Ihor Rybak
  • 3,091
  • 2
  • 25
  • 32
-1
<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.