Javascript 如何在RequireJS配置中将request.getContextPath()定义为baseUrl?
新手问题:我今天刚刚启动了RequireJS,我正在尝试将request.getContextPath()设置为配置文件中的baseUrl 我正在使用JSP。在每个JSP页面上,我在头部导入scripts.JSP:Javascript 如何在RequireJS配置中将request.getContextPath()定义为baseUrl?,javascript,java,spring,jsp,requirejs,Javascript,Java,Spring,Jsp,Requirejs,新手问题:我今天刚刚启动了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://ja
<%@ 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
})
问题是它试图加载库,但没有我的上下文路径,因此它将尝试以下操作:
www.myUrl.com/NOTmyContextPath/libs/jqueryLib
我需要在baseUrl中获取${context}。在那里获得价值的最佳方式是什么
谢谢你的建议
更新:这似乎是一个Spring映射问题,我现在迷路了,因为我是一名前端开发人员。我收到的服务器错误如下:
警告:2017-03-17 16:08:04:636 org.springframework.web.servlet.pageNot在名为“myContextPath”的DispatcherServlet中未找到URI为[/myContextPath/app/dir/js/myFolder/libs/jquery-2.2.4.min]的HTTP请求的映射
我认为我不需要在web.xml中添加新的servlet映射,我也不希望使用window.location或类似的东西来获取应用程序上下文。纠正这个问题的最好方法是什么 在另一个堆栈溢出帖子上使用JS找到了答案:
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',
}
});
您应该在servlet.xml中使用,并在放置静态文件的位置使用“app”映射设置其他静态文件。
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',
}
});