在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>
整个< 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" />
传递 *。js
通过 JspServlet
(仅当它足以仅评估 $ {}
表达式时)。例如。在 web.xml
(< servlet-name>
of JspServlet
可以在相关的servletcontainer的 web.xml
中找到,它通常是 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>
使用具有修改内容类型的老旧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在加载期间以ajaxically方式获取数据。这是一个 jQuery 目标示例。
$.getJSON('json/messages', function(messages) {
$.each(messages, function(key, value) {
$.messages[key] = value;
});
});
这篇关于在JavaScript文件中混合使用JSF EL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!