简体   繁体   中英

JSF1.2 Messages not rendered

Migrating from WAS6.1+JSF1.1+richfaces.3.1.5 to WAS7+JSF1.2+facelets1.1.14+richfaces3.3.3.

Error/Status messages are not rendering using h:messages, even though on debugging the facescontext.getMessages() contains the messages.

On submitting a form, I am validating an input. If the validation fails I am adding an error msg to the facescontext. It can be for multiple inputs. Each error msg is added to the facescontext

    FacesMessage message = new FacesMessage();
    message.setDetail(msg);
    message.setSummary(msg);
message.setSeverity(FacesMessage.SEVERITY_ERROR);

    getFacesContext().addMessage(componentID, message);

and on the xhtml I am displaying it using h:messages

I was using jsp in WAS6.1 and JSF1.1 and this used to work fine Thanks

Adding more details

My xhtml

<ui:composition template="/template.xhtml">
    <ui:define name="content">
        <div id="content-nosidebar">
            <h:form id="uploadDoc1" >
                <h:messages/>
                <h:panelGrid id="panelGridContact" headerClass="standardPageHeader" width="100%" cellpadding="5">
                    <f:facet name="header">
                        <h:panelGroup>
                            <h:outputText value="Upload Contact Info" />
                            <hr/>
                        </h:panelGroup>
                    </f:facet>
                    <h:panelGroup id="msgId">
                        <xyz:errorMessages />
                        <xyz:statusMessages />
                    </h:panelGroup>
                    <h:panelGrid style="text-align: center;font-weight: bold;">
                        <h:outputText value="Click on Browse button to identify CSV file with contact information for upload."/>
                        <h:outputText value="File size limit is 10MB."/>
                        <h:panelGroup>
                            <rich:fileUpload id="fileuploader" fileUploadListener="#{uploadContactCntrl.onSubmit}"
                                acceptedTypes="csv" allowFlash="true" listHeight="50" addControlLabel="Select File" uploadControlLabel="Submit" clearControlLabel="clear"/>
                        </h:panelGroup>
                        <h:outputText value=" "/>
                    </h:panelGrid>
                </h:panelGrid>
            </h:form>
        </div>
    </ui:define>
</ui:composition>

errorMessages and statusMessages are common tags to display error(validation error) and status((like Update complete) messages

In the the backingbean on submit if an error is encountered (like "File not found" or "Database is down" I call a common method with the error/status message from the resource file.

WebUtils.addCustomErrorMessage("global.error.ContactInfo-DuplicateRecords-UserID", new String[]{userid,Integer.toString(j+1)}, Constants.RESOURCE_BUNDLE_NAME); or WebUtils.addCustomStatusMessage("global.error.ContactInfo-successMessage", new String[]{Integer.toString(noOfRowsInserted)}, Constants.RESOURCE_BUNDLE_NAME);

public static void addCustomErrorMessage(String msg, String componentID) {
    FacesMessage message = new FacesMessage();

    message.setDetail(msg);
    message.setSummary(msg);
    message.setSeverity(FacesMessage.SEVERITY_ERROR);

    getFacesContext().addMessage(componentID, message);
}

public static void addCustomStatusMessage(String msg, String componentID) {
    if (errorCodeParameters != null && errorCodeParameters.length > 0)       
        msg = MessageFormat.format(msg, (Object[])errorCodeParameters); 

    FacesMessage message = new FacesMessage();

    message.setDetail(msg);
    message.setSummary(msg);
    message.setSeverity(FacesMessage.SEVERITY_INFO);

    getFacesContext().addMessage(componentID, message);
}

We also use the same tags to display an error message when an error is encountered on an input field. For eg a Firstname field has invalid characters.

As mentioned earlier, this was working fine before we migrated to JSF1.2

Your answer need to have xhtml code too, any way i'm giving u sample for using validation in JSF 1.2...

The xhtml code should be like..

    <h:inputText id="txt_project_name"
        value="#{FIS_Project_Master.txt_project_name_value}"
      validator="#{FIS_Project_Master.txt_project_name_validator}"/>

   <h:message id="msg_txt_project_name" for="txt_project_name" />

The java code should be like...

  public void txt_project_name_validator(FacesContext context, UIComponent component, Object value) {
    if (bmwpProjectMstFacade.ispmProjName_exists(value.toString().toUpperCase().trim())) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Warning", "Project Name Already Exist");
        throw new ValidatorException(message);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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