我想知道在DWR和Spring应用程序中获取HttpServletRequest对象的正确方法是什么 - 我尝试按如下方式进行:
private HttpServletRequest getRequest() {
ServletRequestAttributes servletAttributes =
(ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
// this is Spring application's DispatcherServlet call
if (servletAttributes != null) {
return servletAttributes.getRequest();
} else {
if (WebContextFactory.get() == null) {
// non-HttpRequest call
return null;
} else {
// dwr call
return WebContextFactory.get().getHttpServletRequest();
}
}
}
我问这个是因为当这个方法用完了任何一个http上下文时,WebContextFactory方法会记录下面的警告:
WARN org.directwebremoting.WebContextFactory:39 - Missing WebContextBuilder. Is DWR setup properly?
我可能缺少一个方法,可以判断此方法调用是否在HttpServletRequest中,所以我可以直接返回null值:
private HttpServletRequest getRequest() {
// something like this would be ideal:
if (!ServletContextFactory.isInServletContext()) {
// method was not called from ServletRequest, so there is none to be found
return null;
}
...
答案 0 :(得分:0)
您可以使用 DWR 类作为spring bean,因此您不需要 HttpServletRequest 。
例如,这是你的dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
"http://getahead.org/dwr/dwr30.dtd">
<dwr>
<allow>
<create creator="spring" javascript="Report">
<param name="beanName" value="reportController" />
</create>
</allow>
</dwr>
这是你的春豆
<bean name="reportController" class="com.repsys.ajax.ReportController">
<property name="reportService" ref="reportService"></property>
</bean>