在spring中从表单中获取null值

问题描述 投票:0回答:1

我想在春天创建一个注册和登录表单。我的问题是我在登记时获得nullusernamepassword值。

我的控制器:

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>

当我打印出用户名到控制台时,我得到null。

我的用户和用户形式模型如下:

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

用户窗体:

public class UserForm {

    private String username;
    private String password;


    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;
    }
}
java spring spring-mvc spring-boot thymeleaf
1个回答
0
投票

th:feild的错字。

将其更改为字段

<input type="text" th:field="*{username}" class="form-control" placeholder="Ex: John96">
© www.soinside.com 2019 - 2024. All rights reserved.