【发布时间】:2016-02-18 12:18:35
【问题描述】:
我正在使用 Spring Boot 应用程序。我已经为其设置了 MvcConfig 类,并将 tomcat-embed-jasper 和 jstl 依赖项添加到 pom.xml。但是,我无法在“WEB-INF”文件夹中查看我的 jsp 文件,我会收到 404 错误(Whitelabel 错误页面)。我已经设置了 Application.properties。这是我的 application.properties:
#
## View resolver
#
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
这是我的 MvcConfig 类:
package com.goodvideotutorials.spring.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
}
这是我的 home.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>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Up and running with Spring Framework quickly</title>
</head>
<body>
<h2>Hello, world!</h2>
</body>
</html>
它被插入到 src > main > webapp > WEB-INF > jsp > home.jsp
我已将这些依赖项添加到 pom.xml:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
这是我的 Application.java 类:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}
和我的控制器类:
@Controller
public class RootController {
// @RequestMapping("/")
// public String home() {
//
// return "home";
//
// }
}
无论如何,如果我在控制器中注释代码并且不使用 MvcConfig,它就不起作用。如果我评论该代码并使用 MvcConfig 类,它就不能正常工作。这是网址:localhost:8080
我刚刚测试了很多东西,但它显示的是“Whitelabel Error Page”而不是 JSP。我还在 JEE 环境中安装了 Tomcat 服务器。这会导致问题吗?
【问题讨论】:
标签: spring jsp spring-boot