Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

create component with ajax and listener behavior in java

 
yacel el
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I'm new in jsf2 mybe it's a basic Q but I will ask it
I implemented the following jsf component file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:composite="http://java.sun.com/jsf/composite"
xmlns:hx="http://www.ibm.com/jsf/html_extended"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:btl="http://java.sun.com/jsf/composite/components">

<composite:interface>
<composite:attribute name="id" required="false" />
<composite:attribute name="value" required="true" />
<composite:attribute name="isRendered" required="true" />
<composite:attribute name="viewContent" required="true" />
<composite:attribute name="compName" required="true" />
<composite:attribute name="onclickCommand" required="false" />
</composite:interface>
<composite:implementation>
<li><h:commandLink id="#{cc.attrs.id}" value="#{cc.attrs.value}" onclick="#{cc.attrs.onclickCommand}"
rendered="#{cc.attrs.isRendered}">
<f:param name="viewContent" value="#{cc.attrs.viewContent}"></f:param>
<f:param name="compName" value="#{cc.attrs.compName}"></f:param>
<f:param name="id" value="#{param['id']}"></f:param>
<f:ajax render=":#{id:clientId('tikLakohachContentId') }" listener="#{contentBean.viewContentString }" execute="@form" ></f:ajax>
</h:commandLink><composite:insertChildren /></li>
</composite:implementation>

</ui:composition>

and I want to create the same component in java
I have problems to deal with the ajax
I don't know how to implement the f:ajax in java
I know that there is an AjaxHandler class for that but I do not know how to create a new one because the constractor ask me to implement TagConfig which I do not have
the following file is the component in java but I need the last part, the ajax implementation part:

@FacesComponent(value="org.btl.tevel.core.components.DynamicUImenuItemComponent")

public class DynamicUImenuItemComponent extends HtmlCommandLink implements Cloneable{

private String viewContent;

private String compName;

//ResponseWriter responseWriter;

...
@Override
public void encodeBegin(FacesContext context) throws IOException {

ResponseWriter responseWriter = context.getResponseWriter();
Collection<String> executes = new ArrayList<String>();
Collection<String> renders = new ArrayList<String>();
executes.add("@form");
//UIComponent y = context.getViewRoot().findComponent("tikLakohachContentId");
UIComponent t = findComponent(context.getViewRoot(),"tikLakohachContentId");
renders.add(":"+t.getClientId());
AjaxBehavior ajaxBehavior = new AjaxBehavior();
ajaxBehavior.setRender(renders);
ajaxBehavior.setExecute(executes);
ajaxBehavior.setDisabled(false);
ajaxBehavior.setTransient(false);
String action = "#{tikLakohachContentBean.viewContentString}";
Collection<ClientBehaviorContext.Parameter> paramList = new ArrayList<ClientBehaviorContext.Parameter>();
paramList.add(new ClientBehaviorContext.Parameter("compName", (String)this.compName));
paramList.add(new ClientBehaviorContext.Parameter("viewContent", (String)this.viewContent));
populateDynamicParameters(paramList);

ClientBehaviorContext behaviorContext = ClientBehaviorContext.createClientBehaviorContext(context, this, "action", getClientId(context), paramList);
String[] splitBuffer = getClientId().split(":");
responseWriter.startElement("a", this);
responseWriter.writeAttribute("id",getClientId(context),"id");
if((String)this.viewContent != null){
StringBuffer changeClassStr = new StringBuffer();
changeClassStr.append("changeSelectedClass(").append("'a'").append(",'").append("rightSideFormId").append("'").append(",'").append(splitBuffer[splitBuffer.length-1]).append("'").append(",'panel-tab-slctd panel-tab-ico1'").append(")");
responseWriter.writeAttribute("onclick", ajaxBehavior.getScript(behaviorContext), null);
responseWriter.writeAttribute("class", "panel-tab","class");
responseWriter.writeAttribute("className", "panel-tab","className");
}
else{
responseWriter.writeAttribute("style","cursor:normal","style");
}
responseWriter.write((String)super.getValue());

}



private void populateDynamicParameters(Collection<ClientBehaviorContext.Parameter> paramList){
if(this.parameterList != null && this.parameterList.size() > 0){
for(int i=0; i < this.parameterList.size(); i++){
TreeMenuParameter currParam = (TreeMenuParameter)parameterList.get(i);
paramList.add(new ClientBehaviorContext.Parameter((String)currParam.getKey(), (String)currParam.getValue()));
}
}
}

@Override
public void encodeEnd(FacesContext context) throws IOException {
ResponseWriter responseWriter = context.getResponseWriter();
responseWriter.endElement("a");

}

public static UIComponent findComponent(UIComponent base, String id) {

// Is the "base" component itself the match we are looking for?
if (id.equals(base.getId())) {
return base;
}

// Search through our facets and children
UIComponent kid = null;
UIComponent result = null;
Iterator kids = base.getFacetsAndChildren();
while (kids.hasNext() && (result == null)) {
kid = (UIComponent) kids.next();
if (id.equals(kid.getId())) {
result = kid;
break;
}
result = findComponent(kid, id);
if (result != null) {
break;
}
}
return result;
}

public Object clone(){
try{
DynamicUImenuItemComponent cloned = (DynamicUImenuItemComponent)super.clone();
return cloned;
}
catch(CloneNotSupportedException e){
System.out.println(e);
return null;
}
}


}

please help...
Thanks all.
Yacel.
 
yacel el
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,
Is any one can help here???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic