在 JavaScript 文件中混合 JSF EL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2547814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mixing JSF EL in a JavaScript file
提问by user283680
Is there a way to have Expression Language(EL) expressions included JavaScript files be evaluated by JSF?
有没有办法让JSF评估包含JavaScript文件的表达式语言(EL)表达式?
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.
我希望 Seam 可以解决这个问题,但到目前为止还没有运气。我想要的只是能够在跨页面共享的 JavaScript 函数中使用本地化消息。
回答by BalusC
Five ways:
五种方式:
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>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>Put the whole
<script>in a XHTML file and useui:includeto include it.<script type="text/javascript" src="script.js"></script> <ui:include src="script-variables.xhtml" />Pass
*.jsthrough theJspServlet(only if it's enough to evaluate only the${}expressions). E.g. inweb.xml(the<servlet-name>ofJspServletcan be found inweb.xmlof the servletcontainer in question, it's usuallyjsp).<servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping>Make use of "good old" JSP with a modified content type. Rename
script.jstoscript.jspand add the following line to top of JSP (only if it's enough to evaluate only the${}expressions):<%@page contentType="text/javascript" %>Let JS obtain the data ajaxically during load. Here's a jQuerytargeted example.
$.getJSON('json/messages', function(messages) { $.each(messages, function(key, value) { $.messages[key] = value; }); });
在父 JSF 页面中将其声明为全局变量。
<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 格式。
<script type="text/javascript" src="script.js"></script> <script type="text/javascript">var messages = #{bean.messagesAsJson};</script>将整个
<script>文件放在一个 XHTML 文件中并使用ui:include它来包含它。<script type="text/javascript" src="script.js"></script> <ui:include src="script-variables.xhtml" />通
*.js过JspServlet(只有当它足以只评价${}表达式)。例如,在web.xml(对<servlet-name>中JspServlet可以发现web.xml有问题的servletcontainer的,它通常是jsp)。<servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping>使用具有修改内容类型的“好旧”JSP。重命名
script.js为script.jsp并将以下行添加到 JSP 的顶部(仅当它足以仅计算${}表达式时):<%@page contentType="text/javascript" %>让JS在加载时ajax方式获取数据。这是一个针对jQuery 的示例。
$.getJSON('json/messages', function(messages) { $.each(messages, function(key, value) { $.messages[key] = value; }); });
回答by timr
Since I don't like techniques that won't let the browser cache the localized Strings, I used the following technique to localize JavaScript alerts, etc. It seems a good fit if the Strings that you need in your JavaScript code are different from the ones needed by the Web server:
由于我不喜欢不允许浏览器缓存本地化字符串的技术,因此我使用了以下技术来本地化 JavaScript 警报等。如果您在 JavaScript 代码中需要的字符串与Web服务器需要的:
<h:head>
<h:outputScript library="javascript" name="#{fw.JsFwStrings}" />
...
I then assign the resource string JsFwStrings to the filename of the JavaScript file defining the localized strings for the given language.
然后我将资源字符串 JsFwStrings 分配给定义给定语言的本地化字符串的 JavaScript 文件的文件名。
For example, the fw_en.properties file contains the entry JsFwStrings=JsFwStrings_en.js
例如,fw_en.properties 文件包含条目 JsFwStrings=JsFwStrings_en.js
And the JsFwStrings_en.js file contains
并且 JsFwStrings_en.js 文件包含
var TosFramework = TosFramework || {};
TosFramework.Strings = {
UnSavedChangesWarning : 'You have unsaved changes.',
CancelConfirmQuestion : 'Are you sure you want to cancel?'
}

