如何在 RequireJS 配置中将 request.getContextPath() 定义为 baseUrl?

How to define request.getContextPath() as baseUrl in RequireJS config?

新手问题:我今天刚开始使用 RequireJS,我试图在我的配置文件中将 request.getContextPath() 设置为我的 baseUrl。

我正在使用 JSP。在每个 JSP 页面上,我在头部导入 scripts.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<%String context = request.getContextPath();%> // i.e. /www.myUrl.com/myContextPath

<c:set var="context" value="<%=context %>"/>

<script data-main="${context}/js/config" src="${context}/js/require.js"></script>
<script>
require(['config'], function(){
    // all dependencies have loaded
})

里面 config.js 我有这个:

requirejs.config({
baseUrl: 'libs',
paths:{
    jquery:'jquery-2.2.4/jquery-2.2.4.min',
    bootstrap:'bootstrap-3.3.7/js/bootstrap.min',
    dataTables: 'datatables.min'
    }
});

问题是它试图加载库但没有我的上下文路径所以它会尝试这样的事情:

www.myUrl.com/NOTmyContextPath/libs/jqueryLib

我需要在 baseUrl 中获取 ${context}。获得该价值的最佳方式是什么?

感谢您提供任何有用的提示。

更新:这似乎是一个 Spring 映射问题,我现在迷路了,因为我是一名前端开发人员。我收到的服务器错误如下:

警告:2017-03-17 16:08:04:636 org.springframework.web.servlet.PageNotFound 未找到具有 URI [/myContextPath/app/dir/js/myFolder/libs/jquery-2.2.4/jquery-2.2.[=44 的 HTTP 请求的映射=]] 在 DispatcherServlet 中,名称为 'myContextPath'

我认为我不需要在 web.xml 中添加新的 servlet 映射,我宁愿不使用 window.location 或类似的东西来获取应用程序上下文。纠正此问题的最佳方法是什么?

在另一个 Stack overflow 上使用 JS 找到了答案 post:

function getContextPath() {
   return window.location.pathname.substring(0, window.location.pathname.indexOf("/",2));
}

requirejs.config({
    baseUrl: getContextPath() + '/js/ops/libs',
    paths:{
        jquery:'jQuery-2.2.4/jquery-2.2.4.min',
        bootstrap:'Bootstrap-3.3.7/js/bootstrap.min',
        dataTables: 'datatables.min',
    }
});