Using JSP code in JavaScript

后端 未结 7 1414
夕颜
夕颜 2020-12-09 05:14

I want to use JSTL\'s fmt tag in JavaScript to localize my alert messages. My JavaScript file is a standalone file and when I include fmt tag in js, the file browser gives J

相关标签:
7条回答
  • 2020-12-09 06:08

    You should strive to keep your javascript code in a separate file from the jsp-code. This is to get browser caching, easier maintenance, reuse across pages, and to allow compression.

    I suggest that you create a global object for text in the jsp, to be used by your javascript files. Like this:

    <script>
        var text = text || {}; // play well with other jsps on the page
        text.this_page_name = {
            required_field_error: "<fmt:message key="required.field.error"/>",
            system_error: "<fmt:message key="system.error"/>"
        };
    </script>
    

    Later you use it in your javascript:

    alert(text.this_page_name.required_field_error);
    
    0 讨论(0)
提交回复
热议问题