• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get back bean value in javascript

 
Harini nataraj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

can anyone please help me out in getting my bean value in javascript.


 
Jaik Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the bean's attribute value in any of the component, keep that component hidden and access that component in your java script.

Thanks
Jaikrat Singh
 
Harini nataraj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

trying to do the same but i am getting null value

<h:inputHidden id="html" value="#{ques.str}" />

function onCall()
{
var htmlstring = document.getElementById('form:html').value;
alert(htmlstring);


}

tried something like this, i m new to jsf, that would be great if someone helps
 
Jaik Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try once without using hidden attribute, like simple inputText. If it works, try with hidden attribute.


Thanks
Jaikrat Singh
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18226
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on what point in time you're looking to obtain the value from.

To get the backing bean property value as of page load time, it can be as simple as this:


Anything more immediate would require AJAX.
 
Harini nataraj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, i got it.
 
Aiswarya Geidth
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

"<f:verbatim><script>
function onCall() {
alert("#{ques.str}");
}
</script></f:verbatim> "

I used this code but i am not got the value, when alert opens it shows "#{ques.str}" this as it is.

How can you resolve this one. Please tell me.


Thanks
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18226
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm. Try it without the "f:verbatim" tags. I didn't think that they'd disable the EL processing, but maybe they did.
 
Aiswarya Geidth
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry! still it's not working.
 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't feel this is a good method to do it. By enabling <f:verbatim> parsing would not happen when the page gets rendered, so EL expression would be off. (Always <script> should not be parsed as a best practice).
Try save the bean value in hidden input box, then extract by java script function call. Here you have used faces input box <h:inputHidden> with id attribute, but faces will be given different id value than what you have specified.
Create input hidden box inside 'div' or 'span' and extract them by (div.id/span.id).firstChild. This should work for you.
 
Aiswarya Geidth
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please provide me any sample code.


Thanks
 
Harini nataraj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<h:inputHidden id="html" value="#{ques.conversiontostring}" />

function onCall()
{
var htmlstring = document.getElementById("form:html").value;
alert(htmlstring);


}

actually the above code worked for me. try to do this.
 
Harini nataraj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<h:commandButton value="display_alert" onclick="onCall()" />

and call that function onclick of command button or use it as per your requirement
 
Aiswarya Geidth
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that one, but i am getting null value.
 
Harini nataraj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the beginning even i was getting null value. Make sure that your Bean class should look like this.

public String getConversiontostring()
{

str = "some text whatever you need in you java script";
System.out.println("My Html :" + str);
return str;
}
for the above function you have to make the call as below, ques is my bean name.

#{ques.conversiontostring}

it must work
 
Aiswarya Geidth
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally I got the solution.

This is JSF


Bean class:



Now it's working.


Thank you.
 
kumar toomu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if your bean is in session scope you can use this...

alert('${beanname.property}');
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic