在 JavaScript 文件中混合 JSF EL [英] Mixing JSF EL in a JavaScript file
问题描述
有没有办法让 表达式语言 (EL) 表达式包含 JavaScript 文件由 JSF 评估?
Is there a way to have Expression Language (EL) expressions included JavaScript files be evaluated by JSF?
我希望 Seam 可以解决这个问题,但到目前为止还没有运气.我想要的只是能够在我的 JavaScript 函数中使用本地化的消息,这些消息是跨页面共享的.
I was hoping that Seam might have a way around this, but no luck so far. All I want is to be able to use localized messages in my JavaScript functions which are shared across pages.
推荐答案
五种方式:
在父 JSF 页面中将其声明为全局变量.
Declare it as global variable in the parent JSF page.
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
var messages = [];
<ui:repeat value="#{bean.messages}" var="message">
messages['#{message.key}'] = '#{message.value}';
</ui:repeat>
</script>
或者,如果它已经是 JSON 格式.
Or, if it's in JSON format already.
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">var messages = #{bean.messagesAsJson};</script>
将整个 放在一个 XHTML 文件中,并使用
ui:include
来包含它.
Put the whole <script>
in a XHTML file and use ui:include
to include it.
<script type="text/javascript" src="script.js"></script>
<ui:include src="script-variables.xhtml" />
通过 JspServlet
传递 *.js
(仅当它足以仅计算 ${}
表达式时).例如.在web.xml
中(JspServlet
的
可以在web.xml
有问题的 servletcontainer,通常是 jsp
).
Pass *.js
through the JspServlet
(only if it's enough to evaluate only the ${}
expressions). E.g. in web.xml
(the <servlet-name>
of JspServlet
can be found in web.xml
of the servletcontainer in question, it's usually jsp
).
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
使用good old"JSP 和修改后的内容类型.将 script.js
重命名为 script.jsp
并将以下行添加到 JSP 的顶部(仅当它足以仅评估 ${}
表达式):
Make use of "good old" JSP with a modified content type. Rename script.js
to script.jsp
and add the following line to top of JSP (only if it's enough to evaluate only the ${}
expressions):
<%@page contentType="text/javascript" %>
让JS在加载时ajax方式获取数据.这是一个 jQuery 目标示例.
$.getJSON('json/messages', function(messages) {
$.each(messages, function(key, value) {
$.messages[key] = value;
});
});
这篇关于在 JavaScript 文件中混合 JSF EL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!