简体   繁体   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

I have a submit button that submits to a database using the button below. 我有一个使用下面的按钮提交到数据库的提交按钮。 If this button return "error", I want to use an AJAX to remove the form and its content and add a text in place of the form. 如果此按钮返回“错误”,我想使用AJAX删除表单及其内容,并添加一个文本代替表单。

Edit: 编辑:

The reason of staying in the same page after clicking the submit button is because, the same CMS content above and below the form is used on both the current page and redirected page after submit. 单击“提交”按钮后留在同一页面上的原因是,提交后的当前页面和重定向页面都使用了表单上方和下方的相同CMS内容。 Thus, its easier to just remove form and display the error page. 因此,更容易删除表格并显示错误页面。 This will avoid the whole page to reload due to redirect. 这样可以避免由于重定向而重新加载整个页面。

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

Remember to use <h:head> and <h:body> instead of <head> and <body> . 请记住使用<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";
        //} 
    }
}

Initially, the <h:outputText> is not rendered because errorMessage by default was set to null . 最初,不会呈现<h:outputText>因为默认情况下errorMessage设置为null

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

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