16

Problem: I can't reach my view under WEB-INF/jsp on my Spring Boot web MVC app.

What I've done:

this is my JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <ul>
        <%--@elvariable id="users" type="java.util.List"--%>
        <c:forEach items="${utenti}" var="utente">
            <li>
                <c:out value="${utente.getUsername()}"/>
            </li>
        </c:forEach>
    </ul>
</body>
</html>

this is my Controller:

@Controller
public class UtenteController {

    @Autowired
    private UtenteService utenteService;

    @RequestMapping("/lista_utenti")
    public ModelAndView getListaUtentiView(){
        ModelMap model = new ModelMap();
        model.addAttribute("utenti", this.utenteService.getListaUtenti());
        return new ModelAndView("lista_utenti", model);
    }

}

this is my application.properties:

# MVC
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp

#Postgres config
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/DB_Test
spring.datasource.username=postgres
spring.datasource.password=postgres

and, at least, my main application:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SpringWebApplication extends SpringBootServletInitializer{

    public static void main(String[] args) {
        SpringApplication.run(SpringWebApplication.class, args);
    }

    @Override
    protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.sources(SpringWebApplication.class);
    }
}

What I'll get if I go to http://localhost:8080/lista_utenti is:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jan 15 15:59:57 CET 2015
There was an unexpected error (type=Not Found, status=404).
No message available

What am I doing wrong?

abierto
  • 1,447
  • 7
  • 29
  • 57
  • Looks like the server is not able to find the controller (not JSP file like you mentioned). Did you get any exceptions while staring the server ? – kranthi117 Jan 15 '15 at 15:24
  • Nope, it actually starts fine.. And it also shows on console: "Mapped "{[/lista_utenti],methods=[]"... – abierto Jan 15 '15 at 15:25
  • Are you using an embedded container, or deploying a war file to a separate servlet container? Given that you're using SpringBootServletInitializer, I'd guess it's the latter but it'd be useful to know for sure. Also, have you checked that the JSP is packaged in your jar or war file in the expected location? – Andy Wilkinson Jan 15 '15 at 16:09
  • I'm using the embedded container provided by Boot. I've created my web-app from scratch, using the "wizard" on the latest version of STS (same as the Spring Initializer). On my POM, I've only added few dependencies for my postgres driver, jstl and MyBatis. I'm Packaging everything into a war, and it's at my expected location. – abierto Jan 15 '15 at 16:18
  • Sorry to ask, but I'm facing a similar problem: is your application WAR- or JAR-packaged? Where exactly did you place the *.jsp files respectively your WEB-INF directory... – DoNuT Nov 16 '17 at 15:21
  • @DoNuT it was WAR-packaged. – abierto Dec 04 '17 at 08:12
  • OK, you named your JSP file ´lista_utenti.jsp` (as the view is named)? – DoNuT Dec 04 '17 at 12:57
  • I'm sorry, but I don't remember that. This question is 2 years and a half old..! – abierto Dec 05 '17 at 10:11

5 Answers5

24

Zio, Are you sure you've included this dependency on your pom ?

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

Without this, I'm getting your same error.

Trink
  • 584
  • 3
  • 14
7
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>required</scope>
</dependency>

Added this to my project that is in Intellj and it works..

lakshmi
  • 133
  • 1
  • 6
1

I'am adding this in my maven dependecies and now it's working

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-tomcat</artifactId>
 <scope>provided</scope>
</dependency>
Gusti Arya
  • 1,281
  • 3
  • 15
  • 32
0

Add Tomcat Embed Jasper Dependency in pom.xml file

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

If you've moved from using the standard thymeleaf project to Bootstrap (as I have), ensure you remove this dependency;

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
Mike at Savient
  • 250
  • 2
  • 12