Springs形成空值

时间:2017-01-04 15:19:25

标签: spring forms spring-mvc

我的表单在春天有一个小问题,它是一个带标题,用户名和密码的简单表单,验证工作好像我尝试提交一个空表单我得到一个错误但是如果我输入值并提交表单,它已提交但显示插入的值为“null”。

域:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String title;

    @NotNull
    @Size(min=2, max=30)
    private String username;

    @NotNull
    @Size(min=6)
    private String password;

    public String getTitle() { return title; }

    public String getUsername() { return username; }

    public String getPassword() { return password; }

    public Long getId() { return id; }

    public void setId(Long id) {
        this.id = id;
    }

    public void setUsername(String username) { this.username = username; }

    public void setPassword(String password) { this.password = password; }

    public void setTitle(String title) { this.title = title; }

    public List<Note> getNotes() {
        return notes;
    }

    @OneToMany
    private List<Note> notes = new ArrayList<Note>();
}

形式:

<form action="#" th:action="@{/create/user}" th:object="${user}" method="post">
    <input type="hidden" th:field="*{id}" />
    <p>Title: <input type="text" th:field="*{title}" /></p>
    <p>Username: <input type="text" th:field="*{username}" /></p>
    <p>Password: <input type="password" th:field="*{password}" /></p>
    <p><input type="submit" value="Submit" /></p>
</form>

<div class="form-group">
    <ol class="list-group">
        <li class="list-group-item list-group-item-info" th:each="user : ${user}">
            <a href="#" th:href="@{/note/}+${user.id}"><span th:text=" ${user.title} + ${user.username} + ${user.password} "></span></a>
            <a href="#" th:href="@{/delete/user/}+${user.id}" class="pull-right">delete</a>
        </li>
    </ol>
</div>

控制器:

@Controller
public class UserController {
    @Autowired
    protected UserService userService;

    @RequestMapping(value = "/create/user", method = RequestMethod.POST)
    public String createUser(Model model, @Valid @ModelAttribute("user") User user, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            model.addAttribute("user", user);
            model.addAttribute("users", userService.findAll());
            model.addAttribute("type", "danger");
            model.addAttribute("message", "Please fill in all the fields" +
                    "Username needs to be at least 2 characters" +
                    "Passwords needs to contain at least 6 characters");
            return "user";
        }

        userService.save(user);

        model.addAttribute("user", new User());
        model.addAttribute("cards", userService.findAll());

        model.addAttribute("type", "success");
        model.addAttribute("message", "A new user has been added");

        return "user";
    }

enter image description here

如果需要任何其他代码请告诉我,我仍然是Spring的新手

1 个答案:

答案 0 :(得分:1)

通过查看代码发现错误:

在输出注册用户的表格中,它是:

<li class="list-group-item list-group-item-info" th:each="user : ${user}">

虽然它应该是:

<li class="list-group-item list-group-item-info" th:each="user : ${users}">