Announcement Announcement Module
No announcement yet.
How to use spring:url tags variable in a seprate Javascript file..? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use spring:url tags variable in a seprate Javascript file..?


    I have added a Javascript file using spring:url in a JSPX file.

    In my JS file i want to use
    set in
    <spring:url value="/crud/doSomething" var="some_url_var" />
    When I tried setting that URL var in JSPX.. And tried accessing same var in JS file. It did not
    executed. Value it assigned was ${some_url_var}


  • #2
    Are you mounting the spring: tag library at the top of the file?

    You are using tiles, so every tile that is using a tag library must declare it. So if your javascript is embedded in a JSPX file as another tile from the main one, you'll need to mount the spring: library in the top level tag of that page.

    Also, if you're just importing a file that is a Javascript file and expecting Roo to interpolate the variable for you, the JSP compiler is not going to compile it - it is downloaded at the browser level. You can have a scriptlet execute within the JSPX file after importing the script file, and the scriptlet (that perhaps calls a function in the javascript file) can refer to the ${some_url_var} parameter.



    • #3
      Re:How to use spring:url tags variable in a seprate Javascript file..?

      Hi Ken,

      Thanks for your reply...

      In my project, I'm not adding JS file as/in a tile. I'm doing it as follows

      JSPX file
      <spring:url value="/resources/js/some.js" var="some_url" />
      <script type="text/javascript" src="${some_url}"></script>

      JS file - some.js
      var test = "${some_url}";
      I have also added spring tag support in this jspx file.

      Other than scriplet. Is there any way we can do the same..?

      Or Other wise I would have to set a hidden variable in jspx and then use that in JS file...



      • #4
        You just can't do that. JSP files are interpreted on the server, the results are outputted and sent back to the client. JavaScript is executed on the client. It is already too late to send a live variable.

        Just declare a JavaScript function in your script, and in your JSPX file do something like:




        Then, the jspx file will interpret the value of the variable and send it to the script in the js file. Write the function to do the setup of your JavaScript work.