简体   繁体   English

无法正确绑定百里香模板和弹簧靴

[英]Unable to bind thymeleaf template and spring boot correctly

I am working on registration in spring boot with spring security and thymeleaf but for some reason the userForm.getUsername() gives null value during POST (see code below) 我正在使用Spring Security和thymeleaf在Spring Boot中进行注册,但是由于某种原因, userForm.getUsername()在POST期间给出了空值(请参见下面的代码)

Person.java 人.java

@Entity@Table(name = "person")
public class Person {

    @Id@Column(name = "username")
    private String username;

    @Column(name = "mobile")
    private String mobile;

    @JsonIgnore@Column(name = "password")
    private String password;

    @Transient@JsonIgnore
    private String passwordConfirm;

    @ManyToMany(cascade = CascadeType.ALL)@JoinTable(name = "bookandperson", joinColumns = @JoinColumn(name = "username"), inverseJoinColumns = @JoinColumn(name = "bookName"))
    private List < Book > listOfBooks = new ArrayList < Book > ();
}  

PersonController.java PersonController.java

@Controller
public class PersonController {
    @Autowired
    private UserService userService;

@Autowired
private SecurityService securityService;

@Autowired
private UserValidator userValidator;

@RequestMapping(value = "/registration", method = RequestMethod.GET)
public String registration(Model model, Principal user) {
    if (user != null) {
        return "redirect:/";
    }
    model.addAttribute("userForm", new Person());

    return "registration";
}

@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registration(@ModelAttribute Person userForm, BindingResult bindingResult, Model model) {
    System.out.println(userForm.getUsername()); //This is giving null value
    userValidator.validate(userForm, bindingResult);

    if (bindingResult.hasErrors()) {
        System.out.println("binding result has errors");
        return "registration";
    }

    userService.save(userForm);

    securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());

    return "redirect:/";
}

registration.html registration.html

<head>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>

<body>

    <nav th:replace="common/navbar :: common-navbar"/>

    <div class="container mt-4">
    <div class="row justify-content-center">
    <div class="col-md-6 col-md-offset-6">
    <form th:action="@{/registration}" method="post">
      <div class="form-group row">
        <label for="inputUsername" class="col-sm-2 col-form-label">Username</label>
        <div class="col-md-6">
            <input type="text" class="form-control" th:field="${userForm.username}" id="inputUsername" value="" placeholder="Username">
        </div>
      </div>
      <div class="form-group row">
        <label for="mobile" class="col-sm-2 col-form-label">Mobile</label>
        <div class="col-md-6">
            <input type="text" class="form-control" th:field="${userForm.mobile}" id="inputMobile" placeholder="Mobile">
        </div>
      </div>
      <div class="form-group row">
        <label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
        <div class="col-md-6">
            <input type="password" class="form-control" th:field="${userForm.password}" id="inputPassword" placeholder="Password">
        </div>
      </div>
      <div class="form-group row">
        <label for="inputPasswordConfirm" class="col-sm-2 col-form-label">Confirm Password</label>
        <div class="col-md-6">
            <input type="password" class="form-control" th:field="${userForm.passwordConfirm}" id="inputPasswordConfirm" placeholder="Password">
        </div>
      </div>
      <div class="form-group row">
       <div class="offset-2 col-sm-10">
        <button type="submit" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" class="btn btn-primary">Submit</button>
       </div>
       </div>

       <small th:text="${error}"></small>
    </form>
    </div>
    </div>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</body>

I binded correctly using th:field="${userForm.username}" but unable to find what is the problem here. 我使用th:field="${userForm.username}"正确绑定,但是在这里找不到问题所在。

you have to take the model object into the form as follows 您必须将模型对象放入如下形式

<form action="#" th:action="@{/registration}" th:object="${userForm}" method="post">

and add the getters and setters to the Person class 并将getter和setter添加到Person类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM