【问题标题】:Spring MVC Controller List<MyClass> as a parameterSpring MVC Controller List<MyClass> 作为参数
【发布时间】:2012-12-03 10:11:02
【问题描述】:

我有一个 Spring MVC 应用程序。 Jsp页面包含表单,提交。 我的控制器的方法如下:

@RequestMapping(value = "photos/fileUpload", method = RequestMethod.POST)
    @ResponseBody
    public String fileDetailsUpload(List<PhotoDTO> photoDTO,
                                    BindingResult bindingResult,
                                   HttpServletRequest request) {
        // in the point **photoDTO** is null
    }

我的班级是:

    public class PhotoDTO {
        private Boolean mainPhoto;
        private String title;
        private String description;
        private Long realtyId;

//getters/setters

但是,如果我写同样的方法,但是 param 只是我的对象:

    @RequestMapping(value = "photos/fileUpload", method = RequestMethod.POST)
        @ResponseBody
        public String fileDetailsUpload(PhotoDTO photoDTO,
                                        BindingResult bindingResult,
                                        HttpServletRequest request) {
// Everething is Ok **photoDTO** has all fields, which were fielded on the JSP

在这种情况下我应该怎么做?如果我在 Jsp 上有很多 PhotoDTO 对象 (15) 我怎样才能在服务器部分接收所有这些对象?

【问题讨论】:

    标签: spring list model-view-controller parameters controller


    【解决方案1】:

    页面应将列表中每个PhotoDTO 的参数以适当的格式传递回服务器。在这种情况下,您需要使用 status var 来使用 name 属性指定列表中每个对象的索引。

    <form>
    <c:forEach items="${photoDTO}" var="photo" varStatus="status">
            <input name="photoDTO[${status.index}].title" value="${photo.title}"/>
            <input name="photoDTO[${status.index}].description" value="${photo.description}"/>
    </c:forEach>
    </form>
    

    【讨论】:

    • 无论如何都可以使用客户端脚本而不是使用 JSTL 在纯 html 中完成。我需要在客户端上动态添加列。我需要知道的是我应该将什么作为输入对象的name 属性传递,以便将它们作为控制器参数中的列表。
    猜你喜欢
    • 2011-09-11
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多