2015-01-15 143 views
12

问题:我无法通过我的Spring Boot web MVC应用程序在WEB-INF/jsp下看到我的视图。Spring boot MVC:找不到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> 
     <%[email protected] 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 
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 

我在做什么错?

+0

貌似服务器不能够找到控制器(不像您提到的JSP文件)。盯着服务器时是否有任何异常? – kranthi117 2015-01-15 15:24:05

+0

不,它实际上开始罚款..它也显示在控制台上: “映射”{[/ lista_utenti],methods = []“... – abierto 2015-01-15 15:25:50

+0

您是使用嵌入式容器,还是部署战争文件到单独的servlet容器?假设你使用的是SpringBootServletInitializer,我猜测它是后者,但它确实有用。另外,你是否检查过JSP是否打包在你的jar或war文件中的预期位置? – 2015-01-15 16:09:16

回答

15

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

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

没有这个,我得到了同样的错误。

+1

这是为什么需要?我认为只要返回一个ModelAndView html,jsp或其他任何模板引擎就可以实现这个功能。 – theadam 2015-11-11 22:30:05

+0

它正在工作,但仍然对此依赖性感到困惑。 – 2016-01-26 11:49:34

+0

它在使用.jsp时有效。如果我想使用.html并且仍然希望Spring为我找到这些视图,该怎么办? – Sategroup 2016-04-22 05:21:18

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

将此添加到我的项目,是在Intellj和它的作品..

+0

Upvoted..it为我工作。 – Vikki 2017-10-23 15:11:06

0

我'在我的Maven的依赖条件加入这个,现在它的工作

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