正在将JSP转换为Thymleaf:属性名称不能为Null或空

g6ll5ycj  于 2022-12-07  发布在  其他
关注(0)|答案(3)|浏览(230)

我目前正在使用Thymeleaf将一些.jsp页面转换为HTML。我已经成功转换了其中的一些页面,但是在检查任何带有表单标记的页面时,我收到以下错误:

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(

读取方式(& R)

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中删除验证,因为它很有可能是由它引起的,但错误消息没有改变。

iugsix8n

iugsix8n1#

我想问题是关于这两行的:

<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" />

由于thymeleaf没有很好地解释问题或有问题的行号,因此要找到问题就变得非常头痛了。它只是说“属性名不能为空或空”,而您只是在搜索空属性。

eulz3vhy

eulz3vhy2#

我遇到了同样的问题,我只是把错误减少了1倍。所以我对每一个输入和每一个表单都进行了注解。问题就在这里

<form action="#" th:action="@{/register/processRegistrationForm}" th:object="${user}" method="POST" class="form-horizontal">

th:object="${user}",也许这个技巧可以帮助你一点,因为这个问题还没有解决。

olhwl3o2

olhwl3o23#

也许晚了,但我想我明白问题出在哪里了。

th:field:="*{password}"

有一个:太多了
应该是

th:field="*{password}"

相关问题