富表对象的Spring表单验证

时间:2016-08-04 11:32:17

标签: java spring forms validation

我尝试使用百日咳和弹簧(启动)从表单验证用户的输入。

这是我的表格:

<!DOCTYPE html>
<html xmlns:th="http://www.thymealeaf.org">
<head>
<meta charset="ISO-8859-1"></meta>
<title>Add new owner to database</title>
</head>
<body>
    <div id="form">
        <form th:action="@{/addOwner.do}" th:object="${Owner}" method="post">
            <table>
                <tr>
                    <td>First name:</td>
                    <td><input type="text" th:field="*{firstName}" /></td>
                    <td th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}">fieldError</td>
                </tr>
                <tr>
                    <td>Last name:</td>
                    <td><input type="text" th:field="*{lastName}" /></td>
                    <td th:if="${#fields.hasErrors('lastName')}" th:errors="*{lastName}">fieldError</td>
                </tr>
                <tr>
                    <td>Pet name:</td>
                    <td><input type="text" th:field="*{pets[0].petName}" /></td>
                    <td th:if="${#fields.hasErrors('pets[0].petName')}" th:errors="*{pets[0].petName}">fieldError</td>
                </tr>
                <tr>
                    <td>Pet name:</td>
                    <td><input type="text" th:field="*{pets[1].petName}" /></td>
                    <td th:if="${#fields.hasErrors('pets[1].petName')}" th:errors="*{pets[1].petName}">fieldError</td>
                </tr>
                <tr>
                    <td>Pet name:</td>
                    <td><input type="text" th:field="*{pets[2].petName}" /></td>
                    <td th:if="${#fields.hasErrors('pets[2].petName')}" th:errors="*{pets[2].petName}">fieldError</td>
                </tr>
                <tr>
                    <td>Number:</td>
                    <td><input type="text" th:field="*{phones[0].number}" /></td>
                    <td th:if="${#fields.hasErrors('phones[0].number')}" th:errors="*{phones[0].number}">fieldError</td>
                </tr>
                <tr>
                    <td>Number:</td>
                    <td><input type="text" th:field="*{phones[1].number}" /></td>
                    <td th:if="${#fields.hasErrors('phones[1].number')}" th:errors="*{phones[1].number}">fieldError</td>
                </tr>
                <tr>
                    <td>Number:</td>
                    <td><input type="text" th:field="*{phones[2].number}" /></td>
                    <td th:if="${#fields.hasErrors('phones[2].number')}" th:errors="*{phones[2].number}">fieldError</td>
                </tr>
                <tr>
                    <td>Number:</td>
                    <td><input type="text" th:field="*{phones[3].number}" /></td>
                    <td th:if="${#fields.hasErrors('phones[3].number')}" th:errors="*{phones[3].number}">fieldError</td>
                </tr>
                <tr>
                    <td><input type="submit" value="add" /></td>
                </tr>
            </table>
        </form>
        <a href="/ ">Back</a>
    </div>
</body>
</html>

这是实体:

@Entity
public class Owner {

    @Id
    @GeneratedValue
    private int id;
    @Column(name = "first_name")
    @NotNull(message="{NotNull}")
    @Size(min=2,max=15,message="{Size}")
    private String firstName;
    @NotNull(message="{NotNull}")
    @Size(min=2,max=15,message="{Size}")
    @Column(name = "last_name")
    private String lastName;
    @Size(min=1, message="{Size}")
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
    private List<Phone> phones;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
    private List<Pet> pets;

这是控制器方法:

@RequestMapping(value="addOwner.do", method = RequestMethod.POST)
    public String addOwnerDo(@Valid @ModelAttribute(value = "Owner") Owner owner, BindingResult result){

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

        List<Phone> phones = new ArrayList<>();
        List<Pet> pets = new ArrayList<>();

            for(int i = 0;i<owner.getPhones().size();i++){
                if(owner.getPhones().get(i).getNumber() != ""){
                    owner.getPhones().get(i).setOwner(owner);
                    phones.add(owner.getPhones().get(i));
                    }
            }
            for(int i = 0;i<owner.getPets().size();i++){
                if(owner.getPets().get(i).getPetName() != ""){
                    owner.getPets().get(i).setOwner(owner);
                    pets.add(owner.getPets().get(i));
                    }
            }
        owner.setPets(pets);
        owner.setPhones(phones);
        ownerService.add(owner);
        return "redirect:addOwner";
    }

SO:

  1. 字段验证
    • 的firstName
    • lastName的 工作正常,如果输入错误,我会在表单中显示错误消息。
  2. 但是当我输入错误的“手机”时,这意味着我没有在该字段中插入任​​何内容。错误也会发生,但它不是表单中的错误,而是带有stackstrace错误的简单“whielabel”错误页面。

    我确实使用过调试器,当我输入错误的“phones”字段时,BindingResult对象似乎没有错误。我怎么能解决这个问题?

0 个答案:

没有答案