在Spring中从表单获取空值

时间:2018-09-03 10:15:36

标签: java spring spring-mvc spring-boot thymeleaf

我正在尝试在春季创建注册和登录表格。我的问题是我在注册时获得了nullusername的{​​{1}}值。

我的控制器:

password

我尝试单独使用用户对象(表单的获取模型属性)进行操作,并尝试为表单(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"; } } )创建自定义对象,但仍然无法正常工作。

我的百里香模板如下:

userForm

当我打印出用户名进行控制台时,我得到的是空值。

我的用户模型和用户表单如下:

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

UserForm:

@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;
    }
}

1 个答案:

答案 0 :(得分:0)

th:feild中有错字。

将其更改为字段

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