【问题标题】:JSF 2.2 Form Submit, reloading page = resubmit?JSF 2.2 表单提交,重新加载页面 = 重新提交?
【发布时间】:2013-11-19 20:33:47
【问题描述】:

所以我有一个 JSF 应用程序。如果我提交表单,刷新页面时会重新提交。我当然不想那样。

索引.xhtml:

[...]
    <h:form >
        <h:outputText value="Form"/><br/>
        <h:inputText label="first" value="#{example.newExample.firstWord}" autocomplete="off"></h:inputText>
         <h:commandButton value="click" actionListener="#{example.NewExample()}">        

         </h:commandButton>

    </h:form>
[...]

运行的方法:

  public void NewExample(){
    pfDB.InsertNewExample(NewExample);
    NewExample.setFirstWord(null);
    NewExample.setSecondWord(null);
}

所以每次我在提交表单后刷新 index.xhtml 时,都会运行 NewExample()。

【问题讨论】:

  • 那么,你的意思是 NewExample() 被调用了两次? (另外,请记住,在 java 中,约定是方法的第一个字母应该小写)
  • 不,如果我在提交表单后手动刷新页面,则会调用它
  • 那是因为刷新页面时再次提交了POST请求。您的网络浏览器可能会提醒您数据将再次发送并要求您确认。

标签: forms jsf jsf-2 facelets


【解决方案1】:

确实问题在于重新提交表单。但是浏览器(opera)没有显示警报。

我通过添加修复它

  FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");

在我的函数中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-03
    • 2019-11-26
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    相关资源
    最近更新 更多