无法访问Spring Boot项目中的索引文件。

huangapple go评论73阅读模式
英文:

Can't access index file in spring boot project

问题

以下是您提供的代码的中文翻译:

package com.school.project;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/")
    public String AllUsers(Model model) {
        model.addAttribute("listUsers", userService.getAllUsers());
        return "users";
    }

}

以及位于我的模板文件夹中的index.html文件:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">

<head>
    <title>管理网站</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" th:href="@{webjars/bootstrap/5.2.3/css/bootstrap.min.css}">
</head>

<body>
    <div class="container-fluid text-center">
        <div>
            <table>
                <thead>
                    <th>ID</th>
                    <th>电子邮件</th>
                    <th>名称</th>
                    <th>用户名</th>
                    <th>密码</th>
                    <th></th>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>
    </div>
</body>

</html>

但是,每当我启动我的应用程序时,我都会收到以下错误:

2023年5月20日18:57:06 IDT
发生了意外错误(类型=内部服务器错误,状态=500)。
错误解析模板[users],模板可能不存在,或者可能无法由任何配置的模板解析器之一访问
org.thymeleaf.exceptions.TemplateInputException: 错误解析模板[users],模板可能不存在,或者可能无法由任何配置的模板解析器之一访问

如果我更改我的索引映射的路由,HTML将无问题加载,但如果我为它们提供相同的路由,我会收到该错误。

这与我尝试访问没有匹配模板文件的URL时收到的错误相同。

我知道这个错误已经发布过,但是其中没有任何解决方案适用于我。

我的服务类如下:

package com.school.project;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public List<User> getAllUsers() {
        List<User> users = userRepository.findAll();
        return users;
    }
}

以及我的模型类:

package com.school.project;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@Table(name = "user")
@AllArgsConstructor
@NoArgsConstructor
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "isAdmin")
    private boolean isAdmin;

    @Column(name = "email")
    private String email;

    @Column(name = "username")
    private String userName;

    @Column(name = "name")
    private String name;

    @Column(name = "password")
    private String password;
}

希望这些翻译能帮助您更好地理解您的代码和问题。如果您有其他问题或需要进一步的帮助,请随时提出。

英文:

I have the following controller.

package com.school.project;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(&quot;/&quot;)
    public String AllUsers(Model model) {
        model.addAttribute(&quot;listUsers&quot;, userService.getAllUsers());
        return &quot;users&quot;;
    }

}

and an index.html file placed in my templates folder.

&lt;!DOCTYPE html&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot; lang=&quot;en&quot;&gt;

&lt;head&gt;
    &lt;title&gt;Manager Site&lt;/title&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; th:href=&quot;@{webjars/bootstrap/5.2.3/css/bootstrap.min.css}&quot;&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;div class=&quot;container-fluid text-center&quot;&gt;
        &lt;div&gt;
            &lt;table&gt;
                &lt;thead&gt;
                    &lt;th&gt;ID&lt;/th&gt;
                    &lt;th&gt;E-mail&lt;/th&gt;
                    &lt;th&gt;name&lt;/th&gt;
                    &lt;th&gt;username&lt;/th&gt;
                    &lt;th&gt;password&lt;/th&gt;
                    &lt;th&gt;&lt;/th&gt;
                &lt;/thead&gt;
                &lt;tbody&gt;
                    
                &lt;/tbody&gt;
            &lt;/table&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

But whenever I launch my application, I get the following error.

Sat May 20 18:57:06 IDT 2023
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [users], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [users], template might not exist or might not be accessible by any of the configured Template Resolvers

If I change the routing of my index mapping, the html will load with no problem, but if I give them the same routing, I get that error.

It is the same error. I get if I try to go to a URL with no matching template file.

I know this error has already been posted, but none of the solutions worked for me.

My service class is the following.

package com.school.project;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public List&lt;User&gt; getAllUsers() {
        List&lt;User&gt; users = userRepository.findAll();
        return users;
    }
}

and my model class.

package com.school.project;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@Table(name = &quot;user&quot;)
@AllArgsConstructor
@NoArgsConstructor
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &quot;id&quot;)
    private Long id;

    @Column(name = &quot;isAdmin&quot;)
    private boolean isAdmin;

    @Column(name = &quot;email&quot;)
    private String email;

    @Column(name = &quot;username&quot;)
    private String userName;

    @Column(name = &quot;name&quot;)
    private String name;

    @Column(name = &quot;password&quot;)
    private String password;
}

答案1

得分: 1

更新文件内容如下:html

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org" lang="en">

    <head>
        <title>Manager Site</title>
        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap/5.2.3/css/bootstrap.min.css}">
    </head>

    <body>
        <div class="container-fluid text-center">
            <div>
                <table class="table">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Email</th>
                            <th>Name</th>
                            <th>Username</th>
                            <th>Password</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr th:each="user : ${listUsers}">
                            <td th:text="${user.id}"></td>
                            <td th:text="${user.email}"></td>
                            <td th:text="${user.name}"></td>
                            <td th:text="${user.username}"></td>
                            <td th:text="${user.password}"></td>
                            <td><!-- additional actions if needed --></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </body>

    </html>
英文:

Update the file with the following content: html

    &lt;!DOCTYPE html&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot; lang=&quot;en&quot;&gt;

&lt;head&gt;
    &lt;title&gt;Manager Site&lt;/title&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; th:href=&quot;@{/webjars/bootstrap/5.2.3/css/bootstrap.min.css}&quot;&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;div class=&quot;container-fluid text-center&quot;&gt;
        &lt;div&gt;
            &lt;table class=&quot;table&quot;&gt;
                &lt;thead&gt;
                    &lt;tr&gt;
                        &lt;th&gt;ID&lt;/th&gt;
                        &lt;th&gt;Email&lt;/th&gt;
                        &lt;th&gt;Name&lt;/th&gt;
                        &lt;th&gt;Username&lt;/th&gt;
                        &lt;th&gt;Password&lt;/th&gt;
                        &lt;th&gt;&lt;/th&gt;
                    &lt;/tr&gt;
                &lt;/thead&gt;
                &lt;tbody&gt;
                    &lt;tr th:each=&quot;user : ${listUsers}&quot;&gt;
                        &lt;td th:text=&quot;${user.id}&quot;&gt;&lt;/td&gt;
                        &lt;td th:text=&quot;${user.email}&quot;&gt;&lt;/td&gt;
                        &lt;td th:text=&quot;${user.name}&quot;&gt;&lt;/td&gt;
                        &lt;td th:text=&quot;${user.username}&quot;&gt;&lt;/td&gt;
                        &lt;td th:text=&quot;${user.password}&quot;&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;!-- additional actions if needed --&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/tbody&gt;
            &lt;/table&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

答案2

得分: 0

我认为我找到了问题,你的文件html的名称应该与你的方法AllUsers返回的名称相同,所以只需将index.html替换为users.html。

英文:

I think i found the problem, The name of your file html should be the same name returned in your method AllUsers, so just replace index.html by users.html.

huangapple
  • 本文由 发表于 2023年5月21日 00:13:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定