java - Spring中Thymeleaf异常解析文档

标签 java spring thymeleaf

错误消息如下:

There was an unexpected error (type=Internal Server Error, status=500). Exception parsing document: template="register", line 15 - column 3

如果我删除使用 th: 的表单,则它会正确显示页面。但通过表格我得到了错误。我在代码中看不到任何错误,希望有人能看一下。

注册.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>User Registration</title>
</head>
<body>

<h3>User Registration</h3>

<form action="#" th:action="@{/user/register}" th:object="${user}" method="post">
    <p>First Name:</p> <input type="text" th:field="*{firstname}">
    <p>Last Name:</p> <input type="text" th:field="*{lastname}">
    <p>Password:</p> <input type="text" th:field="*{password}">
    <input type="submit" value="Register">
</form>

</body>
</html>

UserController.java:

package com.demo.spring.controller;

import com.demo.spring.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String registerView(Model model)
    {
        User user = new User();
        model.addAttribute("user", user);
        return "register";
    }

}

用户.java:

package com.demo.spring.domain;

public class User {

    String firstname;
    String lastname;
    String password;

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }



}

最佳答案

这是因为您错过了 <input> 的结束标记元素。

例如:使用这个

<input type="text" th:field="*{firstname}"/>

而不是

 <input type="text" th:field="*{firstname}">

Thymeleaf只能处理有效的XML .它仅适用于格式良好的 XML,因此如果您想使用 HTML5模板模式您的 HTML 必须是格式良好的 XML。否则,您可以使用LEGACYHTML5模板

关于java - Spring中Thymeleaf异常解析文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48327528/

相关文章:

java - 调整框架大小后如何调整网格大小?

java - Spring DBCP 连接池 |配置重试次数

java - 为什么Tuckey UrlRewrite Filter在规则匹配后不调用chain.doFilter?

java - 插入数据库后,第一次没有读取该值

Spring Thymeleaf - 用于显示 HTML 项目的调用服务方法 bool 值

java - 没有测试用例的 super JUnit 类

java - 如果没有语法文件,我需要做什么才能在 Antlr 中以编程方式实现语法、解析器和词法分析器?

java - 如何在java中解密C#加密的AES字符串

spring - 如何在 Spring MVC Web 应用程序中安全地存储数据库凭据?

html - thymeleaf 折断 :checked css style change