首页 > 解决方案 > 您可以为 PrimeFace 组件设置 Ajax 响应操作,该组件的 html 是在 Bean 中生成的吗?

问题描述

亲爱的友好陌生人,

在 JSF 2.2 上使用 PrimeFaces 7.0 我在我的 Bean 中生成 html-Code 并将其注入到我的 xhtml 中<h:outputText value="#{myBean.myHtml}" escape="false"/>。这自然不适用于<p:.../>组件,因为它们本身会生成/渲染实际的 html。我从我的数据库中更改数据以获取最终 html 的方式对于 html 函数来说太复杂了,所以我仍然想在我的 Java-Beans 中完成它,而不是使用大量ui:repeat和超复杂的自定义样式 - 即使我知道这不是如何使用 jsf/PrimeFaces。p:commandLink现在检查它给出的实际呈现的 html 例如

    <a id="myContainerID:myComponentID" href="#" class="ui-commandlink ui-widget" onclick="PrimeFaces.ab({s:"myContainerID:myComponentID",f:"myContainerID"});return false;">myComponentValue</a>

,我可以很容易地生成,但是在接收到组件的 Ajax 请求时调用的响应动作s:"myContainerID:myComponentID"<p:...>

有没有办法手动设置响应动作,如果是这样,如何/在哪里?

编辑:因为(引用PrimeFaces.ab 函数

//ajax shortcut
ab: function(cfg, ext) {
    return PrimeFaces.ajax.AjaxRequest(cfg, ext);
} 

PrimeFaces.ajax.AjaxRequest 可以是异步的或同步的。AjaxRequest 使用 AjaxUtils,它处理所有发送、处理、响应和更新。

PrimeFaces.ajax.AjaxRequest = function(cfg, ext) {
cfg.ext = ext;

if(cfg.async) {
    return PrimeFaces.ajax.AjaxUtils.send(cfg);
}
else {
    return PrimeFaces.ajax.Queue.offer(cfg);
} }

我想答案(如果有的话)应该在某处AjaxUtils,但还没有找到。

感谢 helluvalot 的任何建议/帮助。

编辑 2:我最终确实设法将它全部转录到带有嵌套 ui:repeats 和许多自定义样式的 xhtml,但我仍然很好奇是否有办法使用 in-Bean 生成的 html 来做到这一点。

示例代码

我的豆:

@ManagedBean(name = "myBean")
@SessionScoped
public class myBean {

private String html1;
private String html2;

@PostConstruct
public void init() {
    html1 = "<p:commandLink id=\"myComponentID\" value=\"myComponentValue\" "
            + "action=\"#{someBean.doSomething()}\"";
    html2 = "<a id=\"myContainerID:myComponentID\" "
            + "href=\"#\" class=\"ui-commandlink ui-widget\" "
            + "onclick=\"PrimeFaces.ab({s:\"myContainerID:myComponentID\","
            + "f:\"myContainerID\"});"
            + "return false;\">1. myComponentValue</a>";
}

public String getHtml1() {
    return html1;
}

public void setHtml1(String html1) {
    this.html1 = html1;
}

public String getHtml2() {
    return html2;
}

public void setHtml2(String html2) {
    this.html2 = html2;
}

}

我的索引.xhtml:

<h:html xmlns="http://www.w3.org/1999/xhtml"   
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:p="http://primefaces.org/ui"
 >

<h:head>        
</h:head>
<h:body>
    <h:form id="myContainerID">         
        <h:outputText value="#{myBean.html1}" escape="false" />     
        <h:outputText value="#{myBean.html2}" escape="false" /> 
    </h:form>   
</h:body>
</h:html>

标签: jsfprimefaces

解决方案


推荐阅读