确保 struts 过滤器允许异步。这是 web.xml 文件中的内容:
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<async-supported>true</async-supported>
</filter>
然后从 Action 中获取HttpServletRequest
andHttpServletResponse
并AsyncContext
像在 servlet 中一样使用:
public String execute() {
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse res = ServletActionContext.getResponse();
final AsyncContext asyncContext = req.startAsync(req, res);
asyncContext.start(new Runnable() {
@Override
public void run() {
try {
// doing some work asynchronously ...
}
finally {
asyncContext.complete();
}
}
});
return Action.SUCCESS;
}