1.请求:http://localhost/NewSea/admin/task/start.action?id=12.代码
package com.sea.action;import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;public class Collect2Action extends ActionSupport implements Preparable { private static final Map<Integer, Thread> threads = new HashMap<Integer, Thread>();
private int id; @Override
public String execute() throws Exception {
return super.execute();
}
public String start() throws Exception {
Thread appointedThread = threads.get(id);
System.out.println("size()"+threads.size());
if(appointedThread==null){
System.out.println("appointedThread==null");
}
return SUCCESS;
}
@Override
public void prepare() throws Exception {
// 这个prepare只是对于execute方法起作用
System.out.println("prepare");
}

public void prepareStart() throws Exception {
Thread t = new Thread();
threads.put(id, t);
System.out.println("prepareStart");
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}}3.打印的结果
prepareStart
prepare
size():1
appointedThread==null4.问题
为什么在start方法中取得的appointedThread 为null?这一点我想不明白!5.帖尾
请指教,小生有礼了,临帖涕零,不知所言!---

解决方案 »

  1.   

    debug跟一下. 看看执行流程就知道了阿.!
      

  2.   


            <action name="start" class="com.sea.action.Collect2Action" method="start">
                <result name="success" type="redirectAction">list</result>
            </action>
    感觉应该跟struts.xml关系不大吧
    我再百度,看看能找出点线索不
      

  3.   

    原因应该是这样:
    Preparable 是先与parameter拦截器的,所以在prepareStart中的id=null而在start中id已经获取了值。如果你直接Thread appointedThread = threads.get(null);应该能得到值