Spring Boot项目访问template模板文件页面返回404

发布于 2022-09-05 09:04:43 字数 3943 浏览 23 评论 0

今天刚刚接触Spring Boot框架技术,安装网上的教程做了一个Hello World的测试项目,但是不知道为什么,访问静态资源和json数据都没问题,但是访问template模板界面的时候却不行,浏览器返回404错误代码。

项目路径截图
clipboard.png

在templates目录下添加了一个template模板文件:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>

最主要的Controller类:

package com.shanshan.start.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        return "Hello World";
    }
    
    @RequestMapping("/w")
    public String index() {
//        map.addAttribute("host", "http://www.shanshan.com");
        return "world";
    }
}

页面报错:
图片描述

最后再申明一下!访问静态资源图片和@ResponseBody注解的json数据都是可以的!只有访问页面的时候不行!

补充一下,pom文件内容(只引入了web和thymeleaf)

<?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>com.shanshan</groupId>
    <artifactId>start</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>start</name>
    <description>Demo project for Spring Boot</description>

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

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


</project>

隔天8.8再次补充:
昨天晚上回去在自己电脑上一样的流程新建了一个spring boot项目,可以正常运行没有上述问题。
今天早上在公司删除昨天建的项目重新构建,还是一样的问题不能访问。
代码已上传git,望大佬指点迷津
https://git.oschina.net/dmh54...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冰雪之触 2022-09-12 09:04:43

最后解决了。
按照在Stack Overflow上一位外国朋友遇到的差不多的问题以及解答,说是有可能是maven引入的包有问题导致的。但是该问题无法通过在项目中清理后重新构建来修复(本人尝试无用),只能把配置的对应的maven m2目录清空,然后打开eclipse以后重新构建的时候让maven重新下载所需jar包才可以。

奶茶白久 2022-09-12 09:04:43

依赖了 thymeleaf jar 包吗?

那一片橙海, 2022-09-12 09:04:43

看着没毛病。方便的话传一发工程到Git上?

别靠近我心 2022-09-12 09:04:43

遇到同样的问题。后来在pom文件里的<properties>标签下加了下面两行就好了

<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
在梵高的星空下 2022-09-12 09:04:43

application.properties里写
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文