views:

219

answers:

2

I want to get a JavaScript value in a JSF backing bean. I've tried the following:

JSF:

<h:inputHidden id="fileName" value="#{TestBean.fileName}" />
<a4j:commandButton id="button" value="Send Mail" action="#{TestBean.send}" onclick="onCall()"/>

Bean:

public String send() {
    System.out.println("File Name: " + fileName);
}

JS:

function onCall(){
    //value changes dynamically everytime this function is called
    document.getElementById('case:fileName').value = '123';
}

Problem with this code is: for the first time it is fetching empty string in backing bean from the second time onwards it is getting previously generated value in the java script function.

Where did I go wrong and how can I solve it?

A: 

I just tried this code and it works fine on my machine. As I can see the code is not exactly copy-pasted, because there is mistake in send() method. It declares return type String, but it doesn't return anything.

Maybe your mistake is somewhere in the part of code you didn't copy here.

amorfis
I think it's a red herring. It at least doesn't match with the problem description: "each click gives the previousy set value" :) If the return value was really missing, the code simply won't have compiled. By the way, I've seen more than often issues with `a4j:commandButton` like that, see also [this topic](http://forums.sun.com/thread.jspa?threadID=5390192). I won't be surprised if that's again the case. I'd upgrade RichFaces or report it.
BalusC
A: 

Check the generated HTML output. Open the page in your webbrowser, rightclick and choose View Source. The onclick attribute must look like this:

onclick="onCall(); A4J.AJAX.Submit(...);"

and thus NOT

onclick="A4J.AJAX.Submit(...); onCall();"

If that was the case, then it explains perfectly the problem you're having. I'd upgrade RichFaces to the latest available version, maybe it's already fixed. If not, then I'd report it to the RichFaces guys over there at JBoss.org.

BalusC
Hi BalusC this is the generate html code as you said , im using jsf 2.0 my faces thanks for your support .<input id="case:sendMail" name="case:sendMail" onclick="onCall();A4J.AJAX.Submit('case:j_id_jsp_24982259_24','case',event,{'parameters':{'case:sendMail':'case:sendMail'},'actionUrl':'/SEAD/faces/trends.jsp'});return false;" value="Send Mail" type="button" />
ravi