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!!");
}
} |
Partager