I am having some problems getting RequestContext to work in the way required. Basically I have a login page, and if the login is unsuccessful, the page is redisplayed with error messages. This works correctly. If the login is successful, it navigates to one of three required pages. This is also successful.
However, I want a popup window to be displaying giving the user some information if the login is successful, but not if it fails.
I have the following fragment of code:
Code: Select all
public String login() {
// Other code
RequestContext context = RequestContext.getCurrentInstance();
context.execute("openPopup(750,650,'popups/gotoOption.f')");
return "option" + optNum;
}
My xhtml file has the following line of code:
Code: Select all
<p:commandButton id="login" value="Login" update="out" action="#{login.login}" styleClass="button"/>
Code: Select all
var winRef=null;
var winFeatures=",toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=yes,menubar=no";
function openPopup(w,h,f) {
close popup();
var x = (screen.width-w)/2;
var y = (screen.height-h)/2;
var settings="width="+w+",height="+h+",left="+x+",top="+y+winFeatures;
var ext = f.indexOf(".f");
if (ext < 0) winRef = window.open(f+".html",f,settings);
else winRef = window.open(f+"aces",f.substring(0,f.length-2),settings);
}
function closePopup() {
if (winRef == undefined) return;
if (!winRef.closed) winRef.close();
}
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
</h:head>
<h:body>
<h:form id="loginPopup" prependId="false">
<h:commandButton value="OK" onclick="self.close()"/>
</h:form>
</h:body>
At the moment the window is not opened if my login action navigates to the required page on success. If I replace:
Code: Select all
return "option" + optNum;
//by
return "";
// then add the method
public String gotoOption() {
System.out.println("Going to: option" + optNum);
return "option" + optNum;
}
Code: Select all
<h:commandButton value="OK" action="#{login.gotoOption}" onclick="self.close()"/>
My JavaScript and the popup window work correctly, as I have used the code elsewhere, and the RequestContext also works and invokes the JavaScript, but only if I do not try and navigate away from the page being displayed. If I do, it does not work. Also the popup window will not cause navigation to take place.
Does anybody know how to invoke conditionally a popup window, and when it is displayed, on clicking "OK" the window is closed and the main window then navigates to the required page? I also looked at <p:dialog/>, but that has not helped so far.
Any suggestions would be most welcomed.