Un AjaxBehaviour ajouté programmatiquement à un commandLink n'a pas marché.
Salut !
Depuis 2 jours j'essaie de trouver une solution à ce problème mais sans succès.
A une form dans une page .xhtml, j'ai réussi à ajouter un commandLink par du code java (dynamiquement) et cette opération se déroule convenablement puisque j'arrive à voir cet élément dans la page après un rafraîchissement partiel avec ajax du block dans lequel j'ai ajouté le commandLink.
J'essaie d'ajouter un AjaxBehaviour au commandLink (j'ai essayé de deux façon s: 1- après avoir ajouter le commandLink au view , je le récupère avec un form.findcompoent('id du commandlink') Et 2-J'ajoute le AjaxBehaviour au commandLink avant d'ajouter ce dernier au view.
Et puis, pour l'ajout de l'ajax behaviour lui même j'ai aussi essayé de faire de deux façons (voir le managed Bean : method 1 et method 2) Mais aucune façon n'a reussit.
voila mon index.xhtml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html>
<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"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>dyn add comps test </title>
</h:head>
<body>
<h:form id="form1">
<h:commandLink value="cliick me">
<f:ajax event="click" listener="#{myBean.sayHelloToName}"/>
</h:commandLink>
<br />
<br />
<!-- une fois le commandLink plus haut cliqué , un autre commandLink s ajoute dynamiquement au block qui suit : -->
<h:panelGroup id="foo" layout="block" style="position:absolute;width:70px;height:30px;border:1px solid blue;">
</h:panelGroup>
</h:form>
</body>
</html> |
et voila mon Managed Bean :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
package mybeans;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.behavior.AjaxBehavior;
import javax.faces.component.html.HtmlCommandLink;
import javax.faces.component.html.HtmlPanelGroup;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import org.primefaces.component.behavior.ajax.AjaxBehaviorListenerImpl;
import org.primefaces.context.RequestContext;
@ManagedBean
@SessionScoped
public class MyBean {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//Le listener Handler pour le commandLink qui s'ajoute dynamiquement :
public void sayHelloToName2(AjaxBehaviorEvent abe){
System.out.println("!!!-->>>>> the Ajax Behaviour Works !!!!! ");
}
public void sayHelloToName(AjaxBehaviorEvent abe){
System.out.println("sayHelloToName is running ! ");
FacesContext fc = FacesContext.getCurrentInstance();
Application application = fc.getApplication();
ExpressionFactory ef =fc.getApplication().getExpressionFactory();
UIComponent form1 = fc.getViewRoot().findComponent("form1");
if(form1!=null){
//Creating the commandLink
HtmlCommandLink mynewcmdlink = (HtmlCommandLink) application.createComponent(HtmlCommandLink.COMPONENT_TYPE);
mynewcmdlink.setId("mynewcmdlink");
mynewcmdlink.setValue("clickme2!!");
//Adding the commandLink to the form
HtmlPanelGroup foo = (HtmlPanelGroup) form1.findComponent("foo");
foo.getChildren().add(mynewcmdlink);
//creating the Ajax Behaviour method 1
// MethodExpression me = ef.createMethodExpression( fc.getELContext(), "#{myBean.sayHelloToName2}", String.class, new Class[0]);
// AjaxBehavior ajaxBehavior = new AjaxBehavior();
//ajaxBehavior.setListener( me );
//ajaxBehavior.addAjaxBehaviorListener( new AjaxBehaviorListenerImpl( me ) );
//creating the Ajax Behaviour method 2
AjaxBehavior sayhelloBehavior = new AjaxBehavior();
sayhelloBehavior.addAjaxBehaviorListener(new SayhelloListener());
sayhelloBehavior.setTransient(true);
//////////Adding the Ajax Behaviour
//for the method 1
//mynewcmdlink = (HtmlCommandLink) fc.getViewRoot().findComponent("form1").findComponent("mynewcmdlink");
//mynewcmdlink.addClientBehavior( "sayHello", ajaxBehavior);
//for the method 2
mynewcmdlink = (HtmlCommandLink) fc.getViewRoot().findComponent("form1").findComponent("mynewcmdlink");
mynewcmdlink.addClientBehavior("sayhelloBehavior", sayhelloBehavior);
RequestContext context = RequestContext.getCurrentInstance();
context.update("form1");
context.update("form1:foo");
}else
System.out.println("form1 is null!!");
}
} |
Merci pour votre aide !!!