Results 1 to 4 of 4

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

  1. #1
    Join Date
    Jul 2012
    Posts
    3

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

    Hi,

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

    In my JS file i want to use
    Code:
    ${some_url_var}
    set in
    Code:
    <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}


    Thanks,
    Aditya

  2. #2
    Join Date
    Jun 2008
    Location
    Philadelphia, PA, USA
    Posts
    212

    Default

    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.

    Ken
    Ken Rimple
    Chariot Solutions
    email: krimple@chariotsolutions.com
    work: www.chariotsolutions.com/education
    personal: www.rimple.com

    Author: Spring Roo in Action (Manning)
    MEAP Site: manning.com/rimple

  3. #3
    Join Date
    Jul 2012
    Posts
    3

    Default 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
    Code:
    <spring:url value="/resources/js/some.js" var="some_url" />
    
    <script type="text/javascript" src="${some_url}"></script>

    JS file - some.js
    Code:
    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...

    Thanks,
    Aditya

  4. #4
    Join Date
    Jun 2008
    Location
    Philadelphia, PA, USA
    Posts
    212

    Default

    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:

    <script>

    do_something("${my_jsp_var}");

    </script>

    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.

    Ken
    Ken Rimple
    Chariot Solutions
    email: krimple@chariotsolutions.com
    work: www.chariotsolutions.com/education
    personal: www.rimple.com

    Author: Spring Roo in Action (Manning)
    MEAP Site: manning.com/rimple

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •