1

我有一个非常简单的用户设置表单:

<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].questionIdanswer[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 中包含的内容。很明显,它显示为answersquestionIdanswer)返回了值。为什么 spring 不调用setAnswers()模型对象(记住它setTimeZone() 调用了)?

4

1 回答 1

1

这原来是 viewState 中的活页夹的问题。看起来这应该有效:

<binder>
  <binding property="answers" />
  <binding property="timeZone" />
</binder>

因为answers是绑定模型对象的属性。但是,我实际上必须指定列表中元素的属性:

<binder>
  <binding property="answers[0].questionId" />
  <binding property="answers[0].answer" />
  <binding property="answers[1].questionId" />
  <binding property="answers[1].answer" />
  <binding property="answers[2].questionId" />
  <binding property="answers[2].answer" />
  <binding property="answers[3].questionId" />
  <binding property="answers[3].answer" />
  <binding property="answers[4].questionId" />
  <binding property="answers[4].answer" />
  <binding property="timeZone" />
</binder>

对我来说,这将起作用(目前),因为我有固定大小的元素数量。必须包含列表中每个元素的每个属性有点痛苦,但它有效。有为此提交的错误/功能请求应该使它更容易,但在那之前......

于 2012-04-26T22:13:39.543 回答