我有一个看似奇怪的问题,我得到一个IllegalStateException,说明:BindingResult和bean名称'custSection'的普通目标对象都不可用作请求属性
我已将问题分离到带有id =“sectionsName”的文本输入...它之前被命名为“sectionName”,但我认为这可能是名称冲突,但我错了。
在任何情况下,当我从输入中取出th:field="${custSection.sectionName}"
时,一切都运行良好,我没有得到500错误。
奇怪的是,有${custSection}
param的其他引用可以很好地嵌套在我的for each
循环中...例如,在我引用的<span>
内部th:text="${custSection.sectionName}"
<div id="sections">
<div th:each="custSection : ${course.sections}" class="panel panel-primary">
<div class="panel-heading">
<div class="form-group">
<label class="control-label col-xs-12 col-md-2" for="sectionsName">Section Name:</label>
<div class="col-xs-12 col-md-5">
<div class="control-label pull-left label-input" id="_sectionsName"><span id="_sectionsNameText" th:text="${custSection.sectionName}" style="margin-right: 1em;"></span><span class="glyphicon glyphicon-pencil" id="_sectionsNameEditIcon"></span>
</div>
<div class="input-group">
<input class="form-control hidden" id="sectionsName" type="text" th:field="${custSection.sectionName}" maxlength="60" />
<span class="btn btn-default input-group-addon hidden" id="sectionsNameOK"><i class="glyphicon glyphicon-ok"></i></span>
<span class="btn btn-default input-group-addon hidden" id="sectionsNameCancel"><i class="glyphicon glyphicon-remove"></i></span>
</div>
</div>
</div>
</div>
<div th:each="lesson : ${custSection.lessons}" class="panel-body">
<a th:href="@{{courseId}/sections/{sectionId}/lessons/{lessonId}(courseId=${course.id},sectionId=${custSection.id},lessonId=${lesson.id})}" th:text="${lesson.title}"></a>
</div>
<form th:action="@{{courseId}/sections/{sectionId}/lessons(courseId=${course.id},sectionId=${custSection.id})}" method="post">
<button class="btn btn-default">Add a Lesson</button>
</form>
</div>
</div>
<form th:action="@{{courseId}/sections(courseId=${course.id})}" method="post">
<button id="addSection" class="btn btn-info">Add a Section</button>
</form>
在我的代码中,Course
有许多Section
s。
我在模型上放置了一个course
对象,然后我在section
内迭代每个course
。
我唯一能想到的是,你不允许对模型上父对象的任何子项使用th:field
引用。
答案 0 :(得分:0)
事实证明,这是一个问题,因为我正试图用百里香来更新父母的孩子。
以下是解释流程的帖子:http://justinrodenbostel.com/2014/05/12/part-3-form-binding/
我通过添加stat
变量来解决我的代码,以跟踪for each
循环中的索引状态,然后通过父级索引引用子级,如下所示:
th:field="${course.sections[__${stat.index}__].sectionName}"
以下是完整代码解决方案的片段:
<div th:each="custSection, stat : ${course.sections}" class="panel panel-primary">
<div class="panel-heading">
<div class="form-group">
<label class="control-label col-xs-12 col-md-2" for="sectionsName">Section Name:</label>
<div class="col-xs-12 col-md-5">
<div class="control-label pull-left label-input" id="_sectionsName"><span id="_sectionsNameText" th:text="${custSection.sectionName}" style="margin-right: 1em;"></span><span class="glyphicon glyphicon-pencil" id="_sectionsNameEditIcon"></span>
</div>
<div class="input-group">
<input class="form-control hidden" id="sectionsName" type="text" th:field="${course.sections[__${stat.index}__].sectionName}" maxlength="60" />
<span class="btn btn-default input-group-addon hidden" id="sectionsNameOK"><i class="glyphicon glyphicon-ok"></i></span>
<span class="btn btn-default input-group-addon hidden" id="sectionsNameCancel"><i class="glyphicon glyphicon-remove"></i></span>
</div>
</div>
</div>
</div>
<div th:each="lesson : ${custSection.lessons}" class="panel-body">
<a th:href="@{{courseId}/sections/{sectionId}/lessons/{lessonId}(courseId=${course.id},sectionId=${custSection.id},lessonId=${lesson.id})}" th:text="${lesson.title}"></a>
</div>
<form th:action="@{{courseId}/sections/{sectionId}/lessons(courseId=${course.id},sectionId=${custSection.id})}" method="post">
<button class="btn btn-default">Add a Lesson</button>
</form>
</div>
答案 1 :(得分:0)
你是对的。 th:字段要求您正在使用的对象直接在模型上。像你一样使用迭代变量,但是我认为最简单的解决方法是完全放弃th:field并指定输入元素的所有属性。 th:由于某种原因(?),value不使用与th:field相同的绑定,并按预期工作。
<div th:each="custSection : ${course.sections}" class="panel panel-primary">
<div class="panel-heading">
<div class="form-group">
<label class="control-label col-xs-12 col-md-2" for="sectionsName">Section Name:</label>
<div class="col-xs-12 col-md-5">
<div class="control-label pull-left label-input" id="_sectionsName"><span id="_sectionsNameText" th:text="${custSection.sectionName}" style="margin-right: 1em;"></span><span class="glyphicon glyphicon-pencil" id="_sectionsNameEditIcon"></span>
</div>
<div class="input-group">
<input class="form-control hidden" id="sectionName" name="sectionName" type="text" th:value="${custSection.sectionName}" maxlength="60" />
<span class="btn btn-default input-group-addon hidden" id="sectionsNameOK"><i class="glyphicon glyphicon-ok"></i></span>
<span class="btn btn-default input-group-addon hidden" id="sectionsNameCancel"><i class="glyphicon glyphicon-remove"></i></span>
</div>
</div>
</div>
</div>
<div th:each="lesson : ${custSection.lessons}" class="panel-body">
<a th:href="@{{courseId}/sections/{sectionId}/lessons/{lessonId}(courseId=${course.id},sectionId=${custSection.id},lessonId=${lesson.id})}" th:text="${lesson.title}"></a>
</div>
<form th:action="@{{courseId}/sections/{sectionId}/lessons(courseId=${course.id},sectionId=${custSection.id})}" method="post">
<button class="btn btn-default">Add a Lesson</button>
</form>
</div>