HTML页面无法从模型Spring Boot Thymeleaf读取。

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

html page can't read from model spring boot thymeleaf

问题

I have the following controller

package com.website.school;

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.PostMapping;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

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

and a users.html file

<!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">
        <h1>ljk&#241;afs&#241;jafnjdfbhj&#241;sdbfj&#241;sdfvhjsdf</h1>
        <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></td>
                </tr>
            </tbody>
        </table>
    </div>
</body>

</html>

But whenever I run my application and navigate to /users I get the following error

There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/users.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/users.html]")

If I comment out the tbody the page loads as expected
I also know that I have a connection with my database because I made a test class that is a RestController and it returns the json I expected

My service class

package com.website.school;

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() {
        return userRepository.findAll();
    }
}

My Model class

package com.website.school;

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

My repository

package com.website.school;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
英文:

I have the following controller

package com.website.school;

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.PostMapping;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

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

and a users.html file

&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;h1&gt;ljk&#241;afs&#241;jafnjdfbhj&#241;sdbfj&#241;sdfvhjsdf&lt;/h1&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;/td&gt;
                &lt;/tr&gt;
            &lt;/tbody&gt;
        &lt;/table&gt;
    &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

But whenever I run my application and navigate to /users I get the following error

There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: &quot;class path resource [templates/users.html]&quot;)
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: &quot;class path resource [templates/users.html]&quot;)

If I comment out the tbody the page loads as expected
I also know that I have a connection with my database because I made a test class that is a RestController and it returns the json I expected

My service class

package com.website.school;

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() {
        return userRepository.findAll();
    }
}

My Model class

package com.website.school;

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

My repository

package com.website.school;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository&lt;User, Long&gt; {
}

答案1

得分: 1

错误拼写的实体字段 userName

应该是:

<td th:text="${user.userName}"></td>

英文:

Wrong spelling of of the entity field userName.

&lt;td th:text=&quot;${user.username}&quot;&gt;&lt;/td&gt;

Should be:

&lt;td th:text=&quot;${user.userName}&quot;&gt;&lt;/td&gt;

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

发表评论

匿名网友

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

确定