无法将Http Form数据绑定到HashMap集合

时间:2019-02-15 13:56:53

标签: grails

我正在尝试将表单数据发布为

  

“ student.id = 1&courses [10] .course.id = 10”

我们试图绑定的命令对象类如下...

class StudentEnrollmentCmd {
    Student student
    Map<String,CourseCmd> courses;
}

class CourseCmd {
    CourseDomain course
}

class CourseDomain {
    Long id
}

希望它会绑定到

  

“ StudentEnrollmentCmd->课程-> course.id”

它似乎在grails 2.2.4中有效,但在3.3.7中失败,具有以下例外情况

  

没有这样的属性:班级的course.id:student.CourseCmd

这是说明问题的测试案例

void 'databinding from request parameters'() {
        given:
        // request with simple formdata: student.id=1&courses[10].course.id=10
        MockHttpServletRequest request = buildMockRequestWithParams('POST',['student.id':'1','courses[10].course.id':'10']);
        DataBindingSource source = bindingSourceCreator.createDataBindingSource(null,null,request);

        // databinder & command object
        def binder = new SimpleDataBinder()
        def obj = new StudentEnrollmentCmd()

        when:
        binder.bind(obj,source)

        then:
        // this should not throw an exception, but throws an exception
        MissingPropertyException ex = thrown()
        System.out.println ( "Exception again:" + ex.message );

        // the following should work, but does not work
        obj.student.id == 1
        obj.courses['10'].course.id == 10
    }

这是完整规格的链接... https://github.com/swzaidi/sample/blob/master/grails3.3.7/src/test/groovy/student/DataBindingSpec.groovy

正在寻找有关如何传递表单数据的帮助,以便将其正确绑定到上述命令对象。

0 个答案:

没有答案