0

Dear friendly strangers,

using PrimeFaces 7.0 on JSF 2.2 I'm generating html-Code in my Bean and inject it in my xhtml with <h:outputText value="#{myBean.myHtml}" escape="false"/>. This naturally doesn't work with <p:.../> components, as they themselves generate/render actual html. The way I alter the data from my Database to get the final html is too complicated for html functions though, so I still wanna do it in my Java-Beans instead of using lots of ui:repeat and hypercomplex custom styles - even though I know this is not how jsf/PrimeFaces is meant to be used. Now checking the actual rendered html e.g. of a p:commandLink it gives

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

,which I can generate easily, but the response-action called when receiving the component's Ajax request (s:"myContainerID:myComponentID") will be missing, which seems to be saved somewhere in the moment the actual html is generated with <p:...>.

Is there a way to manually set that response-action, if so how/where?

EDIT: Since (quoting PrimeFaces.ab function)

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

The PrimeFaces.ajax.AjaxRequest can be asynchronous or synchronous. The AjaxRequest uses the AjaxUtils, which handles all send, process, response, and update.

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);
} }

I suppose the answer, if there is any, should lay somewhere in AjaxUtils, but couldn't find it yet.

Thanks helluvalot for any suggestion/help.

EDIT 2: I did eventually manage to transcribe it all to the xhtml with nested ui:repeats and lots of custom styles, I'm still curious though whether there's a way to do it with in-Bean-generated html.

ExampleCode

myBean:

@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;
}

}

myIndex.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>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • 1
    Don't go in this direction. Try creating a custom component instead. Not too difficult using one as an example and a good tutorial. or a composite component – Kukeltje Jul 17 '19 at 05:49
  • You are just manipulating client side HTML instead of adding components to your (server side) view tree. Luckily JSF won't invoke your action method `#{someBean.doSomething()}` this way. This is a feature - any (possibly evil) user is able to manipulate HTML within the browser at will. You are propably looking for a way on [How to create dynamic JSF form fields](https://stackoverflow.com/questions/3510614/how-to-create-dynamic-jsf-form-fields). Doing this in XHTML instead leads to source code by far better readable and maintainable than the dynamic Java way. – Selaron Jul 17 '19 at 06:37

0 Answers0