为什么我总是得到状态为#34; 404"的白标错误页面?在运行简单的Spring Boot应用程序时

时间:2016-09-22 01:37:03

标签: java spring jsp spring-mvc spring-boot

我的控制器

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

我的JSP(welcome.jsp)里面/ WEB-INF / 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);
    }
}

我甚至将thymeleaf依赖添加到我的pom中。它仍然没有奏效。当我点击localhost:8080/hello or /indexPage or /indexPageWithModel时,它总是说

Whitelabel错误页面

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

Wed Sep 21 21:34:18 EDT 2016 出现意外错误(type = Not Found,status = 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

请帮帮我。谢谢!

4 个答案:

答案 0 :(得分:4)

自己想出来。

当我将动态webproject转换为maven项目时,它没有以这种方式创建文件夹结构

src/main/java

src/main/resources

src/main/webapp

我手动创建了自己并将jsp文件从WebContent/WEB-INF/jsp移动到src/main/webapp/WEB-INF/jsp并修改了项目属性中的Java build path

然后我重新启动embedded tomcat并再次尝试。它奏效了。

答案 1 :(得分:3)

这是几乎所有春季靴初学者都面临的最常见错误之一。

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

P.S。-这里的“ org.test.controller”是我保存控制器的软件包的合格名称。

答案 2 :(得分:1)

有关Whitelabel错误页面的常规疑难解答指南:

为Spring Web处理启用跟踪日志记录。添加到您的application.properties中: logging.level.org.springframework.web.*=TRACE

由此,您可以在启动过程中查看控制器是否已正确注册。例如,让HelloController具有一个映射的GET方法,您应该在日志中看到:

2020-08-20 08:56:55.731 TRACE 19687 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping :
    c.n.g.c.HelloController:
    {GET /hello}: sayHello()

如果您看到控制器方法已记录,则表明它已正确注册。如果没有,请按照其他建议正确设置项目结构。

如果控制器已正确注册,但仍显示白色标签错误页面,则可能表示以下情况之一:

  • 您正在使用错误的url /方法/内容类型呼叫端点
  • 您的端点上发生了错误
  • 您没有返回正确的响应(这可能导致Spring显示错误页面)

在这些情况中,还应该从生成的日志中揭示出来。狩猎愉快!

答案 3 :(得分:0)

这种类型的错误的原因是Bootstrap类不知道控制器的位置。

在这种情况下,我们需要指定可以参考使用的包或类路径 @ComponentScan(basePackages={"com.sample.controller"}),在我的情况下,我已将软件包指定为com.sample.controller

相关问题