I have a custom component that includes a logout button (using a commandButton) which is used on several pages of a Force.com site. I have written a controller for this component which includes a simple action for the button which sets a cookie and then logs the user out by returning a PageReference to the login page.
However, when I click the logout button, the user is not redirected to the login page, but rather to the actual parent page of the custom component (i.e. the page that the user is already on). On inspection of the generated HTML I see that the component has a form tag which has an action set to the url of the parent page.
So when I click on the commandButton the button must be submitting this form rather than using the returned PageReference from the controller. I can see that the component controller action is running and is returning the correct url.
I wonder first of all why the form for this component is being generated with the action having it's parent page's url, and secondly if this is obligatory then how I can prevent this.
public PageReference setCookieAndLogOut() {
Util u = new Util();
u.setUserLoggedOutAndExpireWebsiteSession(sessionId);
PageReference pRef = new PageReference('/apex/loginpage');
pRef.setRedirect(true);
return pRef;
}
<apex:component controlle="mycontroller">
<apex:form>
<apex:commandButton action="{!setCookieAndLogOut}" value="Log Out" />
</apex:form>
</apex:component>
Generated HTML:
<form id="thePage:j_id6:j_id7:j_id11" name="thePage:j_id6:j_id7:j_id11" method="post" action="/homepage" enctype="application/x-www-form-urlencoded">
<input type="submit" name="thePage:j_id6:j_id7:j_id11:j_id13" value="Log Out">
</form>
Many thanks