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

标签 java html wicket

关于 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 值的验证不正确。您必须将反馈面板添加到您的页面并检查。

关于java - 为什么当存在 DateTimeField 时不调用表单的 onSubmit 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46215383/

相关文章:

java - ServletContext#getRequestDispatcher() 添加了额外的斜杠,导致云服务器上出现 "Requested URI does not match Resource path pattern"错误

java - NoSuchMethodError : org. apache.hadoop.io.retry.RetryUtils.getDefaultRetryPolicy

javascript - 添加 CSS3 过渡 float 导航栏 - JQUERY

PHP - 从数据库中获取值并将其设置为 HTML 选择标记的值

java - Wicket - IValidator 在 TextField 为空时不调用验证

java - 序列化Point,PointF对象

java - 无法从 Eclipse 中的 jsp 连接到 mySQL DB

javascript - 如何使用 onLoad 加载 jQuery JSON。从表格上的邮政编码获取城市和州?

java - 具有历史 API 的 Wicket AjaxEventBehavior

wicket - Wicket 页面 id 机制的见解