简体   繁体   中英

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.

Running java methods or jstl functions(also jstl functions are java methods) from JavaScript is impossible. Because java methods run on server-side but javascript on client-side.

If you want to run java methods in client-side anyway you must create java applet for this. You can run java methods with JavaScript inside your applet. For detailed information see this Java Applet Tutorial

I hope this will help you

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM