JSF: auto-forward to a JSF action
I'm new to JSF, so please forgive me if this is a stupid question...!
I'm trying to set up a situation which is similar, if not identical, to the one described in this thread:
<http://forum.java.sun.com/thread.jspa?threadID=573490&messageID=3620546>
That is:
- the user presses a button which triggers the start of some background processing, and results in an action which causes the user to navigate to a new page (pleaseWait.jsp);
- the pleaseWait.jsp page displays some text and periodically refreshes;
- on each refresh, the application checks to see whether the background processing is complete;
- if it is NOT, a 'null' action is performed (i.e. the user remains on the "Please Wait" page);
- if it IS, a 'complete' action is performed (which should then cause the user to move on to another jsp).
Adapting the advice in the thread I mentioned above, I've created my pleaseWait.jsp to look (something) like this:
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<HTML>
<HEAD>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="REFRESH" CONTENT="1; URL=pleaseWait.jsp">
<title>Please wait...</title>
</HEAD>
<BODY>
<f:view>
<h:form onsubmit="#{waiter.ready}">
<h1>Please wait</h1>
<h:commandButton value="Give up" action="complete"/>
</h:form>
</f:view>
</BODY>
</HTML>
and a backing bean...
publicclass Waiter{
public String getReady(){
// logic here to determine if processing is complete (ready)
System.out.println(ready);
if (ready){
return"complete";
}else{
returnnull;
}
}
Now, the commandButton works a treat, but the automatic processing does not.
Looking at the generated HTML, I end up with a FORM element with onsubmit="complete" - but presumably this is attempting to execute the string as a piece of JavaScript...
So, do I need some JavaScript to back this up, or is there a better way?
To restate the problem: when a JSF-JSP loads, how can I have logic in a backing bean identify a JSF action to follow?
Thanks for any suggestions!
Alistair.
[3232 byte] By [
AJYa] at [2007-11-27 6:28:42]

# 2
Hello Flow!
Many thanks for the suggestion, but it doesn't work for me, I'm afraid.
In fact, it seems that although whatever JS code I return ends up in the rendered HTML, it is not being executed when the page refreshes (which, presumably, was the point of the solution given in the original thread I quoted above).
To check this out, I tied my bean property ("ready") to an onmouseover event of the command button instead:
<h:commandButton value="Give up" action="configComplete" id="button"
onmouseover="#{waitDetails.ready}"/>
Now, when the background processing is complete, moving the mouse over the button causes the navigation to occur. Not much use, but proves some kind of point, I suppose! :)
Any other suggestions? I'd like to avoid generating JavaScript if at all possible!
In case it makes a difference, I'm using Oracle's OC4J (yes, I know... boo!).
Cheers,
Alistair.
AJYa at 2007-7-12 17:52:12 >

# 5
Many thanks, Raymond.
I believe that I am okay, as far as your first point is concerned. All URLs of the form /faces/* are handled by the Faces Servlet.
And I agree about the use of onsubmit - this was based on the solution given in the thread I quoted originally... but I can only assume that there was more to it than was given in that thread.
So, my only real question is: how do I get the JSF framework to navigate to a new action when a page loads? i.e., in your example, when the page loads, how do I cause doTask() to be called, and the action it returns to be followed?
Do I need some javascript in place to periodically activate a control? Or is there a "nicer" solution?
Cheers,
Alistair.
AJYa at 2007-7-12 17:52:12 >

# 6
Okay, I've now got something that works. My JSP file now looks something like this:
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<HTML>
<HEAD>
<title>Please wait...</title>
<!-- script to activate button every second -->
<script type="text/javascript">
setTimeout('check();', 1000);
function check() {
document.getElementById('_id0:check').click();
}
</script>
</HEAD>
<BODY>
<f:view>
<h:form>
<h1>Please wait</h1>
<!-- button to allow user to give up waiting -->
<h:commandButton value="Give up" action="complete"/>
<!-- hidden button, 'clicked' by JavaScript, checks status of task -->
<h:commandButton value="Check" action="#{waitDetails.getReady}"
id="check"
style="display:none; visibility: hidden;"/>
</h:form>
</f:view>
</BODY>
</HTML>
my backing bean is as before.
One of the things that I don't like about this, is how my JavaScript has to refer to the ID which features in the output HTML ("_id0:check") rather than the one I've specified ("check"). I believe that JSF1.2 has a means to address this - but sadly I'm stuck with JSF1.1 at the moment.
Thanks to all for the suggestions!
Alistair.
AJYa at 2007-7-12 17:52:12 >

# 7
> One of the things that I don't like about this, is how my JavaScript has to refer to
> the ID which features in the output HTML ("_id0:check") rather than the one I've
> specified ("check"). I believe that JSF1.2 has a means to address this - but sadly
> I'm stuck with JSF1.1 at the moment.
If you use the tomahawk component library most (if not all) of their tags support a forceId attribute which causes the id to be used unmodified. They have versions of all the standard tags (like commandButton) with their extensions.
Thanks for posting your solution back to the forum, so many people fail to do that.