当包含引用RequestContextUtils的jsp代码时,JQuery无法加载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的load / get方法时,如下所示-它失败了。
$(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");
});我注释了所有不必要的代码,以找出根本原因。我发现jquery = RequestContextUtils.getWebApplicationContext(request);语句使得ApplicationContext locationContext不会加载页面。
例外:
java.lang.IllegalStateException:找不到WebApplicationContext :不在DispatcherServlet中
想知道为什么吗?当它作为普通jsp文件运行时,如何找到它web上下文,以及为什么不能在通过JQuery加载它时找到它。
有人能帮我知道为什么会这样吗?我该如何解决这个问题?
预先感谢-Vijay Daniel
发布于 2013-11-28 08:44:56
解决了问题。问题出在作为方法RequestContextUtils.getWebApplicationContext的参数发送的请求对象上。它是:
ApplicationContext productContext = RequestContextUtils.getWebApplicationContext(request);我发现来自jquery的请求对象不包含获取bean所需的属性DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE。但我找到了构建应用程序上下文的替代方法:
ApplicationContext productContext = RequestContextUtils
.getWebApplicationContext(request,servletContext); 在这里,servlet上下文与请求一起传递。Servlet上下文可以通过如下方式获取:
ServletContext servletContext = (ServletContext)
request.getSession().getServletContext();因此,将jsp代码修改为使用的servlet上下文对我来说很有效。
https://stackoverflow.com/questions/19924699
复制相似问题