【发布时间】: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。