jsf 2 - why action in doesn't work inside a composite component -
jsf 2 - why action in <h:commandLink> doesn't work inside a composite component -
i have simple jsf 2/facelets page looks this:
<ui:repeat value="#{mybean.names}" var="_name"> <h:commandlink value="#{_name}" action="#{mybean.sayhello(_name)}"> <f:ajax execute="@this"/> </h:commandlink> <br/> </ui:repeat>
the backing bean provides java.util.list<string>
names , action-method prints "hello <name>"
message standard output.
this works fine. list of names in browser , click fires action-method says hello specified name.
the problem arises, when want set code in composite component iteration , renders actual link via facet:
<ui:component xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:cc="http://xmlns.jcp.org/jsf/composite"> <cc:interface> <cc:attribute name="value" type="java.util.list" required="true" /> <cc:facet name="content" /> </cc:interface> <cc:implementation> <ui:repeat value="#{cc.attrs.value}" var="_name"> <cc:renderfacet name="content"/> </ui:repeat> </cc:implementation> </ui:component>
i utilize composite component this:
<my:mycomp value="#{bean.names}"> <f:facet name="content"> <h:commandlink value="#{_name}" action="#{bean.sayhello(_name)}"> <f:ajax execute="@this"/> </h:commandlink> <br/> </f:facet> </my:mycomp>
in browser list of names looks before. clicking link renders "hello null"
message. _name
resolved correctly in value
attribute of <h:commandlink>
not in action
attribute.
i tried using actionlistener
instead of action
or listener
attribute <f:ajax>
tag no difference.
could shade lite on issue?
my environment:
wildfly 8.1 with jsf 2.2.6 (mojarra)
the issue has scope of variable in case _name
evaluated 1 time when <ui:repeat/>
beingness processed. in case, ran code , produced hello john
though other names in list. around this, introduced <f:param/>
contain value of _name
, , modified code follows:
<h:form> <my:mycomp value="#{bean.names}"> <f:facet name="content"> <h:commandlink value="#{_name}" action="#{bean.sayhello()}"> <f:param name="name_" value="#{_name}"/> <f:ajax execute="@this"/> </h:commandlink> <br/> </f:facet> </my:mycomp> </h:form>
i modified sayhello()
method follows @requestscoped
bean:
@managedproperty(value = "#{facescontext}") private facescontext facescontext; public void setfacescontext(facescontext facescontext) { this.facescontext = facescontext; } public void sayhello() { map<string, string> params = facescontext.getexternalcontext() .getrequestparametermap(); string name = params.get("name_"); system.out.println("hello " + name); }
you alter shorter in @viewscoped
bean to:
public void sayhello() { map<string, string> params = facescontext.getcurrentinstance() .getexternalcontext().getrequestparametermap(); string name = params.get("name_"); system.out.println("hello " + name); }
the final result prints out names correctly.
jsf-2 composite-component
Comments
Post a Comment