Using JSP code in JavaScript

后端 未结 7 1422
夕颜
夕颜 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:03

    is it possible to treat .js file as .jsp file using web.xml configuration?

    Yes:

    
        scriptjsp
        /script.jsp 
    
    
        scriptjsp
        /script.js
    
    

    But, there is no actual advantage in doing this, because JavaScript files do not have to have URLs ending in ‘.js’. What determines whether a file is a JavaScript file is what MIME media type it is served as. You can set this from JSP using:

    <%@ page contentType="text/javascript" %>
    

    at the top. Then you can link directly to:

    
    

    (Aside: in current browsers, scripts linked to with the

    (changing 'en' to match whichever language you want), and in that file define a lookup like:

    var msg= {
        messageName: 'Message in English',
        ...
    };
    

    Then look up msg.messageName for each localisable string in the script.

提交回复
热议问题