繁体   English   中英

如果提交时出现错误响应,则提交可能使用ajax的JSF 2表单的最佳实践

[英]Best Practice for submitting a JSF 2 form that might use ajax if we have an error response from the submit

我有一个使用下面的按钮提交到数据库的提交按钮。 如果此按钮返回“错误”,我想使用AJAX删除表单及其内容,并添加一个文本代替表单。

编辑:

单击“提交”按钮后留在同一页面上的原因是,提交后的当前页面和重定向页面都使用了表单上方和下方的相同CMS内容。 因此,更容易删除表格并显示错误页面。 这样可以避免由于重定向而重新加载整个页面。

</h:form id="myForm">
  ...
  <h:commandButton action="#{testBean.submitForm}"
                   type="submit" value="Create Button">
  </h:commandButton>
</h:form>

请记住使用<h:head><h:body>而不是<head><body>

<h:panelGroup id="mypanelgroup">   
    <h:outputText value="#{testBean.errorMessage}" rendered="#{testBean.errorMessage ne null}" />

    <h:form rendered="#{testBean.errorMessage eq null}">
        <!--  -->
        <h:commandButton value="submit" action="#{testBean.submitForm()}">
           <f:ajax execute="@form" render=":mypanelgroup"></f:ajax> 
        </h:commandButton>
    </h:form>
</h:panelGroup>


@ManagedBean
public class TestBean {

    private String errorMessage = null;

    public String getErrorMessage() {
        return errorMessage;
    }

    public void submitForm(){
        // .....
        //if(error){
            this.errorMessage = "My error Message";
        //} 
    }
}

最初,不会呈现<h:outputText>因为默认情况下errorMessage设置为null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM