habuma / spring-in-action-5-samples

Home for example code from Spring in Action 5.
Apache License 2.0
1.21k stars 1.04k forks source link

Charpter 4 register form object binding #65

Open clnmahone opened 4 years ago

clnmahone commented 4 years ago

There is no@ModelAttribut or th:object={} in register.html form and the related controller, but the object binding works. Is there anyboday kowns why?

The register.html file:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>Taco Cloud</title>
  </head>

  <body>
    <h1>Register</h1>
    <img th:src="@{/images/TacoCloud.png}"/>    

    <form method="POST" th:action="@{/register}" id="registerForm">

        <label for="username">Username: </label>
        <input type="text" name="username"/><br/>

        <label for="password">Password: </label>
        <input type="password" name="password"/><br/>

        <label for="confirm">Confirm password: </label>
        <input type="password" name="confirm"/><br/>

        <label for="fullname">Full name: </label>
        <input type="text" name="fullname"/><br/>

        <label for="street">Street: </label>
        <input type="text" name="street"/><br/>

        <label for="city">City: </label>
        <input type="text" name="city"/><br/>

        <label for="state">State: </label>
        <input type="text" name="state"/><br/>

        <label for="zip">Zip: </label>
        <input type="text" name="zip"/><br/>

        <label for="phone">Phone: </label>
        <input type="text" name="phone"/><br/>

        <input type="submit" value="Register"/>
    </form>

  </body>
</html>

The registerController.java file:

package tacos.security;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import tacos.data.UserRepository;

@Controller
@RequestMapping("/register")
public class RegistrationController {

  private UserRepository userRepo;
  private PasswordEncoder passwordEncoder;

  public RegistrationController(
      UserRepository userRepo, PasswordEncoder passwordEncoder) {
    this.userRepo = userRepo;
    this.passwordEncoder = passwordEncoder;
  }

  @GetMapping
  public String registerForm() {
    return "registration";
  }

  @PostMapping
  public String processRegistration(RegistrationForm form) {
    userRepo.save(form.toUser(passwordEncoder));
    return "redirect:/login";
  }

}
MylQl commented 4 years ago

你也学到这一节了,加个wx交流一下?

MylQl commented 4 years ago

经过一番实验,我虽然不明白是什么机制导致这种结果,但也算弄明白为什么不需要@ModelAttributes也能执行数据绑定了。

clnmahone commented 4 years ago

根据的name属性绑定的. @MylQl

MylQl commented 4 years ago

根据的name属性绑定的. @MylQl

是这样的没错,最重要的是这个@Entity注解,它的存在与否决定了是否使用@ModelAttributes。但我并不清楚这里面的运行机制。

olegkamuz commented 4 years ago

Maybe answer is https://github.com/habuma/spring-in-action-5-samples/issues/78