当 JQuery 包含引用 RequestContextUtils 的 jsp 代码时,它无法加载 jsp 页面。
<%@ page import="org.springframework.context.ApplicationContext,org.springframework.web.servlet.support.RequestContextUtils,org.sunshine.location.LocationService,org.sunshine.domain.Location,java.util.List"%>
<%
out.println("request:" + request.getContextPath());
ApplicationContext locationContext = RequestContextUtils.getWebApplicationContext(request);
LocationService locationService = (LocationService) locationContext.getBean("locationService");
List<Location> locations = locationService.getLocations();
out.println(locations);
%>
<table id="location-table" width="95%" bgcolor="f8f8ff" border="0"
cellspacing="0" cellpadding="5">
<c:forEach items="<%=locations%>" var="location">
<tr>
<td><c:out value="${location.id}" /></td>
<td><a href="index.htm?store=${location.id}"><c:out
value="${location.name}" /></a></td>
</tr>
</c:forEach>
</table>
如果我正常访问,上面的 jsp 页面可以正常工作。但是当我使用如下 JQuery 的加载/获取方法时 - 它失败了。
$(document).ready(function(){
$("button").click(function(){
$.get("jsp/locations.jsp",function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
或者
$(document).ready(function(){
alert("locad");
$("#locations-column").load("jsp/locations.jsp");
});
我评论了所有不必要的代码以找出根本原因。我发现 ApplicationContext locationContext = RequestContextUtils.getWebApplicationContext(request); 语句使 jquery 不加载页面。
例外:
java.lang.IllegalStateException:未找到 WebApplicationContext:不在 DispatcherServlet 中
想知道为什么?当它作为普通的 jsp 文件运行时如何找到它的 web 上下文,以及为什么不通过 JQuery 加载它。
有人可以帮我知道为什么会这样吗?我怎么解决这个问题?
在此先感谢-维杰·丹尼尔