I can't make my commandLink fire a bean action.
I've tried calling my bean method with <p:commandButton>, <h:commandLink>, <h:outputLink> and <p:commandLink>.
Also, i've used onclick, onmousedown, action and actionListener and none of those worked.
My code looks like this at the moment:
base.xhtml
<div>
<h:form>
<p:commandLink id="logoutButton" action="#{loginController.logout}">
<p:graphicImage name="/images/logout.png"/>
</p:commandLink>
</h:form>
<!-- Rest of code (layout with some layoutUnits) -->
</div
Bean method (Testing purposes, not the actual method):
public void logout() {
System.out.println("logged out");
}
According to BalusC, my question is duplicated. I had already searched for similar problems before making this question, and guess what? Nothing worked. Looking at same question that has already been answered, here are my answers to the previously described problems:
1 - The commandLink is inside a form.
2 - The commandLink is not in a nested form.
3 - No UIInput value validation/conversion is showing errors.
4 - The commandLink is not inside a iterating component.
5 - As the pages load, the button is not rendered. I've set it to render only if the user log-ins with a commandLink placed in that same form. It is indeed using AJAX.
6 - The onclick attribute of the UICommand component and the onsubmit attribute of the UIForm are not throwing a JavaScript error.
7 - base.xhtml has a <h:head>
8 - The parent of my <h:form> is not being updated/rendered.
9 - Im not using enctype="multipart/form-data" in my form.
10 - I'm not using actionListener.
11 - I'm not using PhaseListener or EventListener.
12 - There is no Filter or Servlet blocking the request to the FacesServlet.
Bonus:
Debugging with Firebug, i've noticed that when i click the logout button i'm getting a code 200 response from the server, meaning it is supposed to be OK.
My form is in a Template file (base.xhtml). After that form, i am using a <p:layout> element and my website code is inside it's <p:layoutUnit>.
Related
I have some problems with calling a bean method when clicking on Label.
When page is loading or refreshing click handler function pokus() is called, but when the label is clicked it isn't.
Part of my web page:
<h:form id="pokus">
<p:outputLabel id="pokus2" value="klikni" onclick="#pozadavkyBean.pokus(5)}"/>
</h:form>
and a method in bean:
public void pokus(int i){
System.out.printf("kliknuto sloupec:%d",i);
}
I've also tried it with:
<p:ajax event="click" listener="....
with the same result - method called on loading/refreshing but not when clicking
also tried others events: mousedown, mouseup, .... with same result
using PrimeFaces 5.0
If you will check official document of Primeface regarding Tag outputLabel.You can easily get attribute onclick used for
Client side callback to execute when component is clicked.
But here you are trying to run Managed bean method directly from onclick attribute while onclick used to call JavaScript functions.
As #Mahendran Ayyarsamy Kandiar mentioned and its simple thing outputLabel is not used to call any bean method. Its Just Simply used to show something in the page.In mean time for your requirement you can use CommandButton,CommandLink or some other component but its all depend upon your requirment etc.
I have a nested <h:form>, and inside the inner most <h:form> I have a <h:commandButton> like this,
<h:commandButton value="Next" actionListener="#{billController.saveFiles}"\>
public void saveFiles(ActionEvent event){
//code
}
But this method is not getting invoked by clicking the action button.
Can anyone give a solution to this?
Just stop nesting forms. Nested forms are invalid in HTML. You should never nest HTML <form> elements. As JSF is merely a HTML code generator, it's also not different from JSF side on. You should never nest JSF <h:form> components as well.
The browser behavior on nested forms is unspecified.
Can you post your whole jsp page? If button is not inside <h:form> tag it wont call action method
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.
Here is the problem: actionlistener does not want to be fired
#ManagedBean(name="hotelsController")
#SessionScoped
public class HotelsController implements Serializable {
public void requestHotelAvail(ActionEvent event) {
request = new Request(df.format(arrivalDate), df.format(departureDate));
}
}
and xhtml
<h:panelgroup id="rooms"/>
<h:form id="hotelSearch">
<p:commandButton actionListener="#{hotelsController.requestHotelAvail}" value="submit" update="rooms" />
</h:form>
I have tried everything I could search of changed #managedbean to #component set import to import javax.faces.event.ActionEvent;
But it still does not fire anything.
Form is in a p:accordion and when used with h:commandbutton it works fine
EDIT: sorry for mislead. rooms updates after click but actionListener is not fired. so rooms will not get any new data. Important code in requestHotelAvail needs to be fired before updating rooms and its not.
EDIT2: PrimeFaces 2.2.1 - I've read whole manual to primefaces but theres no explanation to this as I've done all that it states
I've tried using action instead of actionListener without ActionEvent but it never do anything. using <h:commandbutton action="#{hotelscontroller.requestHotelAvail}"/> works great but I want that ajax engine to refresh only that rooms panelgroup
UPDATE: Now it works. Form couldn't be in <p:accordion> but why and how to enable it there? Form now I'll work without it.
I suspect the different behavior from h:commandLink comes from ajax/non-ajax processing.
By default - if you don't use f:ajax - h:commandLink is non-ajax and entire page is rerendered. Primefaces p:commandLink is using ajax and you indicate rooms as component to be updated. In your case rooms is outside form so it should rather be addressed as :rooms (mind the colon) instead of just rooms.
update: have you tried ajax with h:commandLink? It would be:
<h:commandButton action="#{hotelscontroller.requestHotelAvail}" value="submit">
<f:ajax render=":rooms"/>
</h:commandButton>
Also I'm not that familiar with primefaces but maybe you can try to explicitly indicate the component to process with additional process="#this" - although I would assume this to be default as in basic library.
You try to inspect the response:
Open Chrome or Firefox -> Inspect Element -> Network and follow the ajax call.
I have a h:commandlink control in page1. the control uses f:ajax to call to the following h:panelgroup :
I have a h:panelgroup control in page2 (a snippet), which has a ui:include within it.
I have a h:panelgroup control in page3 (a snippet), which has a ui:include within it.
Now according to the choices made on page1, I would like to switch the snippets by clicking on the h:commandlink control.
I have a BIG problem there: it seems that only if I click twice on the commandlink, only then the snippet changes - and not on one click.
I have tried to remove the f:ajax to render the panelgroup, and still it does not work...
There are two potential causes of this problem.
The <f:ajax> is fully re-rendering another <h:form> than where it is sitting in. This way the view state of the other form will get lost which would require invoking the action on the other form twice before it really get executed.
The solution is to not re-render the other <h:form>, but only some container component in that form. E.g.
<h:form id="otherForm">
<h:panelGroup id="content">
...
<h:panelGroup>
</h:form>
with
<f:ajax render=":otherForm:content" />
When there's a rendered attribute on the <h:commandLink> or any of its parent components, then it must evaluate true during the apply request values phase of the postback request in order to get JSF to invoke the bean action associated with the <h:commandLink> during the invoke action phase of that request. Perhaps the bean is request scoped and/or some odd/illogical flow inside the bean caused that the rendered attribute is not properly been preserved.
Best is to maintain those rendered conditions in a #ViewScoped bean and let its action methods return void or null so that the bean lives as long as you're interacting with the same view. Change the rendered conditions during action methods only and not inside setters/getters or something.