【问题标题】:Why PostConstruct method is called several times inside an RichFaces 4.3 component with a ViewScoped?为什么在带有 ViewScoped 的 RichFaces 4.3 组件中多次调用 PostConstruct 方法?
【发布时间】:2023-03-28 08:55:01
【问题描述】:

我在 Tomcat 7 上遇到了 JSF 2.2、richfaces 4.3.2 的问题。 我的页面被注释为ViewScoped。 我显示第一个表格。当我更改值并选择特定值时,我通过 ajax 在a4j:outputPanel 中显示rich:panel 元素。 在这个a4j:outputPanelrich:panel 组件中,我有一个执行表单的h:commandButton。 例如,如果字段为空(或其他内容),我想检索表单的消息错误

但是当我点击h:commandButton 时,视图被重新实例化并且@postcontruct 方法被重新执行。它应该只在视图范围内执行一次,我错了吗?

我不希望视图被重新显示,我希望在单击内部的 h:commandButton 时保持 ajaxoutputPanel 显示。(并且我希望在我的表单字段旁边看到 h:messages...不是多问:-))

我读到了一些 bug... 有没有办法改变这种行为而不传递给 SessionScoped 例如。

谢谢各位。

    <fieldset>

    <h:form>

        <h:panelGrid columns="3">

            <h:outputText value ="Nom de l'étude : "></h:outputText> 
            <h:inputText id="study_name" value="#{analyse.study_name}"   size="20" required="true"  label="Nom de l'étude" />
            <h:message for="study_name" style="color:red" />

            <h:outputText value ="Analyse : "> </h:outputText> 
            <h:selectOneMenu  id = "analyse" value="#{analyse.analyse_type}">
            <f:selectItems value="#{analyse.analyse_type2Value}" />
                  <f:ajax execute="analyse" render=":ajaxOutputPanelAnalyse"  /> 
            </h:selectOneMenu>

         </h:panelGrid>

    </h:form>

</fieldset>

    <a4j:outputPanel id="ajaxOutputPanelAnalyse" layout="block" ajaxRendered="true"  >

    <rich:panel  id="richPanelAnalyse"  rendered="#{analyse.analyse_type == 'NGS' and request.isUserInRole('ROLE_ADMIN_PROFILER_NGS')}" >

        <h:form id ="NGS_form" >

            <h:panelGrid columns="4">

                <h:outputText value ="Run # :"> </h:outputText> 
                <h:inputText id="run_number" value="# {analyse.run_number}" size="20" required="true" label="Run" />
                <h:message for="run_number" style="color:red" />
                <h:outputText></h:outputText>


            </h:panelGrid>



                 <h:commandButton value="Submit" action="#{analyse.addAnalyse}"/>


            </h:form>

    </rich:panel>




  </a4j:outputPanel>

在豆子里...

@PostConstruct
public void setFlashParam(){

    System.out.println("POST CONSTRUCT MON POTE");

    FacesContext facesContext = FacesContext.getCurrentInstance();


     return;
}

public String addAnalyse(){


    System.out.println("Kikou");

    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(" - Ajout de l'analyse ?"+" pour le patient ?"+" dans l'étude "+ study_name +" -"));


    return "pretty:home";

}

【问题讨论】:

  • 所以,您的提交按钮在任何表单之外,这是不正确的。还能显示bean定义和按钮的动作方法吗?
  • 不错的镜头,但我在复制代码时犯了一个错误。提交实际上是在表单中。 (我编辑)。例如,按钮的操作方法是空的,它只是打印一些东西......我会发布 bean,但我不认为问题来自 bean。这是一个非常常见的 bean 'annoted managedBean & ViewScoped),带有用于 xhtml 中属性的 getter 和 setter ......而我的 postconstruct 方法只是从 bean 属性中的 faces 上下文设置 flash 参数。
  • 你的action方法返回了什么?
  • 我返回 pretty:home ...我使用 prettyFaces 样式进行导航,然后转到我的主页。
  • 注意如果我删除rich:panel, f:ajax call inside first form, and a4j:outputPanel , postconstruct 方法被调用一次...bean被初始化一次并且每个表单的消息错误字段显示良好。我认为问题是由于 ajax 的东西......

标签: ajax jsf-2 richfaces view-scope postconstruct


【解决方案1】:

感谢 BalusC 的这篇帖子(一如既往)http://balusc.blogspot.fr/2011/09/communication-in-jsf-20.html#AjaxRenderingOfContentWhichContainsAnotherForm

编辑: 但是,当您尝试使用 h:inputFile 添加rich:fileupload 时,它不再起作用...当您单击按钮时,它将在新的文本区域中显示奇怪的 html 代码。 ...这很奇怪...

           <rich:fileUpload  id="upload" fileUploadListener="#{analyse.test}"  acceptedTypes="bam,pdf,png" ontyperejected="alert('Seulement les fichiers avec l'extension bam et pdf sont acceptés.');" maxFilesQuantity="3">
                  <a4j:ajax event="uploadcomplete" execute="@none" render="upload" />        
            </rich:fileUpload>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2012-10-28
    相关资源
    最近更新 更多