struts2 Async Action

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

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; } 


文章来源: struts2 Async Action
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!