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]
# 1
Hey!In your situation i would try to return JS code that clicks the button.(getElementById().click())
Flow86a at 2007-7-12 17:52:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Anybody?Or, can anybody tell me what the "best practice" is, within the JSF framework, to achieve what I'm looking for?Again - any assistance very much appreciated!Thanks,Alistair.
AJYa at 2007-7-12 17:52:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

It looks as if you have the page set up to refresh to the request "pleaseWait.jsp". I would have expected you to use a JSF URL, for example "pleaseWait.jsf". I imagine it is possible that you have things configured so that this is actually a JSF operation, in which case, ignore the above.

Something else that looks funny; you have the form onsubmit attribute set to a backing bean method. The form onsubmit is for client side JavaScript.

I think what you need to do is have the same method for setting up the task and doing the waiting:

public String doTask()

{

if (!taskStarted)

startTask();

if (taskComplete())

return "complete";

else

return "wait";

}

Then set up navigation rules accordingly. The reload you have set up should call back into the doTask() method.

RaymondDeCampoa at 2007-7-12 17:52:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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.

RaymondDeCampoa at 2007-7-12 17:52:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Thanks, Raymond. I shall definitely investigate the tomahawk component library!Alistair.
AJYa at 2007-7-12 17:52:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...