从列表中以form:option标记传递对象

时间:2019-01-30 17:03:19

标签: java spring spring-mvc jsp

我有我的DTO,要在其中分配来自form:option标签的列表中的对象。 DTO

public class UserDTO implements Serializable {

    private Group group;
    @JsonProperty("phone")
    private String phone;
    @JsonProperty("password")
    private String password;
    @JsonProperty("confirmPassword")
    private String confirmPassword;
    -- Getters and setters
}

JSP

                         <spring:bind path="group">
                                <div class="form-group inner-addon left-addon ${status.error ? 'has-error' : ''}">
                                    <form:select path="group" class="form-control">
                                        <c:forEach items="${groups}" var="group">
                                            <form:option value="${group}" label="${group.name}"/>
                                        </c:forEach>
                                    </form:select>
                                    <form:errors path="group"/>
                                </div>
                            </spring:bind>

控制器

@RequestMapping(value = "/finish-registration", method = RequestMethod.GET)
    public String finishRegistration(Model model) {
        model.addAttribute("groups", GROUP_LIST);
        model.addAttribute("UserDTO", new UserDTO());
        return "finish-registration";
    }

    @RequestMapping(value = "/finish-registration", method = RequestMethod.POST)
    public String finishRegister(@ModelAttribute("UserDTO") UserDTO userDTO, BindingResult result, Model model, Locale locale) {
        System.out.println(userDTO.toString());
        studentValidator.validate(userDTO, result);
        if (result.hasErrors()) {
            return "finish-registration";
        }
}

结果我有错误

Caused by: javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String

我不明白为什么值没有作为对象映射到DTO字段。

0 个答案:

没有答案