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

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

Related

JSF outputlabel click event [duplicate]

This question already has an answer here:
How to call JSF backing bean method only when onclick/oncomplete/on... event occurs and not on page load
(1 answer)
Closed 5 years ago.
first of all, I am very new to JSF and web development.
What I want to do is to print a message in the console if the user click an outputlabel.
{businessBean.select()} gets called only on page refresh and not when I click.
<h:outputLabel styleClass="cat" id ="businesstasks" onclick="setColor('businesstasks'); #{businessBean.select()}">
Business and productivity
</h:outputLabel>
My test bean:
#ManagedBean(name="businessBean")
#SessionScoped
public class businessBean {
public void select(){
System.out.print("Test");
}
}
I get "Test" only when I refresh the page but I want to get it on every click.
Thanks for helping!
Is there a reason you 'need' the outputLabel to execute an action? Because onclick will execute it as a javascript, and that interaction can cause issues if you're calling the bean action method. You could use <h:commandLink action="#{businessBean.select}" /> within your output label.

validator for input text using the backing bean method not working [duplicate]

This question already has answers here:
JSF custom validator not invoked when submitted input value is null
(2 answers)
Closed 5 years ago.
I have a form with h:inputText and have the attribute validator with the backing bean method name as value, example:
h:inputText id="poolmanagementinput" validator="#{poolManager.validatePoolManagement}"
Now I have method in backing bean named poolManager in following format:
public void validatePoolManagement(final FacesContext context, final UIComponent component,
final Object value) {
... }
I expected this method to be called in the validation phase in JSF cycle. But to my surprise this method is not being called and validation is not happening. Could anyone point out any missing point or direct me to a soultion.
Note: Just as a side note the input is placed inside a composite:implementation.
Thanks in advance.
Cheers!
Incase if you have forgot add these attributes between body and form like this
<h:body>
<h:form>
<your ui item>
</h:form>
</h:body>
sorround your UI attribute with them. <h:form> has to be parent tag for all JSF UI components that can participate in HTML form submission.
I have encountered same issue and this has resolved issue.
I know it is a old question but answering it for the people who have the same problem.
JSF is not executing the validators if the value is blank.
You need to use required=true for validation blank.
for more reference JSF custom validator not invoked when submitted input value is null

JSF difference between action, actionlistener, onClick [duplicate]

This question already has answers here:
Differences between action and actionListener
(4 answers)
How to invoke a bean action method using a link? The onclick does not work
(3 answers)
Closed 5 years ago.
I am using JSF in my project. I am using a context menu from PrimeFaces. I see in p:menuItem we have action, actionListener, onclick methods. So my question is: When do I have to use action, actionListner, onclick and what is the order of execution?
onclick will be executed first. It is used to call a javascript function.
actionListener is used when you want to have some ajax call to a
method. That method should have the return type void, the method
either take an ActionEvent as argument or no argument; it can
also be used for a non-ajax call but then the page will be refreshed.
action is used to navigate to a different page; the method should
have the return type String.
This question has been asked before.
Action is used when you want to call a method in your backing bean. e.g
action="#{myBean.myMethod}"
the code for bean would be like
#ManagedBean(name = "myBean", eager = true)
#ViewScoped
public class MyBean{
myMethod(){
// your method code here
}
}
How ever action listener does the same except that it is triggered with an event
myMethod(Event e){
// your method code here
}
Note that event can be of any type.
onclick works before sending the ajax request i dont have much knowlegde aboput it... i only used it for the UI purposes for example closing a dialog box on clicking a button
<p:commandButton id="cancel" value="Cancel"
icon="ui-icon ui-icon-arrowreturnthick-1-w"
style="float:right;" onclick="PF("dlg").hide()" type="button">
</p:commandButton>
SEE ALSO
Differences between action and actionListener

Commandlink with ajax works only on second click [duplicate]

This question already has answers here:
h:commandButton/h:commandLink does not work on first click, works only on second click
(2 answers)
Closed 6 years ago.
What I'm trying to do is just a simple logout function. The main page will call a template page; consist of header and content. The header contains the logout button. There will be 2 forms in the page, one in the header (logout button), one in the content (few buttons,links, etc).
My problem is the page refresh when first click on the logout link and only proceed after second click. The problem starts to appear when I include primefaces or ajax code inside the content form. When I remove that particular code, the logout works as intended.
The logout form in main template templateUser.xhtml:
<h:form id="logout">
<h:commandLink action="#{tenantController.customLogout(e)}"
id="logoutBtn" immediate="true" value="Logout" />
</h:form>
The backing bean:
public String customLogout(ActionEvent e) {
return "login.xhtml";
}
FYI: the customLogout method also consist of session destroy, but I just put page redirect here.
In template client, the template is specified as:
<ui:composition template="/templateUser.xhtml">
As for the JSF library, currently use jsf 2.0
Solutions I've tried:
immediate="true"
put id for both form and commandLink (template form & content form)
use primefaces (<p:commandLink> with ajax false ==> this returb to the previous page; the method in backed bean is not running.
Below are few links I tried here:
commandButton/commandLink/ajax action/listener method not invoked or input value not updated
How can I prevent page refresh on click of <h:commandLInk>
JSF PrimeFaces p:commandLink won't redirect to new page?
The problem starts to appear when I include primefaces or ajax code inside the content form. When I remove that particular code, the logout works as intended.
Your problem is caused by point 7 as described in commandButton/commandLink/ajax action/listener method not invoked or input value not updated. When you fire an ajax request using <f:ajax> and performs an update which also covers all other forms, then all those other forms will lose their view state. This causes that the 1st submit will always "do nothing", but the next submits will work, which matches exactly your problem symptoms.
You can solve this problem in one of the following ways:
Explicitly specify the client ID of all other forms in the <f:ajax render>. You can specify multiple client IDs space separated. E.g.
<f:ajax ... render="someComponent otherComponent :loginForm" />
Use the JavaScript based fix as proposed in this answer: h:commandButton/h:commandLink does not work on first click, works only on second click.

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