我有一个非常简单的用户设置表单:
<form:form method="post" id="fm1" cssClass="fm-v clearfix" commandName="${commandName}" htmlEscape="true">
<div class="row fl-controls-left">
<spring:message code="screen.userSettings.label.timeZone.accesskey" var="timeZoneAccessKey" />
<label for="timeZone" class="fl-label"><spring:message code="screen.userSettings.label.timeZone" /></label>
<form:select id="timeZone" path="timeZone" accesskey="${timeZoneAccessKey}">
<form:options items="${user.supportedTimeZones}" itemLabel="label" itemValue="id" />
</form:select>
<c:forEach items="${user.answers}" var="answer" varStatus="loop">
<div class="row fl-controls-left">
<form:select path="answers[${loop.index}].questionId">
<option value="-1"><spring:message code="screen.userSettings.question.selectOne" /></option>
<form:options items="${user.supportedQuestions}" itemLabel="question" itemValue="id" />
</form:select>
<form:input path="answers[${loop.index}].answer" size="30" autocomplete="false" htmlEscape="true" type="text" cssClass="required" cssErrorClass="error"/>
</div>
</c:forEach>
</div>
</form:form>
基本上,您可以设置您的时区,并提供忘记密码问题列表的答案。绑定到这个表单的模型对象基本上是这样的:
public class User implements Serializable {
private static final long serialVersionUID = 8974875234954842283L;
private List<Answer> answers;
private List<Question> supportedQuestions;
private List<TimeZone> supportedTimeZones;
private String timeZone;
public User() {
credentials = new UsernamePasswordCredentials();
}
public List<Answer> getAnswers() {
return answers;
}
public List<Question> getSupportedQuestions() {
return supportedQuestions;
}
public List<TimeZone> getSupportedTimeZones() {
return supportedTimeZones;
}
public String getTimeZone() {
return timeZone;
}
public void setAnswers( List<Answer> answers ) {
this.answers = answers;
}
public void setSupportedQuestions( List<Question> supportedQuestions ) {
this.supportedQuestions = supportedQuestions;
}
public void setSupportedTimeZones( List<TimeZone> supportedTimeZones ) {
this.supportedTimeZones = supportedTimeZones;
}
public void setTimeZone( String timeZone ) {
this.timeZone = timeZone;
}
...
}
表格显示得很好。它提供了一个下拉菜单,显示所有受支持的时区,后跟几行问题/答案对,问题是包含所有受支持问题的下拉菜单。当我提交回表单时,处理失败,因为List<Answer> answers
没有使用表单中的值进行更新。我的问题有两个方面,首先,我做错了什么?其次,将来我应该如何调试此类映射问题?
我尝试使用调试器单步执行代码。setTimeZone()
使用表单中的值调用该方法,但该setAnswers()
方法不是。setQuestionId()
类的orsetAnswer()
方法也不是Answer
。我查看了该Request
对象,它具有所有answer[x].questionId
和answer[x].answer
参数的键,但不确定值是什么。您建议如何追踪此类问题?
- - - - - - - - - - - - - 更新 - - - - - - - - - - - - ---------
我刚刚从调试器中提取了这个:
map[
'answers[4].questionId' -> 'cn=honeymoon,ou=questions,dc=company'
'answers[2].questionId' -> 'cn=honeymoon,ou=questions,dc=company'
'answers[0].questionId' -> 'cn=firstPet,ou=questions,dc=company'
'lt' -> 'LT-786e9b77-efbd-f302-062e-90364cc4634aZe1s2'
'answers[1].answer' -> 'qwer'
'answers[3].answer' -> 'poiu'
'submit' -> 'Save'
'answers[3].questionId' -> 'cn=honeymoon,ou=questions,dc=company'
'answers[1].questionId' -> 'cn=mothersMaiden,ou=questions,dc=company'
'answers[2].answer' -> 'zxcv'
'answers[4].answer' -> 'lkjh'
'timeZone' -> 'America/New_York'
'_eventId' -> 'submit'
'answers[0].answer' -> 'asdf'
]
它是 requestParameterMap 中包含的内容。很明显,它显示为answers
(questionId
和answer
)返回了值。为什么 spring 不调用setAnswers()
模型对象(记住它setTimeZone()
被调用了)?