【发布时间】:2010-10-28 04:18:20
【问题描述】:
我有一个相当大的数据采集表格。主要问题是我总是必须点击提交按钮两次才能提交表单。我第一次按下它时,它只是刷新并且什么也不做。为什么会这样?在我的表单中,有很多输入文本。
还有一些动态选择菜单,其中的项目列表会根据不同菜单中的选定项目而变化。我的怀疑是问题出在此处,因为有 valueChangeListener、onchange="submit()" 和 immediate="true",它们可能会产生一些我不太了解的副作用。我没有收到任何错误消息。以前有人遇到过这样的问题吗?
<h:commandButton type="submit" value="Add Job Profile" action="{formBean.commitData}"/>
public void commitData() {
QueryRunner run = new QueryRunner(ds);
String insertSql = "sql command here";
try {
int numberOfCommits = run.update(insertSql);
if (numberOfCommits > 0) {
JOptionPane jop = new JOptionPane("Inserted " + numberOfCommits +"record");
JDialog dialog = jop.createDialog("Upload complete");
dialog.setAlwaysOnTop(true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} else {
JOptionPane jop = new JOptionPane("Upload failed. Please try again." );
JDialog dialog = jop.createDialog("Upload failed");
dialog.setAlwaysOnTop(true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
我什至在第一次点击时都没有收到上传失败的消息。第二次点击,上传成功。
编辑:是的,它是动态选择菜单。如果我删除这些菜单,我可以在第一次点击时提交。不过,我该如何解决这个问题?
<h:outputLabel value="Family: " />
<h:selectOneMenu id="selectFamily"
value = "#{formBean.selectedFamily}"
valueChangeListener="#{menuBean.changeFamily}"
onchange="submit()"
immediate = "true" >
<f:selectItems id="familyNames" value="#{menuBean.familyNames}" />
</h:selectOneMenu>
<h:outputLabel value="Function: " />
<h:selectOneMenu id="selectFunction"
value="#{formBean.selectedFunction}"
immediate="true" >
<f:selectItems id="functionNames"
value="#{menuBean.functions}" />
</h:selectOneMenu>
// listen for family change and render relevant function
public void changeFamily(ValueChangeEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
String value = (String) event.getNewValue();
switchFunctions(value);
context.renderResponse();
}
【问题讨论】:
-
你试过在按钮上设置 immediate="true" 吗?
-
是的,已经这样做了,这让事情变得更糟。在我的输入字段中,所有内容都作为 null 传递。