1

So here is my story.

I am developing a spring web application. The reloading of static content (js, css, jsp) was broken and found a solution in the following thread:

Getting resources in VFrabric Server to deploy without causing container to reload

In order for reloading to work I couldn't have the root URL '/'. So I change that to '/project' and my reloading works. Great!

But then comes the next problem. I have a lot ajax requests to '/typeahead/searchUniversities/%QUERY' for example, this would need to be changed to 'project/typeahead/searchUniversities/%QUERY'. And when I upload it to the production server I would need to change it back to '/typeahead/searchUniversities/%QUERY'

Since '/project' is just for development

So the I read about profiles but I'm not sure if this is the way to go. I might overdoing it?

I was also thinking of having something like '${baseurl}/typeahead/searchUniversities/%QUERY' and then just change in one place before production. But not sure how to do this.

If someone could guide me with an example of how to do this it would be great.

Thank you!

Community
  • 1
  • 1
nilsi
  • 10,351
  • 10
  • 67
  • 79
  • Are you using maven as a build tool? – Sinisha Mihajlovski Jul 08 '14 at 14:18
  • With maven you have the filtering option. You can add placeholder in your js code ${holder} and if you setup maven filtering, maven during the build will replace the placeholder with a maven property value. Then by running different profiles for local and production you can dynamically change these values. The filtering concept is described here: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html – Sinisha Mihajlovski Jul 09 '14 at 09:26
  • Thats interesting! thank you for the input – nilsi Jul 10 '14 at 13:42

2 Answers2

2

Edit : add another more direct way to get the context path in a JSP

The url should never be static in the webapp : the context path is determined at deployment time and not at compile time. If you are using spring tag library, you can make use of <spring:url> tag, if not of the JSTL <c:url> tag. Both will automatically add the context path for you.

BTW : in a JSP <%= application.getContextPath() %> gives the context path without any additional taglib.

But please only use relative paths where it has sense and never for "top level" URLs.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • I think this is the solution to my problem. I am not just sure how to reach the this tags in my separate JS file. To modify my ajax calls urls. – nilsi Jul 08 '14 at 13:32
  • @nilsi I'm not very good at JavaScript. But IMHO you need to have at least one JS that will not be static under /js but dynamically generated by a JSP - simply, do not forget to set the type to `text/javascript` ... This one will contain you actual context path, that will either simply define a variable that will be used by other(s) JS file(s), or directly be used by all your ajax calls. – Serge Ballesta Jul 08 '14 at 14:04
  • You could just set a variable in an inline script in the HTML file and reference it from your AJAX scripts. – chrylis -cautiouslyoptimistic- Jul 08 '14 at 14:07
  • Okey, yes this is the approach I will go for. Thanks a lot for the help. – nilsi Jul 08 '14 at 14:28
0

Your application shouldn't depend on the context path it is deployed under. One solution to this particular issue would be to use relative paths in your ajax requests,

e.g.

'typeahead/searchUniversities/%QUERY' rather than '/typeahead/searchUniversities/%QUERY'

gprathour
  • 14,813
  • 5
  • 66
  • 90
marthursson
  • 3,242
  • 1
  • 18
  • 28
  • Beware when using relative queries, you must be sure of the depth level of current url. IMHO it is safe only in trivial applications where all URLs are at level 1. – Serge Ballesta Jul 08 '14 at 12:02
  • This is the reason I don't use this method. Since I having the same links on different depths. – nilsi Jul 08 '14 at 13:25