【问题标题】:Thymeleaf Exception parsing document in SpringSpring中Thymeleaf Exception解析文档
【发布时间】:2018-01-18 17:54:07
【问题描述】:

错误信息如下:

出现意外错误(类型=内部服务器错误, 状态=500)。异常解析文档:template="register",第 15 行 - 第 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>

用户控制器.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 type="password"。并改用简写@GetMapping

标签: java spring thymeleaf


【解决方案1】:

这是因为您错过了&lt;input&gt; 元素的结束标记。

例如:使用这个

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

而不是

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-08
    • 2019-06-11
    • 2019-03-01
    • 2019-02-20
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多