为什么存在 DateTimeField 时不调用表单的 onSubmit 方法?

Why is the form's onSubmit method not called when a DateTimeField is present?

有关 Apache Wicket 的问题:我有一个包含多个文本输入字段的表单。在我添加 DateTextField 之前,它工作正常。重写的方法 onSubmit() 不再被调用。我查看了 Wicket 示例,但看不出我的代码有什么重大差异。

这是 html 代码:

<html xmlns:wicket="http://wicket.apache.org">
 <head>
  <title>Students</title>
 </head>
 <body>
  <div class="container">
   <form id="createStudent" wicket:id="createStudent">
    <div>
     <span id="lastNameLabel"><wicket:message key="lastNameLabel" /></span>
     <input id="lastName" wicket:id="lastName" type="text" />
    </div>
    <div>
     <span id="firstNameLabel"><wicket:message key="firstNameLabel" /></span>
     <input id="firstName" wicket:id="firstName" type="text" />
    </div>
    <div>
     <span id="dateOfBirthLabel"><wicket:message key="dateOfBirthLabel" /></span>
     <input id="dateOfBirth" wicket:id="dateOfBirth" type="text" />
    </div>
    <div>
     <input id="submit" type="submit" value="" wicket:message="value:submitLabel"/>
    </div>
   </form>
  </div>
 </body>
</html>

对应的java文件:

package it.foo;

import java.util.Locale;

import org.apache.wicket.Session;
import org.apache.wicket.datetime.StyleDateConverter;
import org.apache.wicket.datetime.markup.html.form.DateTextField;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;

public class CreateStudentForm extends Form<Student> {

private static final long serialVersionUID = 1L;

private String lastName;
private String firstName;
private DateTime dateOfBirth;

@SpringBean
StudentDao studentDao;

public CreateStudentForm(String id) {
    super(id);
    setDefaultModel(new CompoundPropertyModel<>(this));
    add(new TextField<String>("lastName"));
    add(new TextField<String>("firstName"));

    final DateTextField dateOfBirthField = new DateTextField("dateOfBirth", new StyleDateConverter("S-", true)) {
        private static final long serialVersionUID = 1L;

        @Override
        public Locale getLocale() {
            return Session.get().getLocale();
        }
    };

    dateOfBirthField.setType(DateTime.class);
    add(dateOfBirthField);

}

@Override
protected void onSubmit() {
    // does not get called as soon as the DateTextField is present. Works fine otherwise. 
    Student student = new Student(this.lastName, this.firstName, this.dateOfBirth);
    studentDao.store(student);
    setResponsePage(StudentsPage.class);
}

}

我查看了在浏览器中呈现的 html。如果 dateTextField 不存在,它看起来像这样:

<html>
 <head>
  <title>Students</title>
 </head>
 <body>
  <div class="container">
   <form id="createStudent" method="post" action="./?2-1.IFormSubmitListener-createStudent"><div style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden"><input type="hidden" name="createStudent_hf_0" id="createStudent_hf_0" /></div>
    <div>
     <span id="lastNameLabel">Nachname: </span>
     <input id="lastName" type="text" value="" name="lastName"/>
    </div>
    <div>
     <span id="firstNameLabel">Vorname:</span>
     <input id="firstName" type="text" value="" name="firstName"/>
    </div>
     <!-- <div>
     <span id="dateOfBirthLabel"><wicket:message key="dateOfBirthLabel" /></span>
     <input id="dateOfBirth" wicket:id="dateOfBirth" type="text" />
    </div> -->
    <div>
     <input id="submit" type="submit" value="Schüler anlegen"/>
    </div>
   </form>
  </div>
 </body>
</html>

只要 dateTextField 存在,表单就会有额外的分区 JavaScript 调用。

<html>
 <head>
  <title>Students</title>
 </head>
 <body>
  <div class="container">
   <form id="createStudent" method="post" action="./?6-7.IFormSubmitListener-createStudent"><div style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden"><input type="hidden" name="createStudent_hf_0" id="createStudent_hf_0" /></div><div style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden"><input type="text" tabindex="-1" autocomplete="off"/><input type="submit" tabindex="-1" name="p::submit" onclick=" var b=document.getElementById('submit'); if (b!=null&amp;&amp;b.onclick!=null&amp;&amp;typeof(b.onclick) != 'undefined') {  var r = Wicket.bind(b.onclick, b)(); if (r != false) b.click(); } else { b.click(); };  return false;"  /></div>
    <div>
     <span id="lastNameLabel">Nachname: </span>
     <input id="lastName" type="text" value="Matthias" name="lastName"/>
    </div>
    <div>
     <span id="firstNameLabel">Vorname:</span>
     <input id="firstName" type="text" value="Tonhäuser" name="firstName"/>
    </div>
     <div>
     <span id="dateOfBirthLabel">Geburtsdatum: </span>
     <input id="dateOfBirth" type="text" value="06.09.17" name="dateOfBirth"/>
    </div>
    <div>
     <input id="submit" type="submit" value="Schüler anlegen" name="p::submit"/>
    </div>
   </form>
  </div>
 </body>
</html>

不太明白为什么突然多了几个师。我的猜测是 JavaScript 调用不起作用。

谢谢。

很可能 onSubmit() 方法没有调用可能是因为对 dateField 值的验证不正确。您必须将 feedback panel 添加到您的页面并检查 .

您可以覆盖 onError(...) 方法并查看发生了什么..