java为什么我必须点击提交按钮两次?
我有一个巨大的数据采集表格。主要的问题是,为了提交表单,我总是需要点击提交按钮两次。我第一次按下它,它只是刷新,什么也不做。为什么会这样?在我的表单中,有大量的输入文本
还有一些动态选择菜单,其中的项目列表根据不同菜单中的选定项目而变化。我的怀疑是,问题出在这里,因为存在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();
}
# 1 楼答案
我在显示弹出窗口时也遇到了类似的问题。弹出窗口显示后,单击提交按钮仅刷新弹出窗口。没有数据与托管bean同步。但是,当再次单击按钮时,它会提交数据
我通过更改显示弹出窗口的代码解决了这个问题,从
去
我不确定这其中的技术细节是什么,但也许这有助于某人继续前进