0

Im a Rookie in Angular JS. I'm using the Spring MVC model and would like to get the contextPath from Tomcat (localthost:8080>>>/test<<<).

What I need is a variable that stores the value of "${pageContext.request.contextPath}" from a JSP and pass that variable to the config.

Is it possible to pass that value to the config? And if not: is there another or even better way?

Ive searched stackoverflow, but only found one answer but must confess I didn't understand it since it hadnt much code to go for: Whats the better way to identify the context path in Angular JS

App.js

    (function() {
    var app = angular.module('testApp', [ 'ngRoute']);

    app.config(function($routeProvider) {
        $routeProvider.when('/', {
            templateUrl : '/home',
        })
        ;
    });
}());

HomeController.java

 @Controller
public class HomeController {

    @RequestMapping("/home")
    public String showHomereg(){
        return "content/index.jsp";
    }

The way its shown here will direct me to "localhost:8080/home" instead of "localhost:8080/test/home"

Community
  • 1
  • 1

2 Answers2

0

I think you just need to update your $routeProvider in app.js and add /test in when function.

(function() {
var app = angular.module('testApp', [ 'ngRoute']);

app.config(function($routeProvider) {
    $routeProvider.when('/test', {
        templateUrl : '/home',
    });
});
});
Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
  • This is not the solution, since the contextPath might be a different one on another server. I need to give my angularJS app the contextPath from the JSP its coming from – user3272488 Dec 26 '14 at 22:23
0

I would suggest to go for relative paths. First of start by removing slash before home (client side):

templateUrl : '/home', to: templateUrl : 'home',

which obviously, will try to load something relative to the app it self e.g. if your SPA is say, index.html (or I would guess index.jsp?) in say webapp folder, then app will try to load template from webapp with that name. As this is a service (interesting concept here but IMHO unnecessary to do it as the app is already loaded in the browser) it will do a get relative to the app which might work (I assume that you tried that).

Again, 'home path a.k.a. /' is already loaded in the browser and is the starting html template.

embuc
  • 465
  • 5
  • 5