Search code examples
javascriptjstljsp-tags

Can I somehow call JSTL function from JavaScript file?


The question is as following: when I write JavaScript inside my JSP page, using JSTL function, it renders normally, understanding everything I want from it. But to make my code clear, I want to move that JavaScript from tag in JSP to a separate file. But when I try to call same function from the file, it doesn't work, but just appends to my page as a simple text. Here is code example to make this more understandable.

...other JSP stuff

    <script>
        $.each(data, function(index, item) {
                $('#holder').append(
                    '<tr>' +
                        '<td>item.price + ' <fmt:message key="currency.default"/></td>'
                    '</tr>'
                );
            });
    </script>

This works perfect for me. The actual message from the resource bundle is pulled and set instead of the fmt:message function.

But when I move the same code to a separate file, all this doesn't transform and stays plain text.

I understand, that JSP serves on the server, and all transformations with those functions is done much earlier than actual javascript is loaded.

But maybe somebody knows a certain hack to make this work?

Thanks in advance.


Solution

  • You can use DWR for that cause. An old framework but still holds good if that is what exactly you are looking for in your question. http://directwebremoting.org/dwr/index.html

    DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible.