如何在异步块中设置struts2动作的变量?

时间:2018-04-05 10:01:33

标签: java asynchronous servlets struts2 servlet-3.0

我正在尝试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;
        }
    }

关于如何克服这个问题的任何想法?

感谢。

0 个答案:

没有答案