Spring启动MVC:找不到JSP

abi*_*rto 14 java spring jsp spring-mvc spring-boot

问题:我无法WEB-INF/jsp在Spring Boot web MVC应用程序上查看我的视图.

我做了什么:

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

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

}
Run Code Online (Sandbox Code Playgroud)

这是我的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
Run Code Online (Sandbox Code Playgroud)

至少,我的主要应用:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我去,我会得到的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
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

小智 21

Zio,你确定你已经将这种依赖性包含在你的pom中吗?

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

没有这个,我会得到同样的错误.

  • 如果您正在运行嵌入式,则需要它 - Jasper 是 JSP 编译器。 (3认同)
  • 为什么需要这个?我认为像返回ModelAndView html,jsp或任何其他模板引擎那样简单的事情就可以直接使用吗? (2认同)

小智 7

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>required</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

将此添加到我在Intellj中的项目,它可以工作..