简体   繁体   中英

Get hidden value set by JavaScript in JSF backing bean

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?

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.

You will need to use a4j's commandButton with a actionParam inside. Refer to this answer: https://stackoverflow.com/a/10534779/268016

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM