【问题标题】:f:ajax method gets called on load of the jsf pagef:ajax 方法在加载 jsf 页面时被调用
【发布时间】:2012-06-24 09:03:44
【问题描述】:

我有一个简单的 3line jsf 页面和一个支持 bean。我在我的页面中使用命令按钮。 Onclick 事件我正在调用 ajax。其中 ajax 方法将输出文本组件添加到子项。现在问题来了。输出文本组件在页面加载时呈现,而不是在点击页面时呈现。

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <h:commandButton id="button1" action="return false" value="Submit">
                <f:ajax execute="#{bb.method1()}" render="@form" transient="true" event="click" />
            </h:commandButton>
        </h:form>
    </h:body>
</html>

支持 bean 代码

@Named("bb")
@SessionScoped
public class BackingBean implements Serializable {

public BackingBean() {
}

public void method1()
{
    HtmlOutputText out=new HtmlOutputText();
    out.setValue("Text1");
    FacesContext context=FacesContext.getCurrentInstance();
    context.getViewRoot().getChildren().add(out);
    context.getViewRoot().setTransient(true);
}   
}

点击按钮 firebug show status 200 OK.. 但我在页面上看不到任何变化

【问题讨论】:

  • 在你的 f:ajax 中你应该使用监听器而不是执行!检查此链接以查看 ajax 示例:mkblog.exadel.com/2010/04/…。而且我不知道什么是transcient属性。您也可以删除您的操作。

标签: jsf-2


【解决方案1】:

execute 属性应该引用一个空格分隔的输入组件的客户端 ID 集合,这些输入组件需要在 ajax 提交期间进行处理。当视图被渲染并且任何绑定到它的方法被立即调用时,这个属性被评估。

您应该改用listener 属性。

<f:ajax listener="#{bb.method1()}" render="@form" transient="true" event="click" />

render 属性也是如此。

另见:

【讨论】:

  • 按照您提到的内容,我无法在 Ajax 调用中添加新的 Html 标记。 FireBug 显示成功响应。但它有更新更新标签而不是插入 xml 标签
  • 这是一个不同的问题。您正在将新组件添加为 viewroot 的子组件,但您只是在更新表单。您需要将其添加为表单的子级,或更新整个视图根。顺便说一句,这种方法很臭。查看rendered 属性或使用faces 消息。
  • 是的,谢谢 BalusC,在 a4j 漫游之后,primefaces。发现使用简单的 jquery post 方法和 servlet 更容易,在 jsf 中添加 html 元素............
猜你喜欢
  • 2012-05-03
  • 1970-01-01
  • 2012-08-03
  • 2019-10-03
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 2013-07-27
  • 2016-06-26
相关资源
最近更新 更多