简体   繁体   English

如何将 JSF 托管 bean 属性传递给 JavaScript 函数?

[英]How do I pass JSF managed bean properties to a JavaScript function?

I would like to know how I can pass JSF managed bean properties to a JavaScript function.我想知道如何将 JSF 托管 bean 属性传递给 JavaScript 函数。

Something like this:像这样的东西:

<script>
  function actualizaMenu(key){
    #{linkedMenu.setKey(key)}
  }
</script>
<ul>
  <ui:repeat value="#{moduleList.modulos}" var="entity">
    <li>
      <a onclick="actualizaMenu(#{entity.key})">#{entity.nombre}</a>
    </li>
  </ui:repeat>
</ul>

This is not exactly "passing" of JSF variables.这并不完全是 JSF 变量的“传递”。 This is just printing JSF variables as if they are JavaScript variables/values.这只是打印 JSF 变量,就好像它们是 JavaScript 变量/值一样。 You know, JSF and JS do not run in sync at all.您知道,JSF 和 JS 根本不同步运行。 JSF runs in webserver and produces HTML/CSS/JS code which in turn runs in webbrowser once arrived over there. JSF 在 webserver 中运行并生成 HTML/CSS/JS 代码,一旦到达那里,这些代码又会在 webbrowser 中运行。

Your concrete problem is most likely caused because you wrote JSF code in such way that it generates invalid JS syntax.您的具体问题很可能是因为您以生成无效 JS 语法的方式编写了 JSF 代码。 An easy way to verify that is by just checking the JSF-generated HTML output which you can find by rightclick, View Source in browser, and by checking if you don't see any syntax error reports in the JS console in browser which you can find by pressing F12 in Chrome/IE9+/Firefox23+.验证这一点的一种简单方法是检查 JSF 生成的 HTML 输出,您可以通过右键单击找到该输出,在浏览器中查看源代码,并检查是否在浏览器的 JS 控制台中没有看到任何语法错误报告,您可以在 Chrome/IE9+/Firefox23+ 中按 F12 查找。

Imagine that #{entity.key} here想象一下这里的#{entity.key}

<a onclick="actualizaMenu(#{entity.key})">#{entity.nombre}</a>

prints a Java string variable like "foo" , then the generated HTML would look like打印一个像"foo"这样的 Java 字符串变量,然后生成的 HTML 看起来像

<a onclick="actualizaMenu(foo)">some name</a>

But hey, look, that represents a JavaScript variable named foo , not a JS string value!但是,嘿,看,它代表一个名为foo的 JavaScript变量,而不是一个 JS 字符串值! So if you actually want to ultimately end up as所以如果你真的想最终成为

<a onclick="actualizaMenu('foo')">some name</a>

then you should instruct JSF to generate exactly that HTML:那么您应该指示 JSF 生成该 HTML:

<a onclick="actualizaMenu('#{entity.key}')">#{entity.nombre}</a>

Beware of special characters in the JSF variable though.不过要注意 JSF 变量中的特殊字符。 You can use OmniFaces of:escapeJS() function for that.您可以使用OmniFaces of:escapeJS()函数


Unrelated to the concrete problem, the concrete implementation of actualizaMenu() makes no sense.具体问题无关actualizaMenu()的具体实现是没有意义的。 You seem to be attempting to set a bean property.您似乎正在尝试设置 bean 属性。 You should not use JS for that, but a <h:commandLink> instead.您不应该为此使用 JS,而应使用<h:commandLink>

<h:commandLink value="#{entity.nombre}" action="#{linkedMenu.setKey(entity.key)}" />

Nest if necessary a <f:ajax> to make it asynchronous.如有必要,嵌套<f:ajax>以使其异步。

I would recommend using event binding with jQuery and the data attribute on elements to get the same result (assuming you use jQuery):我建议使用带有 jQ​​uery 的事件绑定和元素上的 data 属性来获得相同的结果(假设您使用 jQuery):

<script>
  function actualizaMenu(key){
    /* Logic here ... */
  }

  $(document).ready(function(){
    $('.menuItem').click(function(){
      var key = $(this).data('key');
      actualizaMenu(key);
    );
  });
</script>

... ...

<ul>
  <ui:repeat value="#{moduleList.modulos}" var="entity">
    <li>
      <a data-key="#{entity.key}" class="menuItem">#{entity.nombre}</a>
    </li>
  </ui:repeat>
</ul>

And, as pointed out elsewhere, unless #{linkedMenu.setKey(key)} actually returns a piece of javascript (which seams unlikely and would probably be really bad even if it did) you need to fix the function as well.而且,正如其他地方所指出的,除非#{linkedMenu.setKey(key)}实际上返回一段 javascript(这不太可能,即使它确实可能会非常糟糕),您也需要修复该功能。

I know this question is old, but to those who are still looking there's an alternative.我知道这个问题很老,但对于那些仍在寻找替代方案的人。

If you are using primefaces just try this out.如果您使用的是primefaces,请尝试一下。 Request Context请求上下文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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