【问题标题】:PostConstruct methos called again with richfaces 4.2 , works fine with myfaces使用 richfaces 4.2 再次调用 PostConstruct 方法,适用于 myfaces
【发布时间】:2014-10-17 22:21:49
【问题描述】:

我在包含多个表单的页面中看到不同的行为。

这是我的支持 bean:

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class MultiFormBean
{
    String inputText1 = "";
    String inputText2 = "";

    @PostConstruct
    public void initializeBean(){
        System.out.println("PostConstruct Called ------------------");
    }

    public String getInputText1()
    {
        return inputText1;
    }

    public void setInputText1(String inputText1)
    {
        this.inputText1 = inputText1;
    }

    public String getInputText2()
    {
        return inputText2;
    }

    public void setInputText2(String inputText2)
    {
        this.inputText2 = inputText2;
    }

    public void doSubmit1() {
        inputText2 = inputText1;
    }

    public void doSubmit2() {
        inputText1 = inputText2;
    }

}

当我使用以下 xhtml 时,多次单击 Submit1 和 Submit2 不会多次调用@PostConstruct:

    <h:body>
        <h:form id="firstForm" prependId="false">
            <h:panelGroup layout="block" id="renderTarget1"/>
            <h:inputText id="first_input" value="#{multiFormBean.inputText1}"/>
            <h:commandButton id="click1" action="#{multiFormBean.doSubmit1}" value="submit1" type="submit"
                             onclick="javascript:jsf.ajax.request(this, event, {execute:'firstForm', render:'renderTarget1 secondForm'}); return false;">
            </h:commandButton>
        </h:form>
        <h:form id="secondForm" prependId="false">
            <h:panelGroup layout="block" id="renderTarget2"/>
            <h:inputText id="second_input" value="#{multiFormBean.inputText2}"/>
            <h:commandButton id="click2" action="#{multiFormBean.doSubmit2}" value="submit2" type="submit"
                             onclick="javascript:jsf.ajax.request(this, event, {execute:'secondForm', render:'renderTarget2 firstForm'}); return false;">

            </h:commandButton>

        </h:form>       
</h:body>

但是下面的 xhtml 会不止一次调用@PostConstruct:

    <h:body>
        <h:form id="firstForm" prependId="false">
            <h:panelGroup layout="block" id="renderTarget1"/>
            <h:inputText id="first_input" value="#{multiFormBean.inputText1}"/>
<a4j:commandButton id="click1" action="#{multiFormBean.doSubmit1}" value="submit1" type="submit" execute="@form" render="renderTarget1,secondForm"/>
        </h:form>
        <h:form id="secondForm" prependId="false">
            <h:panelGroup layout="block" id="renderTarget2"/>
            <h:inputText id="second_input" value="#{multiFormBean.inputText2}"/>

            <a4j:commandButton id="click2" action="#{multiFormBean.doSubmit2}" value="submit2" type="submit" execute="@form" render="renderTarget2,firstForm"/> 


        </h:form>       
    </h:body>

谁能帮我用&lt;a4j:commandButton&gt;代替&lt;h:commandButton&gt;

我还看到我无法使用 a4j commandButton 调用方法 doSubmit2()

【问题讨论】:

  • 我也看到我无法使用 a4j commandButton 调用方法 doSubmit2()

标签: jsf


【解决方案1】:

我认为这里的问题在于 JSF2 和 Richfaces4 中的错误。 Richfaces 从 4 版本开始使用 JSF 嵌入式 ajax 功能。并且在带有 ajax 请求的页面上使用多个表单存在错误。 Richfaces 使用当前呈现的视图状态的 id 呈现特殊隐藏输入的问题。呈现新视图时会更改此 ID。它还与每个请求一起提交,以表明它属于某个特定视图。因此,当您在第一次 ajax 请求后在同一页面上有多个表单时,视图状态会出现错误的位置,并且不能再次提交第二次。有时行为看起来非常奇怪,没有合乎逻辑的描述。

PostConstruct 被调用两次,因为服务器认为两个请求属于不同的视图(视图状态未提交),并且就 bean 的视图范围而言,它被创建了两次。在单击 ajax 后可以完全停止使用此操作,因为服务器无法识别视图(可能是您无法单击第二个提交按钮时看到的内容)。

首先,我建议您使用最新可用的 JSF 和 Richfaces 版本。这个错误(以及更多错误)可能已经在那里修复了。

【讨论】:

    猜你喜欢
    • 2016-11-24
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2013-04-21
    • 2017-02-15
    • 2014-02-14
    • 1970-01-01
    相关资源
    最近更新 更多