我目前正在使用.jsp
将许多HTML
页转换为Thymeleaf
。我已经成功转换了其中的一些,但是在检查带有表单标签的任何页面时,出现以下错误:
Attribute name cannot be null or empty during the initial page load.
我一直在遵循https://spring.io/guides/gs/handling-form-submission/
上的指南查看代码
<!-- Registration Form -->
<form action="#" th:action="@{/register/processRegistrationForm}" th:object="${user}" method="POST" class="form-horizontal">
<!-- User name -->
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" th:field:="*{userName}" placeholder="Username (*)" class="form-control" />
</div>
<!-- Password -->
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input type="text" th:field:="*{password}" placeholder="First Name (*)" class="form-control" />
</div>
控制器代码
@GetMapping("/showRegistrationForm")
public String showMyRegistrationPage(
Model theModel) {
theModel.addAttribute("user", new UserRegistrationDto());
return "Login/registration-form";
}
@PostMapping("/processRegistrationForm")
public String processRegistrationForm(
@Valid @ModelAttribute ("user") UserRegistrationDto userDto,
BindingResult theBindingResult,
Model theModel) {
String userName = userDto.getUserName();
logger.info("Processing registration form for: " + userName);
// form validation
if (theBindingResult.hasErrors()){
return "Login/registration-form";
}
// check the database if user already exists
User existing = userService.findByUserName(userName);
if (existing != null){
theModel.addAttribute("user", new UserRegistrationDto());
theModel.addAttribute("registrationError", "User name already exists.");
logger.warning("User name already exists.");
return "Login/registration-form";
}
// create user account
userService.save(userDto);
logger.info("Successfully created user: " + userName);
return "redirect:/loginPage?rSuccess";
}
DTO代码
public class UserRegistrationDto {
@NotNull(message = "is required")
@Size(min = 1, message = "is required")
private String userName;
@NotNull(message = "is required")
@Size(min = 1, message = "is required")
private String password;
实体代码
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "username")
private String userName;
@Column(name = "password")
private String password;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "users_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Collection<Role> roles;
public User() {
}
public User(String userName, String password, String firstName, String lastName, String email) {
this.userName = userName;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public User(String userName, String password, String firstName, String lastName, String email,
Collection<Role> roles) {
this.userName = userName;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.roles = roles;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
堆栈跟踪指向方法周围的问题:
@GetMapping("/showRegistrationForm")
public String showMyRegistrationPage(
&读取
An error happened during template parsing (template: "class path resource [templates/Login/registration-form.html]")
Caused by: java.lang.IllegalArgumentException: Attribute name cannot be null or empty
仅在使用 th:field:=“ {}” * 时发生,并且在将页面保留为.jsp
时效果很好。
我看不到代码有问题。 会有人知道是什么原因造成的吗?
我曾尝试从DTO
删除验证,因为它是由验证引起的,但错误消息没有改变。
答案 0 :(得分:0)
我认为问题在于这两行:
<input type="text" th:field:="*{userName}" placeholder="Username (*)" class="form-control" />
<input type="text" th:field:="*{password}" placeholder="First Name (*)" class="form-control" />
由于您在:
之后放置了“ th:field
”列,因此可能会给您带来错误。所以这两行应该是这样的:
<input type="text" th:field="*{userName}" placeholder="Username (*)" class="form-control" />
<input type="text" th:field="*{password}" placeholder="First Name (*)" class="form-control" />
由于百里香不能很好地解释问题或有问题的行号,所以找到问题真的很头疼。只是说“属性名称不能为Null或为空”,而您只是在搜索空属性。