欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2的action中获得request response session

程序员文章站 2022-07-03 23:23:24
...
1,servlet 不相关的 非IoC 取得Request等对象的方式
这种方法先取得ActionContext,然后获取Map类型的request等对象

package struts2.login;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
private ActionContext context;
private Map request;
private Map session;
private Map application;

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.context = ActionContext.getContext();
this.request = (Map)this.context.get("request");
this.session = this.context.getSession();
this.application = this.context.getApplication();
this.request.put("attribute", "request value servlet 不相关的 非IoC ");
this.session.put("attribute", "session value servlet 不相关的 非IoC ");
this.application.put("attribute", "application value servlet 不相关的非IoC ");
return SUCCESS;
}
}


2,servlet 不相关的 IoC 取得Request等对象的方式
这种方法,是实现特定的接口,由container来设定request等对象。请注意如下代码示例中红色文字的部分。

package struts2.login;

import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class Login2Action extends ActionSupport
implements RequestAware , SessionAware, ApplicationAware{
private Map request;
private Map session;
private Map application;

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.request.put("attribute", "request value servlet 不相关的 IoC ");
this.session.put("attribute", "session value servlet 不相关的 IoC ");
this.application.put("attribute", "application value servlet 不相关的 IoC ");
return SUCCESS;
}

@Override
public void setRequest(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.request = arg0;
}

@Override
public void setSession(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.session = arg0;
}

@Override
public void setApplication(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.application = arg0;
}
}


3,servlet 相关的 非IoC 取得Request等对象的方式
这种方法可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。
取出方法,请参看代码中的蓝字部分。

package struts2.login;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Login3Action extends ActionSupport{
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.request = ServletActionContext.getRequest();
this.session = this.request.getSession();
this.application = ServletActionContext.getServletContext();
this.request.setAttribute("attribute", "request value servlet 相关的非IoC ");
this.session.setAttribute("attribute", "session value servlet 相关的非IoC ");
this.application.setAttribute("attribute", "application value servlet 相关的 非IoC ");
return SUCCESS;
}

}


4,servlet 相关的 IoC 取得Request等对象的方式

这种方法也可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。
但是取出方法是通过接口进行实现,也就是由struts2的container来设定,请参看代码中的红字部分。

package struts2.login;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class Login4Action extends ActionSupport
implements ServletRequestAware , ServletContextAware{
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.request.setAttribute("attribute", "request value servlet 相关的 IoC ");
this.session = this.request.getSession();
this.session.setAttribute("attribute", "session value servlet 相关的 IoC ");
this.application.setAttribute("attribute", "application value servlet 相关的 IoC ");
return SUCCESS;
}

@Override
public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
this.request = arg0;
}

@Override
public void setServletContext(ServletContext arg0) {
// TODO Auto-generated method stub
this.application = arg0;
}
}


1、非IoC方式

HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();


2、IoC方式(这种方式在tapestry中也是这样实现的) 要使用IoC方式,我们首先要告诉IoC容器(Container)想取得某个对象的意愿,通过实现相应的接口做到这点。

public class IoCServlet extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware { private String message; private Map att; private HttpServletRequest request; private HttpServletResponse response;

public String getMessage() { return message; }

publicvoid setSession(Map att) { this.att = att; }

publicvoid setServletRequest(HttpServletRequest request) { this.request = request; }

publicvoid setServletResponse(HttpServletResponse response) { this.response = response; }

@Override public String execute() { att.put("msg", "Hello World from Session!");

HttpSession session = request.getSession();

StringBuffer sb =new StringBuffer("Message from request: "); sb.append(request.getParameter("msg")); sb.append(" Response Buffer Size: "); sb.append(response.getBufferSize()); sb.append(" Session ID: "); sb.append(session.getId());

message = sb.toString(); return SUCCESS; } }