簡體   English   中英

無法正確綁定百里香模板和彈簧靴

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

我正在使用Spring Security和thymeleaf在Spring Boot中進行注冊,但是由於某種原因, userForm.getUsername()在POST期間給出了空值(請參見下面的代碼)

人.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

@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

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

我使用th:field="${userForm.username}"正確綁定,但是在這里找不到問題所在。

您必須將模型對象放入如下形式

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

並將getter和setter添加到Person類

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM