【问题标题】:JSF1.2 Messages not renderedJSF1.2 消息未呈现
【发布时间】:2011-04-01 19:11:42
【问题描述】:

从 WAS6.1+JSF1.1+richfaces.3.1.5 迁移到 WAS7+JSF1.2+facelets1.1.14+richfaces3.3.3。

错误/状态消息未使用 h:messages 呈现,即使在调试 facescontext.getMessages() 时包含这些消息。

在提交表单时,我正在验证输入。如果验证失败,我将向 facescontext 添加错误消息。它可以用于多个输入。每个错误消息都会添加到 facescontext 中

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

    getFacesContext().addMessage(componentID, message);

在 xhtml 上我使用 h:messages 显示它

我在 WAS6.1 和 JSF1.1 中使用 jsp,这曾经可以正常工作 谢谢

添加更多细节

我的 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 和 statusMessages 是显示错误(验证错误)和状态(如更新完成)消息的常用标签

如果遇到错误(如“找不到文件”或“数据库已关闭”,我会在资源文件中使用错误/状态消息调用通用方法。

WebUtils.addCustomErrorMessage("global.error.ContactInfo-DuplicateRecords-UserID", new String[]{userid,Integer.toString(j+1)}, Constants.RESOURCE_BUNDLE_NAME); 或者 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);
}

当输入字段遇到错误时,我们也使用相同的标签来显示错误消息。例如名字字段包含无效字符。

如前所述,在我们迁移到 JSF1.2 之前它运行良好

【问题讨论】:

  • 请添加更多信息

标签: java jsf


【解决方案1】:

你的答案也需要有 xhtml 代码,无论如何我给你在 JSF 1.2 中使用验证的示例......

xhtml 代码应该是这样的..

    <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" />

java 代码应该是...

  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);
    }
}

【讨论】:

  • OP 确实采用了错误的方法进行验证,但它应该可以正常工作,这并不能解释或回答真正的问题。
猜你喜欢
  • 1970-01-01
  • 2021-10-29
  • 2013-06-13
  • 1970-01-01
  • 2021-12-07
  • 2020-06-16
  • 1970-01-01
  • 2013-01-24
  • 1970-01-01
相关资源
最近更新 更多