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

时间:2017-09-14 09:28:17

标签: java html wicket

有关Apache Wicket的问题:我有一个包含多个文本输入字段的表单。它工作正常,直到我添加DateTextField。不再调用onSubmit()的override方法。我查看了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调用不起作用。

感谢。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您可以覆盖onError(...)方法并查看最新情况。