【问题标题】:Spring controller not able to resolve Thymeleaf templateSpring 控制器无法解析 Thymeleaf 模板
【发布时间】:2019-06-19 02:14:00
【问题描述】:

控制器类无法解析除根以外的任何路径。 我尝试将简单的 html 代码放在 index.html 中,但控制器无法解决它。控制器只能解决这部分

 @RequestMapping("/")
    public String home() {
        return "redirect:/index";
    }

控制器代码:

package com.controller;

import java.util.HashSet;
import java.util.Set;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.userFront.domain.User;

@Controller
public class HomeController {

    @RequestMapping("/")
    public String home() {
        return "redirect:/index";
    }

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String signup(Model model) {
        User user = new User();

        model.addAttribute("user", user);

        return "signup";
    }
//  
}

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.userFront</groupId>
    <artifactId>userFront</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>userFront</name>
    <description>User front for online banking app</description>

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

    <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>

        <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>

index.html 的代码:

我尝试过放置基本的 html 代码,但没有成功解决它

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head th:replace="common/header :: common-header"/>
<body>
<!-- Login Section -->
    <img class="img-responsive" src="/images/banner.png" alt="banner"/>
    <div class="container">
        <div class="row ">
            <div class="main-center ">
                <div class="bg-danger" th:if="${param.error}">
                    Invalid username and secret.
                </div>
                <div class="bg-danger" th:if="${param.logout}">
                    You have been logged out.
                </div>
                <form class="form-signin" th:action="@{/index}" method="post">
                    <h2 class="form-signin-heading">Please sign in</h2>
                    <div class="form-group">
                        <label for="username" class="sr-only">Username</label>
                        <input type="text" roleId="username" class="form-control" placeholder="Username" name="username"
                               id="username"
                               required="required" autofocus="autofocus"/>
                    </div>
                    <div class="form-group">
                        <label for="password" class="sr-only">Password</label>
                        <input type="password" roleId="inputPassword" class="form-control" placeholder="Password"
                               id="password"
                               name="password" required="required"/>
                    </div>
                    <div class="form-group">
                        <input type="checkbox" name="remember-me" id="remember-me"/> &nbsp; Remember me
                    </div>
                    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
                </form>

                <hr />

                <div class="form-group ">
                    <a type="submit" class="btn btn-info btn-lg btn-block login-button" th:href="@{/signup}">Sign up!</a>
                </div>
            </div>
        </div>
    </div>

<div th:replace="common/header :: body-bottom-scripts"/>

</body>
</html>

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    我发现您的 pom.xml 和配置级别中遗漏了一些配置,请按照以下步骤操作,希望对您有所帮助。 1. pom.xml

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            .....
        </plugins>
    </build>
    

    2。 模板:资源文件夹路径

    src/main/resources/templates/index.html

    1. 控制器:不需要重定向

      @GetMapping({"/", "/index"}) public String index() { return "index"; }

    【讨论】:

    • 我可以在进入根页面时看到该页面,但是当我尝试localhost:8080/index 时,它会给出 404 错误。路径为:/userFront/src/main/resources/templates/index.html
    猜你喜欢
    • 2019-02-20
    • 2016-01-27
    • 2017-02-02
    • 1970-01-01
    • 2020-05-14
    • 2020-03-14
    • 2016-01-30
    • 1970-01-01
    • 2018-10-08
    相关资源
    最近更新 更多