Java Spring boot MVC:找不到JSP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27966602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:22:41  来源:igfitidea点击:

Spring boot MVC: can't find JSP

javaspringjspspring-mvcspring-boot

提问by abierto

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

问题:我无法WEB-INF/jsp在 Spring Boot Web MVC 应用程序下访问我的视图。

What I've done:

我所做的:

this is my JSP:

这是我的 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:

这是我的 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_utentiis:

如果我去,我会得到的http://localhost:8080/lista_utenti是:

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?

我究竟做错了什么?

采纳答案by Trink

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

Zio,你确定你已经在你的 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.

没有这个,我会遇到同样的错误。

回答by lakshmi

<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..

将此添加到我在 Intellj 中的项目中,并且可以正常工作。

回答by Gusti Arya

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

我将它添加到我的 Maven 依赖项中,现在它正在工作

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-tomcat</artifactId>
 <scope>provided</scope>
</dependency>

回答by sagarwalke7030

Add Tomcat Embed Jasper Dependency in pom.xml file

在 pom.xml 文件中添加 Tomcat Embed Jasper Dependency

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

回答by Awila_Tech

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

如果您已经从使用标准 thymeleaf 项目转移到 Bootstrap(就像我一样),请确保删除此依赖项;

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