Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 如何在页面加载期间添加FacesMessage?使用@PostConstruct似乎不起作用_Jsf_Message_Pageload_Postconstruct - Fatal编程技术网

Jsf 如何在页面加载期间添加FacesMessage?使用@PostConstruct似乎不起作用

Jsf 如何在页面加载期间添加FacesMessage?使用@PostConstruct似乎不起作用,jsf,message,pageload,postconstruct,Jsf,Message,Pageload,Postconstruct,在一个支持bean的@PostConstruct方法中,我调用一个EJB,它可能会返回一些我希望通过p:messages显示在页面上的消息。但是,即使我添加了FacesMessages,例如FacesContext.getCurrentInstance().addMessage(…),p:消息也不会用FacesMessages更新 如果我在页面的某个操作上调用对EJB的调用(比如用户单击页面上的一个按钮,该按钮调用调用EJB的方法,然后添加FacesMessage),那么消息将按预期使用p:me

在一个支持bean的@PostConstruct方法中,我调用一个EJB,它可能会返回一些我希望通过p:messages显示在页面上的消息。但是,即使我添加了FacesMessages,例如FacesContext.getCurrentInstance().addMessage(…),p:消息也不会用FacesMessages更新

如果我在页面的某个操作上调用对EJB的调用(比如用户单击页面上的一个按钮,该按钮调用调用EJB的方法,然后添加FacesMessage),那么消息将按预期使用p:messages显示

如何在@PostConstruct期间添加Faces消息,并在页面最初呈现时显示这些消息

代码:

Page1Controller.java:

@ManagedBean
public class Page1Controller
{
    @PostConstruct
    public void init()
    {
        FacesContext.getCurrentInstance().addMessage(null, 
            new FacesMessage("Test Message from @PostConstruct"));
    }

    public String getValue()
    {
            return "Some Value";
    }

    public void triggerMessage(ActionEvent event)
    {
            FacesContext.getCurrentInstance().addMessage(null, 
                    new FacesMessage("Test Message from Trigger Button"));      
    }

}
page1.xhtml

   <h:form>
        <p:messages showDetail="true" showSummary="true" autoUpdate="true"/>
        <h:outputText value="#{page1Controller.value}"/>
        <br/>
        <p:commandButton value="Trigger Message" 
                         actionListener="#{page1Controller.triggerMessage}"/>  
   </h:form>



在添加消息之前呈现消息组件时可能发生的情况

在您的特定示例中,
组件第一次引用bean,并因此第一次构造bean。但是在您的特定示例中,
组件出现在
组件之后,因此
组件已经呈现,因此显示消息为时已晚

您需要确保在呈现消息组件之前添加消息。一种方法是使用
。它在
INVOKE\u应用程序
阶段运行,该阶段在
RENDER\u RESPONSE
阶段之前。因此,它在渲染任何组件之前运行。这是一个绝佳的机会

<f:metadata>
    <f:viewAction action="#{bean.onload}" />
</f:metadata>
另见:

您可以收集错误,然后在加载页面结束时使用带autorun=true模式的primefaces远程命令显示错误。 在我的例子中,我有一个viewScope,在xhtml中我显示了@PostConstruct中加载的值列表。如果生成异常,我将在页面加载结束时使用remoteCommand将其保存到示例中(如果存在)

private ArrayList<Exception> postConstucError = new ArrayList<>();

@PostConstruct
public void validarAcceso() {
    /**
     * verificar permisos a la vista de coeficientes
     */
    try {
            this.init() //load data;
        } catch (Exception e) {
        System.out.print(e.getMessage());
        this.postConstucError.add(e);
    }
}

 public void showPostConstructError() {
    try {
        for (int i = 0; i < this.postConstucError.size(); i++) {
            JsfUtil.addErrorMessage("Error al cargar datos iniciales: " + postConstucError.get(i).getMessage());
        }
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, "Error: showPostConstructError() " + e.getMessage());
    }
}
private ArrayList postConstucError=new ArrayList();
@施工后
公共无效Validaraceso(){
/**
*验证是否允许使用系数
*/
试一试{
this.init()//加载数据;
}捕获(例外e){
System.out.print(如getMessage());
本.邮政编码错误.增补(e);
}
}
公共void showPostConstructorTerror(){
试一试{
对于(int i=0;i
xhtml代码

  <p:messages id="messages" showDetail="true" autoUpdate="true" closable="true"/> 
  <h:form>
        <p:remoteCommand id="rcomerror" name="showError" process="@this"  autoRun="true"
                         actionListener="#{mBPresentNinos.showPostConstructError()}" />  
    </h:form>

对于我来说,使用preRenderView事件在表单init上显示消息是一个消息地狱。所以我创建了非常简单的“组件”来保存静态消息。对于本例,只支持一条错误消息

staticMessage.xhtml:

<ui:fragment xmlns="http://www.w3.org/1999/xhtml"
             xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
             rendered="#{rendered}">
    <div id="staticMessage" class="ui-messages ui-widget" aria-live="polite">
        <div class="ui-messages-error ui-corner-all"><span class="ui-messages-error-icon"/>
            <ul>
                <li>
                    <span class="ui-messages-error-summary">#{value}</span>
                </li>
            </ul>
        </div>
    </div>
</ui:fragment>

  • #{value}
包括信息:

 <ui:include src="/template/components/staticMessage.xhtml">
     <ui:param name="rendered"value="#{beanMB.staticMessagesRendered}"/>
     <ui:param name="value" value="Place your message here."/>
 </ui:include>


Note——如果使用f:event type=“preRenderView”侦听器指令调用相同的方法(调用EJB等),则会适当更新FacesMessage。我应该这样做吗?使用Mojarra 2.1.7,Primefaces 3.2(最新版本)。谢谢。。。在@PostConstruct方法中,在添加FacesMessage后,我可以访问Messages组件(通过ViewRoot.findComponent)并告诉Messages组件自己“重新渲染”吗?(这可能吗?)如果可能,这是一种比预渲染视图或将输出文本移动到p:messages上方更好的方法吗?不,这是不可能的。至于正确的方法,这取决于具体的功能需求。在什么情况下,您需要添加faces消息?当某个请求参数无效时?如果是这样,您应该使用
来进行此操作。如果没有,那么
就可以了(最好在以后有机会升级到JSF2.2时用更理智的
来代替)。。。要在屏幕上显示一条消息应该不难。我不明白这一点:“在第一次构建托管bean之前就呈现了。现在添加消息已经太晚了。”为什么太晚了?请注意,我只是好奇而已。@CED:Plain java“示例”:假设您有一个包含5个元素的数组,并对其进行迭代,然后对每个元素执行
System.out.println
(渲染),然后添加一个额外的元素。那就不显示了。这不是一个JSF特有的问题,就像一个8年前的评论所暗示的那样。带EL的普通JSP和使用的任何js UI框架都有相同的问题。。。您的
showPostConstructorr()
方法看起来怎么样?
 <ui:include src="/template/components/staticMessage.xhtml">
     <ui:param name="rendered"value="#{beanMB.staticMessagesRendered}"/>
     <ui:param name="value" value="Place your message here."/>
 </ui:include>