且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将支持bean值传递给JavaScript?

更新时间:2021-12-04 02:36:11

到目前为止,您需要了解JSF仅仅是生成HTML代码,JS是HTML的一部分。所以你需要做的就是相应地编写JSF / EL代码,它生成有效的HTML / JS代码。实际上,使用EL在生成的JavaScript代码中内联bean属性就像在你的尝试中一样是正确的方法。

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.

假设这确实是真实的代码,但是你犯了一些语法错误。 onClick 属性无效。它必须全部小写。否则JSF根本不会呈现该属性。

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没有强类型的概念。函数参数中的 Boolean 类型无效。去掉它。否则你将面临一个不错的JS语法错误(在浏览器的JS控制台中)。

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');
   }
}

请注意,我希望您了解此操作之前命令链接自己的动作已被调用(您现在应该已经理解; JSF生成HTML;在浏览器中右键单击页面并查看源自己看看)。但是你没有在你的具体问题中讨论这个问题(更多的是,你根本没有用问题中的给定代码描述具体问题),所以我不能就此给出详细的答案。

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.