We are upgrading from jsf 1.2 to jsf 2.
We are using apache myfaces 2.1 and rich faces 4.3.
Below is the xhtml code :
Prior to Migration :
<h:selectBooleanCheckbox id="comp1" value="#{bean.select}">
<a4j:support event="onclick" ajaxSingle="true" actionListener="#{bean.processInput}" reRender="compId">
<f:param name="name" value="paramValue"/>
</a4j:support>
</h:selectBooleanCheckbox>
We are passing one parameter with and accept the parameter in actionListener method as : (String)context.getExternalContext().getRequestParameterMap().get("name1");
After Migration :
<h:selectBooleanCheckbox id="comp1" value="#{bean.select}">
<a4j:ajax event="click" listener="#{bean.processInput}" execute="#this" render="compId"/>
</h:selectBooleanCheckbox>
I want to pass a parameter to bean.processInput method which has following signature :
public void processInput(AjaxBehaviorEvent event){
According to this post - Issues in passing parameter in f:ajax , we cannot use <f:param> (its not working
also) and we are not using EL 2.2 which rules out passing parameter in method signature.
Since we cannot use context.getApplication().evaluateExpressionGet due to constraints in our xhtml page only option available is
use <a4j:param name="" value="" assignTo=""/>.
But this needs to have one variable defined in bean requiring code change.
So my question is can we pass a parameter from UI to listener in second case and without changing the code.
You can use f:attribute like this :
<h:selectBooleanCheckbox id="comp1" value="#{bean.select}">
<a4j:ajax event="click" listener="#{bean.processInput}" execute="#this" render="compId"/>
<f:attribute name="name" value="paramValue" />
</h:selectBooleanCheckbox>
And use it in your bean :
public void processInput(AjaxBehaviorEvent event)
{
System.out.println(event.getComponent().getAttributes().get("name"));
}
Make sure that f:attribute component is not a child component of a4j:ajax.
it should be immediate child component of h:selectBooleanCheckBox, if not listener wont trigger.
Related
This question already has an answer here:
How to send form input values and invoke a method in JSF bean
(1 answer)
Closed 6 years ago.
I am using Primefaces 5.0 and JSF 2.0.
Hello I have a defined as follows:
<p:dataTable id="activePackagesBeanTable"
value="#{packageManagerLocal_GetAllPackages.reviewList}"
var="activePkg"
selection="#{packageManagerLocal_GetAllPackages.selectedPackages}"
rowKey="#{activePkg.hashCode()}">
<p:ajax event="rowSelectCheckbox" update=":mainPanel:delegateButtonForm" />
<p:ajax event="rowUnselectCheckbox" update=":mainPanel:delegateButtonForm" />
<p:ajax event="toggleSelect" update=":mainPanel:delegateButtonForm" />
<p:column selectionMode="multiple" style="width:16px;text-align:center"/>
</p:dataTable>
and a that performs an action on that selection list and is defined as follows:
<p:commandButton id="delegateDoAction1" styleClass="button_blue"
type="submit" ajax="false" immediate="true" value="Delegate"
action="#{packageManagerLocal_GetAllPackages.reassign}"
title="#{hints_bundle['button.package.reassign']}"
execute="activePackagesBeanTable">
</p:commandButton>
Inside the backing bean, I have the function reassion() defined as follows.
List<MyObject> selectedPackages;
// Getters and setters for selectedPackages
.....
public String reassign() {
String method = "reassign";
logger.entering(this.getClass().getName(), method);
FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("packageList", getSelectedPackages());
logger.entering(this.getClass().getName(), method, "SWITCH");
return "reassign";
}
However, when I debug with Eclipse my getter is returning a null value for selectedPackages? Why is that? What am I doing wrong?
I found the solution. The commandbutton and the dataTable need to be in the same form for the list to update.
We are migrating from jsf 1.2 to jsf 2 and use following components.
Apache myFaces 2.2.12
Rich Faces 4.3.4
I am facing an issue while replacing <a4j:support> with <a4j:ajax> for <h:selectBooleanCheckbox>
Following is the xhtml code.
<h:panelGrid id="info" columns="2">
<c:forEach items="#{bean.list}" var="provider">
<h:selectBooleanCheckbox id="#{provider.name}" value="#{provider.select}"
disabled="false">
<a4j:ajax event="change" listener="#{bean.processProvider}" execute="#this" render="info">
<f:param name="name" value="#{provider.name}"/>
</a4j:ajax>
</h:selectBooleanCheckbox>
<h:outputText value="prod1"/>
</c:forEach>
<h:panelGrid>
On checking the selectBooleanCheckbox , listener is not getting called. I have tried following things :
1)Change listener method argument from ActionEvent to AjaxBehaviorEvent.
2)Change listener method to accept no argument.
3)a4j:ajax events tried - change,click,valueChange
Following is the method signature for listener method.
public void processProvider(AjaxBehaviorEvent event){
}
Nothing seems to be working .
Can anyone pleas help ?
I have a p:commandLink in my xhtml with the value toggling between "Show"/"Hide".
Is there any way by which I can get the value of this commandlink from the backing bean?
I mean, I want to know what value the command link is showing currently i.e. Show/Hide?
To the point, the invoking component is just available in ActionEvent argument:
<h:commandLink id="show" value="Show it" actionListener="#{bean.toggle}" />
<h:commandLink id="hide" value="Hide it" actionListener="#{bean.toggle}" />
with
public void toggle(ActionEvent event) {
UIComponent component = event.getComponent();
String value = (String) component.getAttributes().get("value");
// ...
}
However, this is a poor design. Localizable text should absolutely not be used as part of business logic.
Rather, either hook on component ID:
String id = (String) component.getId();
or pass a method argument (EL 2.2 or JBoss EL required):
<h:commandLink id="show" value="Show it" actionListener="#{bean.toggle(true)}" />
<h:commandLink id="hide" value="Hide it" actionListener="#{bean.toggle(false)}" />
with
public void toggle(boolean show) {
this.show = show;
}
or even just call the setter method directly without the need for an additional action listener method:
<h:commandLink id="show" value="Show it" actionListener="#{bean.setShow(true)}" />
<h:commandLink id="hide" value="Hide it" actionListener="#{bean.setShow(false)}" />
As #BalusC suggested, your approach is not a good solution. But if you really want to do that, you can bind the component (p:commandLink) to your backingbean, as seen in What is the advantages of using binding attribute in JSF?.
After the component was bound, you can access the value attribute from the p:commandLink.
<h:selectBooleanCheckbox value="#{searchView.allSelected}" onclick="toggleChecked(this.checked)">
<f:ajax execute="#this" event="blur" listener="#{searchController.selectRows}" />
</h:selectBooleanCheckbox>
I have a check box and I need to call a controller event on blur, is it the right way of calling the event through listener
I am getting error as does not have the property 'selectRows'
Your function in the bean must have this signature :
public void selectRows(AjaxBehaviorEvent event)
{
...
}
You are probably missing the parameter.
More info : f:ajax
I would like to nest multiple setPropertyActionListener's in my commandLink but only one works. How do you attempt this? This command link sets properties and then opens a dialog so its basically initializing the dialog.
How is this accomplished?
<p:commandLink update=":dreamWebSearchFrm" value="#{bundle['dreamModify.search.link.TEXT']}" oncomplete="webSearchDlg.show()">
<f:setPropertyActionListener value="false" target="#{dreamSearchBean.shouldRender}"/>
<f:setPropertyActionListener value="true" target="#{dreamSearchBean.shouldRender1}"/>
</p:commandLink>
You could make use of EL parameters and call a single method on your bean. From that method, update whatever you want.
e.g.
#{dreamSearchBean.shouldRenderInit(false, true)}
In your bean:
public void shouldRenderInit(boolean one, boolean two) {
setShouldRender(one);
setShouldRender1(two);
}
I try with
<p:commandLink update=":dreamWebSearchFrm" value="#{bundle['dreamModify.search.link.TEXT']}" oncomplete="webSearchDlg.show()">
<f:setPropertyActionListener value="#{false}" target="#{dreamSearchBean.shouldRender}"/>
<f:setPropertyActionListener value="#{true}" target="#{dreamSearchBean.shouldRender1}"/>
</p:commandLink>
It is correct with JSF 1.2 and richfaces.
I use the following command to set property value
<f:setPropertyActionListener
target="#{facRwMappingListBean.facRwMapping.finalActionCdDesc}"
value="#{gridData.finalActionCdDesc}" />
Mine is working fine. Your code looks correct except the value="true" part. Try to pass boolean value in the following way.
value=#{"true"}