Mixing JSF EL in a JavaScript file
Asked Answered
E

2

42

Is there a way to have Expression Language (EL) expressions included JavaScript files be evaluated by JSF?

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.

Edgerton answered 30/3, 2010 at 18:52 Comment(1)
Please Post an example of a JavaScript function.Wilford
H
71

Five ways:

  1. 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>
    
  2. 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" />
    
  3. 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>
    
  4. 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" %>
    
  5. Let JS obtain the data ajaxically during load. Here's a jQuery targeted example.

    $.getJSON('json/messages', function(messages) {
        $.each(messages, function(key, value) {
            $.messages[key] = value;
        });
    });
    
Hyperacidity answered 30/3, 2010 at 19:4 Comment(2)
I might add that the first one is the easiest. Let Facelets handle it for you.Monandry
Thanks. Didn't know I could use <ui:repeat> inside a <script> block. Btw: I think the <ui:repeat> attribute should be value instead of items.Drank
O
3

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:

<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.

For example, the fw_en.properties file contains the entry JsFwStrings=JsFwStrings_en.js

And the JsFwStrings_en.js file contains

var TosFramework = TosFramework || {};
TosFramework.Strings = {
    UnSavedChangesWarning : 'You have unsaved changes.',
    CancelConfirmQuestion : 'Are you sure you want to cancel?'
}
Oblige answered 24/3, 2014 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.