0

How can I pass a Javascript variable (String) directly to a bean method? When I'm trying the following, it doesn't work.

var dd = document.getElementById("j_idt2:ddcust");  
var customer = dd.options[dd.selectedIndex].value;

var credit = document.getElementById("j_idt2:credit");

credit.value = #{creditController.getCreditScore(customer)};

However, if I hardtype my string into the method call (see below), it works.

var dd = document.getElementById("j_idt2:ddcust");  
var customer = dd.options[dd.selectedIndex].value;

var credit = document.getElementById("j_idt2:credit");

credit.value = #{creditController.getCreditScore("Bau GmbH")};

But I don't want to hardcode. Where is my error here? Help would be appreciated!

curr11
  • 11
  • 2
  • Looks like you have a lot of client-side javascript (ui/framework knowledge). There something like this also does not work and you need some 'ajax' related functionality. That is what is used in the answer below. – Kukeltje Nov 29 '18 at 14:56

1 Answers1

0

You can't do it this way, because javascript is evaluated in your web browser while expression Language expressions (like #{creditController.getCreditScore("Bau GmbH")};) are being evaluated on server side even before the javascript is sent to your browser where it is evaluated.

However you can send back javascript variable content to the server and have it assigned to managed bean properties in at leat following ways:

As a conclusion you are not going to have a clean client side only javaScript execution passing javascript variables to EL expressions and assigning EL expression return values to javascript varialbes without any postback action to the server.

Selaron
  • 6,105
  • 4
  • 31
  • 39
  • EL **value expressions** are evaluated when rendered ;-). Everything in 'value' attributes (and style, styleClass etc) is also everything 'directly' in a page so plain as in the example). There is btw a sort of duplicate that describes this. But a good answer anyhow. Cheers! – Kukeltje Nov 29 '18 at 14:58
  • I just tried this. How can I now set the outputText (in my case the credit score) with the return of my bean method? I passed through the javascript variables as described, however I'm not sure how to put the result of the method back in my form – curr11 Nov 29 '18 at 15:08
  • If you used `p:remoteCommand`, you need to specify component IDs to be refreshed in the `update` attribute. If you are using `f:ajax`, you need to specify component ids in the `render-attribute. These components will then be populate with new state from server upon ajax response. – Selaron Nov 29 '18 at 15:14
  • @Kukeltje not sure if I got you right - is it the bold word 'before' which is not precisely correct? Because rendering and sending happen concurrently? Feel free to link to the sort of duplicate, I had not found a real duplication as this question is kind of special ^^ – Selaron Nov 29 '18 at 15:30
  • 1
    No, the before is right, it is just that not *all** EL expressions are evaluated before the rendering. Method expressions are evaluated after (e.g. in a submit from a button or a 'remotecommand' and the OP might have gotten the impression that by adding the (...) as a parameter it automagically becomes a method expression (in early days you could not have a value expression with a parameter (not even a method one now I think of it). And no there is not a real duplicate. More an additional link that helps explain the value vs method and the moment everything takes place. I will look for it. – Kukeltje Nov 29 '18 at 15:46