gpt4 book ai didi

java - 反复获取带有映射规范的 WhiteLabel 错误页面

转载 作者:行者123 更新时间:2023-12-02 09:03:00 27 4
gpt4 key购买 nike

我是 Spring 新手。我目前正在遵循 T 教程并遇到“Whitelabel 错误页面”。我已经完成了研究,检查Spring boot - Whitelabel Error PageSpring Boot Remove Whitelabel Error Page运气不好。

我试图通过简单地使用:http://localhost:8080/hello

来访问该页面

显然 Spring 找不到该页面,但我不明白这是为什么。

我的 html 文件:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf demo</title>
</head>
<body>
<p th:text="'time on the server is: ' + ${theDate}" />
</body>
</html>

Pom 文件:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cat.otsGrief</groupId>
<artifactId>GriefReconcile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>GriefReconcile</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

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

</project>

Java 类:

package com.cat.ots.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class DemoController {


@GetMapping("/hello")
public String sayHello(Model theModel) {

theModel.addAttribute("theDate", new java.util.Date());

return "helloworld";
}

}

文件结构:

感谢任何帮助

编辑:

我正在寻找的答案在这里找到 - Issue With Spring: There was an unexpected error (type=Not Found, status=404)

添加对不在主类的同一包或子包中的包的组件扫描。

最佳答案

您必须配置viewResolver和internalResourceView解析器来提供jsp/html文件,找到下面的代码片段,浏览 link

package com.howtodoinjava.app.controller;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan
public class MvcConfiguration extends WebMvcConfigurerAdapter
{
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
registry.viewResolver(resolver);
}
}

关于java - 反复获取带有映射规范的 WhiteLabel 错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60032482/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com