【注意】:我们需要知道后两种方法得到的是Map<String,Object>,而后两种方式得到的才是真正的request等对象。而Map就是把request对象中的属性取出做成了键值对而已。
方法一:使用ServletActionContext获取——servlet 相关的 非IoC 取得Request等对象的方式——这种方法可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息
public class StudentAction extends ActionSupport implements ModelDriven<Student> {
private Student student;
private HttpServletRequest request;
private HttpServletResponse response;
private ServletContext application;
private HttpSession session;
public StudentAction(){
request=ServletActionContext.getRequest();
response=ServletActionContext.getResponse();
session=request.getSession();
application=session.getServletContext();
}
public String execute(){
request.setAttribute("student", student);
session.setAttribute("student", student);
application.setAttribute("student", student);
return Action.SUCCESS;
}
@Override
public Student getModel() {
student=new Student();
return student;
}
}
方法二:实现ServletRequestAware,ServletResponseAware接口——servlet 相关的 IoC 取得Request等对象的方式——这种方法也可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。
但是取出方法是通过接口进行实现,也就是由struts2的container来设定
public class StudentAction2 extends ActionSupport implements ModelDriven<Student>,ServletRequestAware,ServletResponseAware {
private Student student;
private HttpServletRequest request;
private HttpServletResponse response;
private ServletContext application;
private HttpSession session;
public String execute(){
request.setAttribute("student", student);
session.setAttribute("student", student);
application.setAttribute("student", student);
return Action.SUCCESS;
}
@Override
public Student getModel() {
student=new Student();
return student;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.response=arg0;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
this.request=arg0;
session=this.request.getSession();
application=session.getServletContext();
}
}
方法三:Map键值对方式,通过ActionContext获得——servlet 不相关的 非IoC 取得Request等对象的方式——这种方法先取得ActionContext,然后获取Map类型的request等对象
public class StudentAction3 extends ActionSupport implements ModelDriven<Student> {
private Student student;
private Map<String,Object> request;
private Map<String,Object> session;
private Map<String,Object> application;
public StudentAction3(){
request=(Map<String, Object>) ActionContext.getContext().get("request");
session=ActionContext.getContext().getSession();
application=ActionContext.getContext().getApplication();
}
public String execute(){
request.put("student", student);
session.put("student", student);
application.put("student", student);
return Action.SUCCESS;
}
@Override
public Student getModel() {
student=new Student();
return student;
}
}
方法四:继承RequestAware,SessionAware,ApplicationAware接口——servlet 不相关的 IoC 取得Request等对象的方式——这种方法,是实现特定的接口,由container来设定request等对象
public class StudentAction4 extends ActionSupport implements ModelDriven<Student>,RequestAware,SessionAware,ApplicationAware {
private Student student;
private Map<String,Object> request;
private Map<String,Object> session;
private Map<String,Object> application;
public String execute(){
request.put("student", student);
session.put("student", student);
application.put("student", student);
return Action.SUCCESS;
}
@Override
public Student getModel() {
student=new Student();
return student;
}
@Override
public void setApplication(Map<String, Object> arg0) {
application=arg0;
}
@Override
public void setSession(Map<String, Object> arg0) {
session=arg0;
}
@Override
public void setRequest(Map<String, Object> arg0) {
request=arg0;
}
}