java - Spring 启动 MVC : can't find JSP

标签 java spring jsp spring-mvc spring-boot

问题:我无法在我的 Spring Boot Web MVC 应用程序的 WEB-INF/jsp 下看到我的 View 。

我做了什么:

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

这是我的 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);
    }

}

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

至少还有我的主要应用程序:

@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);
    }
}

如果我访问 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

我做错了什么?

最佳答案

Zio,你确定你已经在你的 pom 中包含了这个依赖吗?

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

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

关于java - Spring 启动 MVC : can't find JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27966602/

相关文章:

java - 在 Libgdx 中,如何获取图像旋转时的 x、y 坐标?

java - 我正在开发一个简单的英语学习教程应用程序,但我在设置编号位置时遇到了问题

java - 使用数据库测试 spring boot api

java - 如何获取 Autowiring 的bean的范围

java - 如何将 JSP 页面包含到自定义标记中

java - 具有多个数据源的 Spring Data Controller

java - java中的Strictfp方法有什么用?

java - 请求的资源上不存在 'Access-Control-Allow-Origin' header ,并且 JWT token 不以 Bearer String 开头

java - 默认构造函数无法处理隐式 super 构造函数抛出的异常类型 Exception

spring - JSP 表单 : HttpStatus 400: The request sent by the client was syntactically incorrect