代码如下:
package com.web;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.bean.Girls;
import com.biz.GirlsBiz;
public class Controller {
private GirlsBiz girlsBiz;
//设值注入
public void setGirlsBiz(GirlsBiz girlsBiz) {
this.girlsBiz = girlsBiz;
}
public String sayhello(){
List<Girls> list=girlsBiz.findAllGirls();
for (Girls girls : list) {
System.out.println("name: "+girls.getName());
}
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("glist", list);
return "hello";
}
}
用junit测试时可以遍历出集合list的,可同时报空指针异常在HttpServletRequest request=ServletActionContext.getRequest();这句,求大神指点啦,找了一天都没找出答案