Search code examples
javajqueryjspsessionstruts2

How can I get the session values from an external.js by using Struts2 jQuery


I need to recover a value from session in my external JS. Before, I did it in a script embedded in the JSP with this lines and it worked properly:

$(window).load(function(){
    var selectedServer = "<s:property value='%{#session.selectedServer}'/>";
    var selectedMarket = "<s:property value='%{#session.selectedMarket}'/>";
});

but now, I want to do it from an external JS and an alert(selectedServer); after these lines, show me the literal: <s:property value='%{#session.selectedServer}'/>, but not the value.

Is the syntax different in an external.js?


Solution

  • You can use javascript global variables in the external script, or use function parameters to pass the values that you should obtain in the JSP to the external function.

    <script>
        var selectedServer = "<s:property value='%{#session.selectedServer}'/>";
        var selectedMarket = "<s:property value='%{#session.selectedMarket}'/>";
    </script>
    
    function(selectedServer, selectedMarket){
    }