How to pass backing bean value to JavaScript?

后端 未结 2 842
攒了一身酷
攒了一身酷 2020-12-19 19:12

I would like to read the backing bean value in JSF and then pass over to JavaScript, may I know how this can be done?

Backing bean sample code:

@Mana         


        
相关标签:
2条回答
  • 2020-12-19 19:29

    You could try the approach from here

    and save your bean field in:

    <h:inputHidden id="hidden2" value="#{enquiry.noQuery}" />
    

    then, call your method using the value of this field.

    0 讨论(0)
  • 2020-12-19 19:40

    To the point, you need to understand that JSF merely produces HTML code and that JS is part of HTML. So all you need to do is to write JSF/EL code accordingly that it produces valid HTML/JS code. Indeed, using EL to inline bean properties in the generated JavaScript code like as in your attempt is one of the right ways.

    Assuming that this is indeed the real code, you however made some syntax mistakes. The onClick attribute is invalid. It must be in all lowercase. Otherwise JSF simply won't render the attribute at all.

    <h:commandLink value="link" onclick="showNoQueryPrompt(#{enquiry.noQuery})" />
    

    JavaScript has no notion of strong typing. The Boolean type in function argument is invalid. Remove it. Otherwise you will face a nice JS syntax error (in browser's JS console).

    function showNoQueryPrompt(showPrompt) {
       if (showPrompt) {
          alert('No query');
       }
    }
    

    Note that I expect that you understand that this runs before commandlink's own action is been invoked (you should now have understood; JSF generates HTML; rightclick page in browser and View Source to see it yourself). But you didn't cover that in your concrete question (even more, you didn't describe the concrete problem with the given code in the question at all), so I can't give an detailed answer on that.

    0 讨论(0)
提交回复
热议问题