在struts1中,获得到系统的request或者session对象非常方便,都是按照形参传递的,但是在struts2中,request和session都被隐藏了
struts2提供两种方式访问session和request,其中比较常用的是利用SPRING里面所说的IOC即控制反转
1.IOC方式:
action类实现ServletRequestAware接口,并新建一个HttpServletRequest request
public class UserLoginAction extends ActionSupport implements ServletRequestAware{
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
然后可以生成的request得到对象,如request.getRemoteAddr()
action类实现SessionAware接口,并创建一个MAP对象session
public class UserLoginAction extends ActionSupport implements ServletRequestAware,SessionAware{
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
public void setSession(Map session) {
this.session=session;
}
2.非IOC方式
.
HttpServletRequest request = ServletActionContext.getRequest ();
HttpServletRequest request=
HTTP_REQUEST
);