spring-boot - 使用 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>
可能出了什么问题?还有一个地方/视图/日志,我以后可以去检查有什么问题吗?
解决方案
我已经克隆了你的项目并运行了它。我确实有几点注意事项:
- 在您的
pom.xml
中,您添加了spring-boot-starter-security
依赖项,一旦您访问您的 Web 应用程序,它将创建一个默认登录页面。如果您在此阶段不需要安全性,请从您的应用程序中删除所有安全性依赖项,pom.xml
或者您必须为您的应用程序提供安全性配置。 - 访问您的应用程序时,请确保将“home”添加到您的 URL:http://localhost:8080/home,因为这已映射到您的控制器。如果您想为您的应用程序添加默认上下文,请将以下内容添加到application.properties :
server.servlet.contextPath=/zooplus
,然后您可以通过 URL 访问它:http://localhost:8080/zooplus/home
推荐阅读
- reactjs - 当我在路由中从登录页面移动到注册页面时,CSS 没有呈现
- swift - 弹出视图控制器不显示在旧设备 Swift 上
- reactjs - react-responsive-modal,模态在打开时闪烁严重
- watchkit - 如何在 Apple Watch 独立应用程序中获取网络连接状态?
- flutter - 如何在 Flutter 中访问/更改 Widget 的属性
- python - 如何在不考虑python中的主导因素的情况下找到列表中的平均值?
- javascript - 鼠标悬停时弹出窗口消失
- javascript - 重新分配超时变量如何影响 Settimeout
- html - 关于 mat-card-title 不对齐中心
- wpf - 如何删除每个 TabItem 旁边的白线?