当前位置: 首页 > 知识库问答 >
问题:

Simple spring boot webapp不工作,白色标签错误页面

堵飞鸿
2023-03-14

我试着运行我简单的“hello world”Spring Boot web应用程序,但它不起作用,我总是看到“白标签错误页面”。

我的控制器

package com.packt.webapp.controller;

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

    public String home(Model model){
        String welcome = new String("Witam");
        model.addAttribute("welcome", welcome);
        return "home";
    }
}

一个pplication.java

package com.packt.webapp.controller;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

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

home.html

    <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Test</title>
        <link rel="stylesheet" th:href="@{/css/style.css}" />
    </head>
    <body>
        <h1>Hello</h1>
        <span th:text="'Message: ' + ${welcome}"></span>
    </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>com.packt.webapp</groupId>
    <artifactId>basket</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.4.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-thymeleaf</artifactId>
        </dependency>
    </dependencies>

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

我做错了什么?我试图改变我的项目结构,玩弄依赖关系,没有任何帮助。有人能启发我吗?

共有2个答案

梁修贤
2023-03-14

根据上面的评论,您访问了错误的url。由于您已将控制器映射到/,并且您只有1个方法,因此您应该像这样访问它:

http://localhost:8080/

莫河
2023-03-14

您的home()方法很可能没有被spring注册,因为该方法上方没有@RequestMapping()。

如果您想将home注册到/,您有两个选项。您可以像这样将当前处于类级别的@RecestMap移动到home方法...

@Controller
public class HomeController {
    @RequestMapping("/")
    public String home(Model model){
        String welcome = new String("Witam");
        model.addAttribute("welcome", welcome);
        return "home";
    }
}

或者您可以在home()方法上方添加一个加法注释,并将其留空。

@Controller
@RequestMapping("/")
public class HomeController {
    @RequestMapping()
    public String home(Model model){
        String welcome = new String("Witam");
        model.addAttribute("welcome", welcome);
        return "home";
    }
}
 类似资料:
  • 我不知道如何解决这个问题 spring端:role.java 因此,如果我现在尝试inline=“javascript”: 当我运行它时,我得到这样的响应: 白标签错误页

  • 当我运行spring boot MVC应用程序时,得到以下白标签错误页面。 白标签错误页 此应用程序没有/error的显式映射,因此您将其视为一种后退。 Wed Apr 13 15:45:59 IST 2016出现意外错误(Type=内部服务器错误,Status=500)。循环视图路径[home]:将再次发送回当前处理程序URL[/rewards/web/home]。检查您的ViewResolve

  • 我试图创建一个spring boot starter项目。当我试着运行这个时,我遇到了这个错误。 白标签错误页面 此应用程序没有 /error的显式映射,因此您将此视为后备。 Fri Dec29 14:16:46IST 2017有一个意外的错误(type=未找到,status=404)。/ 我将jsp文件添加到src/web-inf/views中/ 已添加 Springmvc。看法前缀=/WEB-

  • 无法使用spring-boot加载非常简单的JSP页面,导致404未找到 HmisApplication.class 主控制器。Java语言 应用属性 pom.xml http://maven.apache.org/xsd/maven-4.0.0.xsd" MVC配置。Java语言 文件结构

  • 下面是我的代码:我正在从application.properties文件swagerconfig.java获取所有值 下面是我的springboot初始值设定项: ServletInitializer.java 日志显示它已映射: 这是我得到的错误:

  • 我坚持使用这个简单的MVC示例。当我启动应用程序并转到localhost:8080时,我得到了“白标签错误页面”,甚至我在“src/main/资源/模板”中创建了“index.html”。我还在我的索引方法上添加了@Request estMap(“/”)。我找不到问题。 : : -在"src/main/资源/模板"下: