Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot: 404 error when calling JSP using controller

I'm getting the following error when running my project using Spring Tool Suite,

enter image description here

But in case my problem is I have already added the appropriate dependencies to pom.XML file. So what could be the problem?

My pom.XML file dependencies as follows,

        <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-starter-tomcat</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>

My controller ApplicationController.java as follows,

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ApplicationController {

    @RequestMapping("/")
    public String Welcome() {
        return "welcomepage";
    }
}

My vives are in src/main/webapp/WEB-INF/view/welcomepage.jsp you can look the tree view below,

enter image description here

And I have already changed the application.properties file as well. But still, I can't understand what is wrong.

My application.properties file as follows,

spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp

I just print hello in My welcomepage.jsp,

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello
</body>
</html>
like image 504
INDRAJITH Avatar asked Mar 23 '19 10:03

INDRAJITH


2 Answers

Looks like you was very close to the working application. The main issue in your code is in <scope>provided</scope> for your Jasper dependency. And also looks like you are running your code from eclipse IDE through the main method.

Long story short:

If you would like to run your application through the main method in MyApplication.java then just remove scope provided for the Jasper.

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

Or you can run your application exactly in that state like you have right now from the console:

mvn clean spring-boot:run

But I suggest to remove this scope so you could be able to run your code from IDE and from console. In addition to that looks like spring-boot-starter-tomcat dependency is redundant (it must be available within spring-boot-starter-web). In a nutshell please try to use following pom file:

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
    </dependencies>
</project>

Hope my answer will help you.

like image 163
Mikita Berazouski Avatar answered Sep 21 '22 22:09

Mikita Berazouski


You may also need to add this in pom.xml

 <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
 </dependency>

UPDATE 1:

JSP Limitation

When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

  • With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.
  • Undertow does not support JSPs.
  • Creating a custom error.jsp page does not override the default view
    for error handling. Custom error pages should be used instead.

Scope

compile This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

provided This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

runtime This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.

Also, Try to change the Following in tomcat-embed-jasper

Remove <scope>provided</scope> OR change the scope to compile <scope>compile</scope>

JSP Limitations Spring Boot JSP 404

like image 30
Romil Patel Avatar answered Sep 21 '22 22:09

Romil Patel