java - 为什么我在运行一个简单的 Spring Boot 应用程序时总是得到状态为 "404"的 Whitelabel 错误页面

标签 java spring jsp spring-mvc spring-boot

我的 Controller

@Controller
//@RequestMapping("/")
//@ComponentScan("com.spring")
//@EnableAutoConfiguration
public class HomeController {

    @Value("${framework.welcomeMessage}")
    private String message;

    @RequestMapping("/hello")
    String home(ModelMap model) {
        System.out.println("hittin the controller...");
        model.addAttribute("welcomeMessage", "vsdfgfgd");
        return "Hello World!";
    }

    @RequestMapping(value = "/indexPage", method = RequestMethod.GET)
    String index(ModelMap model) {
        System.out.println("hittin the index controller...");
        model.addAttribute("welcomeMessage", message);
        return "welcome";
    }

    @RequestMapping(value = "/indexPageWithModel", method = RequestMethod.GET)
    ModelAndView indexModel(ModelMap model) {
        System.out.println("hittin the indexPageWithModel controller...");
        model.addAttribute("welcomeMessage", message);
        return new ModelAndView("welcome", model);
    }
}

/WEB-INF/jsp 中的我的 JSP (welcome.jsp)(父文件夹是 WebContent)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Welcome to Spring Boot</title>
</head>

<body>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Message: ${message}
</body>
</html>

我的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>SpringBootPlay</groupId>
    <artifactId>SpringBootPlay</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.jcabi</groupId>
            <artifactId>jcabi-log</artifactId>
            <version>0.17</version>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
        <start-class>com.spring.play.BootLoader</start-class>
        <main.basedir>${basedir}/../..</main.basedir>
        <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我的应用初始化器

@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan({ "com.spring.controller" })
@PropertySources(value = { @PropertySource("classpath:/application.properties") })
public class BootLoader extends SpringBootServletInitializer {

    final static Logger logger = Logger.getLogger(BootLoader.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(BootLoader.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(BootLoader.class, args);
    }
}

我什至在我的 pom.xml 中添加了 thymeleaf 依赖项。它仍然没有用。每当我点击 localhost:8080/hello 或/indexPage 或/indexPageWithModel 时,它总是说

白标错误页面

此应用程序没有/error 的显式映射,因此您将其视为后备。

美国东部时间 2016 年 9 月 21 日星期三 21:34:18 出现意外错误(类型=未找到,状态=404)。 ]/WEB-INF/jsp/welcome.jsp

我的 application.properties

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
framework.welcomeMessage=Welcome to Dashboard

请帮帮我。谢谢!

最佳答案

这是几乎所有 spring boot 初学者都会遇到的最常见错误之一。

对此的解决方案非常简单,- 您的 Bootstrap 类应该知道它应该引用的包或类路径,以便访问组件/ Controller 。因此,您需要指定如下:- @ComponentScan(basePackages= {"org.test.controller"})

P.S.- 这里的“org.test.controller”是我存放 Controller 的包的限定名称。

关于java - 为什么我在运行一个简单的 Spring Boot 应用程序时总是得到状态为 "404"的 Whitelabel 错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39628930/

相关文章:

Java irc 库

jsp - 使用 Java EE Form 身份验证登录后访问用户详细信息

java - 用Java动态查找局域网中的其他主机

java - 二元运算符+的错误操作数类型(JAVA 程序)

java - 使 Spring/Tomcat 与 HTML5 pushState 兼容

java - 将模拟bean注入(inject)到测试对象中

java - 无法阻止用户使用带有自定义 UserDetail 和 AuthenticationProvider 的 spring security 多次登录

Spring框架库

java - 如何将 JSP 页面解析为 XML 文件?

java - 只要 jquery 函数正在执行,就显示加载图像