Get link to project url in static resources file in Spring MVC

I have a bunch of static javascript files that process AJAX functions to request various parts of my application, and I am deploying several versions of my application, so I might have something like the following:

https://www.myurl.com/projectName/ - production release https://www.myurl.com/projectName_alpha/ - current alpha release https://www.myurl.com/projectName_beta/ - current beta release https://www.myurl.com/projectName_unstable/ - current development build 

so if I have a controller "foo" that I want to make an AJAX call to, I cannot hardcode /projectName/foo as a URL (as this would always indicate a product release). I am currently embedding a script in each of my views, which gets the base URL of the project as a global variable:

 <script type="text/javascript"> // declare a global variable to hold the project base URL var baseUrl = '<spring:url value="/" />'; </script> 

and then specify that in javascript files:

 var url = baseUrl + 'foo/'; 

This is a functional but ugly solution, and I would prefer not to pollute the global namespace if I can avoid it. Does anyone have any suggestions on how I could implement a better workaround?

To clarify: the solution must be workable for any type of request, be it AJAX-based or not-call window.open , setting the src attribute of the <img> and making an AJAX call the controller should work with the solution. It should also be applied no matter what base URL or how many layers it contains, for example:

 https://www.myurl.com/ https://www.myurl.com/projectName/ https://www.myurl.com/projectName_alpha/1.2.3/ 

must be detected correctly.

+4
source share
3 answers

I assume that converting all your js files to jsps is not what you are looking for.

The obvious solution would be to use a modular system ( require.js , for example) and wrap this value in a module,

Something like jsp with:

 define({ baseUrl: '<c:url value="/" />' }); 

which may then be required for other modules.

To do this, you may need a fairly large reorganization of the code.

A simpler option is to create the following jsp included in each page:

 <script type="text/javascript"> $(document).ready(function () { $(document).ajaxSend(function (ev, jqXHR, ajaxOptions) { var context = '${pageContext.request.contextPath}'; ajaxOptions.url = context + '/' + ajaxOptions.url; }); }); </script> 

It adds a context path to the URL of each ajax request created using jquery.

This should work as long as you use the URLs relative to the context (i.e.: starting with / , without the context name already) in your ajax requests.

+2
source

Can you use ( JSTL main URL tag )? Usually we use @RequestMapping(value="/foo") in combination with var url = '<c:url value="/foo">' and are not context sensitive. Although at the JSP level, a parameter is required to change the context root if you need to reinstall the script.

+1
source

You can use a global location object and regular expression

If you are in alpha release, pe https://www.myurl.com/projectName_alpha/resource.html

You will get information from the window.location object

 window.location.protocol = "https" window.location.host = "www.myurl.com" window.location.pathname = "/projectName_alpha/resource.html" 

If you choose, by dividing, or by regular expression, the context root of the path name, you can use this value to create the different URLs that you need.

If you enrich the location object with the baseUrl property, you can easily access it on the page

 window.location.baseUrl = window.location.pathname.split("/")[1]; 

And you can create your URLs this way:

 var url = window.location.baseUrl + '/foo/'; 
0
source

Source: https://habr.com/ru/post/1500402/


All Articles