[Java] How to respond when FacesMessage is set but it is not displayed

1 minute read

Phenomenon: Message is not displayed even though I set FacesMessage to the content

run the method with

ajax


  <h:form id="formId">
    <div id="uploadArea">
<!--... omitted...-->
          <f:ajax event="change" execute="uploadArea" render="uploadArea" listener="#{uploadBean.uploadFile}" />
<!--... omitted...-->
      <div>
        <h:message for="uploadArea" errorClass="error" warnClass="warn" infoClass="info" />
      </div>
    </div>
  </h:form>

I set the message to the content with the ``java: method but it is not displayed public void uploadFile(AjaxBehaviorEvent event) throws IOException { if (!isUpload()) { FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, “”, “No files were selected.”); event.getFacesContext().addMessage(“uploadArea”, message); //…abridgement…



#### **` Output HTML`**
```html

<form id="formId" name="formId" method="post" action="/tryJsf/base.jsf" enctype="application/x-www-form-urlencoded">
  <input type="hidden" name="formId" value="formId">
  <div id="uploadArea">
<!--... omitted...-->
    <input id="formId:file" type="text" name="formId:file" style="display:none;" onchange="mojarra.ab(this,event,'change','uploadArea','uploadArea ')">
    <div>
    </div>
  </div>
  <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-7689768516583343150:2206148480801750608" autocomplete="off">
</form>

Cause: Unknown (Please let me know)

Apparently it is not redisplayed after the method of f:ajax is executed. render="uploadArea" is ignored? No HTML tags? Should it be a JSF tag? Isn’t anything written in JSF tags not a component?

The clientIds of components that will participate in the “render” portion of the Request Processing Lifecycle. ajax(JSF 2.2 View Declaration Language: Facelets Variant)

Solution: Change div tag to h:panelGroup

run the method with

ajax


  <h:form id="formId">
    <h:panelGroup id="uploadArea">
<!--... Omitted... Since it is a JSF tag, it should be the id attribute specified..-->
          <f:ajax event="change" execute="formId:uploadArea" render="formId:uploadArea" listener="#{uploadBean.uploadFile}" />
<!--... omitted...-->
      <div>
        <h:message for="formId:uploadArea" errorClass="error" warnClass="warn" infoClass="info" />
      </div>
    </div>
  </h:form>

method

``` it was displayed after setting the message to the content with the java

public void uploadFile(AjaxBehaviorEvent event) throws IOException { //... omitted... same as before... only the specified id attribute is changed...
        event.getFacesContext().addMessage("formId:uploadArea", message); //... omitted... same as before... ``` 

Tags: ,

Updated: