首页 > 解决方案 > 使用 Spring Boot 和 Thymeleaf 时如何调试 404 错误?

问题描述

这是控制器类:

    package me.ankit.zooplus;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    @Controller
    public class HomeController {   
        @RequestMapping("/home")
        public String home() {

        return "home";
    }
}

这是我的应用程序类

package me.ankit.zooplus;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ZooplusApplication  {

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

我的文件夹结构如下: 文件结构

我从 maven [从控制台复制并粘贴了几行]:

INFO 7484 --- [lication.main()] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/home]}" onto public java.lang.String me.ankit.zooplus.HomeController.home()
INFO 7484 --- [lication.main()] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
INFO 7484 --- [lication.main()] me.ankit.zooplus.ZooplusApplication      : Started ZooplusApplication in 10.554 seconds (JVM running for 16.948)

[INFO] BUILD SUCCESS

最后是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>me.ankit</groupId>
<artifactId>zooplus</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>zooplus</name>
<description>zooplus: Currency Converter</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <!-- hot swapping, disable cache for template, enable live reload -->       
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
    <packaging>jar</packaging>
</project>

可能出了什么问题?还有一个地方/视图/日志,我以后可以去检查有什么问题吗?

标签: spring-bootthymeleaf

解决方案


我已经克隆了你的项目并运行了它。我确实有几点注意事项:

  1. 在您的pom.xml中,您添加了spring-boot-starter-security依赖项,一旦您访问您的 Web 应用程序,它将创建一个默认登录页面。如果您在此阶段不需要安全性,请从您的应用程序中删除所有安全性依赖项,pom.xml或者您必须为您的应用程序提供安全性配置。
  2. 访问您的应用程序时,请确保将“home”添加到您的 URL:http://localhost:8080/home,因为这已映射到您的控制器。如果您想为您的应用程序添加默认上下文,请将以下内容添加到application.properties : server.servlet.contextPath=/zooplus,然后您可以通过 URL 访问它:http://localhost:8080/zooplus/home

推荐阅读