Java 在spring中从窗体获取空值

Java 在spring中从窗体获取空值,java,spring,spring-mvc,spring-boot,thymeleaf,Java,Spring,Spring Mvc,Spring Boot,Thymeleaf,我正在尝试在spring中创建一个注册和登录表单。我的问题是,我在注册时获得了username和password的null值 我的控制器: import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.val

我正在尝试在spring中创建一个注册和登录表单。我的问题是,我在注册时获得了
username
password
null

我的控制器:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.citrya.app.model.User;
import com.citrya.app.model.UserForm;
import com.citrya.app.service.SecurityService;
import com.citrya.app.service.UserService;
import com.citrya.app.validator.UserValidator;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private SecurityService securityService;

    @Autowired
    private UserValidator userValidator;

    @PostMapping("/registration")
    public String registration( @ModelAttribute("userForm") UserForm userForm, BindingResult result,Model model ) {

        System.out.println("User at controller post =" + userForm.getUsername() );

        User user = new User();
        user.setUsername(userForm.getUsername());
        user.setPassword(userForm.getPassword());

        userValidator.validate(user, result);

        if ( result.hasErrors() ) return "register";



        userService.save(user);


        securityService.autoLogin(userForm.getUsername(), userForm.getPassword() );

        return "redirect:/welcome";
    }

    @GetMapping("/registration")
    public String registration( Model model) {

        model.addAttribute("userForm", new UserForm() );

        return "register";
    }

    @RequestMapping ( value = "/login" ,method = RequestMethod.GET )
    public String login( Model model, String error, String logout ) {

        if ( error  != null ) {
            model.addAttribute("error", "Your username and password is invalid");   
        }

        if ( logout != null ) {
            model.addAttribute("message", "You have successfully logged out");
        }

        return "login";
    }

    @RequestMapping( value = { "/", "/welcome"} , method = RequestMethod.GET )
    public String welcome( Model model ) {
        return "welcome";
    }

}
我尝试单独使用用户对象(form get model属性)来执行此操作,并尝试为表单创建自定义对象(
userForm
),但它仍然不起作用

我的ThymileAF模板如下所示:

<html xmlns="http://www.thymeleaf.org" th:replace="~{ fragments/layout :: layout (~{::body}, 'CITRYA | Register') }">

<body>


    <div class = "container">

        <div class="jumbotron">
          <h1 class="display-4">Welcome to Citrya</h1>
          <p class="lead">Let us understand what you like and use that information to shove advertisements down your throats.</p>
          <hr class="my-4">
          <p>We are here for your information nothing else. Just kidding we are here to profit from you too.</p>
          <p class="lead">
            <a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a>
          </p>
        </div>

        <div class= "row">
            <div class= "col-md-4">
                <span th:text="${message}">MSG</span>
                <span th:text="${error}">ERR</span>
                <form th:action="@{/registration}" th:object="${userForm}" method="POST" >

                        <label  for="username" th:text="Username">Name </label>
                        <input type="text" th:feild="*{username}" class="form-control" placeholder="Ex: John96">


                        <label for="password" th:text="Password">Pass</label>
                        <input type="password" th:feild="*{password}" class="form-control" placeholder="Ex: John96">


                     <button type="submit" class="btn btn-primary">Register</button>
                </form>
            </div>
        </div>




    </div>

</body>

</html>
@Entity
@Table( name = "users")
public class User{


    private String username;
    private String password;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @ManyToMany
    @JoinTable(name = "user_tag", joinColumns = @JoinColumn( name = "user_id"), inverseJoinColumns = @JoinColumn(name = "tag_id"))
    private Set<Tag> tags;

    @OneToMany
    private Set<Tag> seenTags;

    @ManyToMany
    @JoinTable( name = "user_role", joinColumns = @JoinColumn( name = "user_id"), inverseJoinColumns = @JoinColumn( name= "role_id"))
    private Set<Role> roles;




    public Integer getId() {
        return id;
    }

    public void setId(Integer 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;
    }

    public Set<Tag> getTags(){
        return tags;
    }

    public void setTags(Set<Tag> tags) {
        this.tags = tags;
    }

    public Set<Role> getRoles(){
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }


    public Set<Tag> getSeenTags() {
        return seenTags;
    }

    public void setSeenTags( Set<Tag> seenTags ) {
        this.seenTags = seenTags;
    }
}

th:feild
中输入错误

将其更改为field

<input type="text" th:field="*{username}" class="form-control" placeholder="Ex: John96">


th:feild
应该是
th:field
我恨我自己。。
<input type="text" th:field="*{username}" class="form-control" placeholder="Ex: John96">