我正在尝试struts2
async
行动。
有没有办法将动作的变量设置为异步上下文?
在下面的代码中cust1List
设置在async
块内,但在块外,它仍为null
。
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>
行动代码:
public class GenerateAsynctResult extends ActionSupport {
private ArrayList cust1List;
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 ...
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
setCust1List(list);
//this will print out "C"
System.out.println(cust1List.get(2));
}
finally {
asyncContext.complete();
}
}
});
//this will throw null pointer exception cause cust1List is null
System.out.println("b4 success" + cust1List.get(2));
return Action.SUCCESS;
}
public ArrayList getCust1List() {
return cust1List;
}
public void setCust1List(ArrayList cust1List) {
this.cust1List = cust1List;
}
}
关于如何克服这个问题的任何想法?
感谢。