Difference between calling back bean method using parenthesis and without parenthesis in jsf? [duplicate] - jsf

This question already has answers here:
Differences between action and actionListener
(4 answers)
Closed 3 years ago.
what is the difference between
<h:commandLink actionListener="#{serviceProviderBean.method}" value="Save" />
and
<h:commandLink actionListener="#{serviceProviderBean.method()}" value="Save" />
is both invoke same method or any error in above code

Second will not work under tomcat6, it will work with tomcat7/jSF2

Both are fine. The second one is being used for passing params like
<h:commandLink actionListener="#{serviceProviderBean.save(someBean.someOption)}"
value="Save" />

I think you also have to use the method with braces, when you want to use a method returning a boolean value, but don't have a matching property defined in the bean.
I had that situation today.
My xhtml page has a <h:panelGroup ...> with the rendered="#{bean.isLoggedIn}" attribute. The isLoggedIn method, calls the method of the boundary, so the bean does not have the matching property private boolean isLoggedIn.
I got a exception because of the missing property.
After adding the braces to the rendered attribute making it to rendered="#{bean.isLoggedIn()}" it's working correctly.
Anyway. The method got removed by now, because my bean should not do business logic stuff :D

Related

commandLink does not call action method with correct backingBean [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 5 years ago.
I am working with JSF trying to do a .xhtml as simple as :
<ui:composition ...>
<span class="fr">
<a4j:commandLink value="#{msg.label_return}"
action="#{backingBeanRef['navigateBack']()}"
rendered="#{backingBeanRef['returnValueStackBean']['returnValueStack']['size']() ne 0}"
render="#{render}"/>
</span>
</ui:composition>
so I can include it in several views. It works as expected in most of them but one, in which the action method is never called.
I have tried to call the method without parameterization (#{myBean.navigateBack()}) and it still doesn't works.
I have also tried to write #{backingBeanRef'navigateBack'} above the commandLink and the method is called so the backingBeanRef is correct and everything should work but it doesn't.
At this point I have no clue about what is happening, does anyone knows?
Thanks in advance
I had two <h:form> in my code. Once I removed one of these, this got fixed.

PrimeFaces dataTable method called many times [duplicate]

This question already has answers here:
Defining and reusing an EL variable in JSF page
(2 answers)
Closed 6 years ago.
I have a datatable with a column:
<p:dataTable value="#{cc.attrs.bean.model}"
...
<p:column style="width:#{bean.getWidth('colDate', 55)}px;"
It seems that the bean.getWidth method is called for every row in the table. Thus, when having 100 rows, the method is called a hundred times. I expected the method to be called only once.
Am I wrong?
No, it's right.
You can cache the value into your bean or use JSTL
<c:set var="width" value="#{bean.getWidth('colDate', 55)}" />
<p:dataTable value="#{cc.attrs.bean.model}"
...
<p:column style="width:#{width}px;"
You can find more infos here https://stackoverflow.com/tags/jstl/info.
Don't forget the namespace .
you can save result of getWidth in managed bean attribute then use this in column style

How to execute a bean method inside foreach without button or link in jsf?

I am looping a list containing strings in foreach, so in every iteration the variable in foreach loop should be passed as a parameter to a bean method written inside the foreach loop. I searched many sites but everywhere I am finding solution that bean method cannot be executed without commandbutton or link. Is there any solution to execute bean method without any commandbutton or link.
<c:forEach var="name" items="#{bean.stringList}"
varStatus="loopCounter">
<!--I have to execute the method here passing "name" as parameter-->
</c:forEach>
Since EL 2.2 you can invoke non-getter methods with arguments. So you could use:
<c:forEach var="name" items="#{bean.stringList}"
varStatus="loopCounter">
<c:set var="dummy" value="#{bean.yourMethod(name)}" />
</c:forEach>
However, you are most likely are trying to solve something here that can be done in more elegant ways like a PhaseListener, a #PostConstruct method, a f:viewAction, etc.
See also
How to implement a PhaseListener which runs at end of lifecycle?
Why use #PostConstruct?
When to use f:viewAction / preRenderView versus PostConstruct?

Call same bean method from multiple onclick with different parameters not working [duplicate]

This question already has an answer here:
Multiple action listeners with a single command component in JSF
(1 answer)
Closed 6 years ago.
I'm using Primefaces and jsf in my application. I'm using mutiple commandlink with onclick function. All the onclick function call the same bean method but parameters are different. Pages are working fine & passing parameters also different even though all the pages are showing same metrics.
Based on parameter should display different metrics but not working properly.
XHTML:
<h:commandLink value="Defects"
action="#{loginbean.setCurrPage('metrics','defects.xhtml')}"
onclick="#{reportsBean.MetricPage('defect')}"
rendered="#{sub_selected =='defects'}" />
<h:commandLink value="Test Case"
action="#{loginbean.setCurrPage('metrics','testcase.xhtml')}"
onclick="#{reportsBean.MetricPage('testcase')}"
rendered="#{sub_selected =='testcases'}" />
Bean:
public void MetricPage(String ipageid) {
metricssection = metricsProcess.getMetricsProcess().getUserMetrics(ipageid, SessionMgr.getProj());
metricsfeature = metricsProcess.getMetricsProcess().getMetricFeatures(ipageid, SessionMgr.getPro());
}
How can i resove the problem?
onclick is a client side event. You can only call Javascript functions from onlcick. Not methods from Managedbean.
Since you tagged this question as Primefaces, I assume you are using Primefaces. In that case you can use p:remoteCommand
See this on how to use p:remotecommand

JSF2 ignores Action attribut [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 2 years ago.
my xhtml code:
<h:commandLink action="#{detailController.updateProject}" class="positive" >
<h:graphicImage library="img" name="tick.png" alt=""/>
<h:outputText value="Save" />
</h:commandLink>
This action (updateProject()) is not being called from JSF framework! Even if I delete it in the managedBean there is no exception thrown.
Does anybodyelse has had problems like that? I can't even explain that -.- I mean this action ethod is there!
ADD: Yes it is in a h:form tag! But I have two forms in that view! May that be the problem?
ADD2: I should also mention that if I hit the button it throws me back to the previous view! So my action method is being ignored and instead it opens another view ?!?!
To provide more information, my page is built like that:
panelGroup name=show rendered=!controller.edit
form
buttons
outputtext
/form
/panelGroup
panelGroup name=edit rendered=controller.edit
form
buttons
inputText
/form
/panelGroup
So I have both, edit and show of one entity at one file! But only the buttons in the bottom form show that strange behaviour (see above).
Answering BalusC:
1. I use two forms (they aren't nested!)
2. In the bottom form I had already placed a h:messages
I'm gonna try putting my controller into viewScop for checking 3 and 4
I don't know how to check 5.
Thank you for that..
This can have a lot of possible causes. #romaintaz has mentioned only the most obvious one (the most common beginner's mistake): UICommand components must be placed inside an UIForm component.
There are however more causes:
You have multiple nested forms. This is illegal in HTML. The behaviour is dependent on the webbrowser used, but usually the button won't do anything. You may not nest forms, but you can use them in parallel.
A validation or conversion error has occurred which is not been catched by a h:message. Normally this is logged to stdout, but you can also use h:messages to get them all.
The UICommand component is been placed inside an UIData component (e.g. h:dataTable) whose value is not been preserved the right way during the subsequent request. If JSF cannot find the associated data row, the action won't be invoked. Putting bean in view scope should help a lot.
The component or one of its parents has a rendered or disabled attribute which evaluated false during apply request values phase. JSF won't invoke the action then. Putting bean in view scope should help a lot.
Some PhaseListener, EventListener, Filter or Servlet in the request-response chain has changed the JSF lifecycle to skip the invoke action phase or altered the request parameters so that JSF can't invoke the action.
Just a quick question: is your <h:commandLink> nested inside a <h:form>?
If this is not the case, you must include your command link inside a form element, otherwise it will not work.
Just for code simplification, you can use the value attribute instead of adding a <h:outputText> component:
<h:commandLink action="#{detailController.updateProject}" class="positive" value="Save">
<h:graphicImage library="img" name="tick.png" alt=""/>
</h:commandLink>
Unfortunately, I don't know where the mistae was. I guess it was about wrong my JSF code.
I solved this problem by simplifying my code. From that xhtml page and that one controller I made 3 xhtml-pages and 3 Controller. After refactoring all that my code looks much easier and it works now :-)
Thank you for your helpful suggestions

Resources