可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Looking to use Struts2 with Serlvet 3.0 Async support.
My first approach was to just handle to writing to the outputstream in the action and returning null. This however returns with a 404 "resource not available". I am attempting to adapt a Bosh servlet inside of a struts action, using ServletRequestAware, ServletResponseAware interfaces to inject the response.
I am using the struts filter dispatcher. Not entirely sure if this is doable,but would be sure happy if someone else has managed to get async to work within a struts action. Perhaps here is an AsyncResult type or someother magic to make this work.
回答1:
Make sure the struts filter allows async. Here's what that looks like in the web.xml file:
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> <async-supported>true</async-supported> </filter>
Then from within an Action obtain the HttpServletRequest
and HttpServletResponse
and use the AsyncContext
as you would within a 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; }