struts2的action中获得request response session几种方法

在struts2中有两种方式可以得到这些对象
1、非IoC方式
要获得上述对象,关键Struts 2中com.opensymphony.xwork2.ActionContext类。我们可以通过它的静态方法getContext()获取当前Action的上下文对象。有了这个对象我们想获得其他几个对象就好办了

ActionContext ctx = ActionContext.getContext();
Map session = ctx.getSession();
这里的session是个map对象在Struts2中底层的session都被封装成了Map类型我们可以直接操作这个map 进行对session的写入和读取操作而不用去直接操作HttpSession对象
另外,org.apache.struts2.ServletActionContext作为辅助类(Helper Class),可以帮助您快捷地获得这几个对象。
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();
如果你只是想访问session的属性(Attribute),你也可以通过ActionContext.getContext().getSession()获取或添加session范围(Scoped)的对象。

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

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>


	<package name="default" extends="struts-default">
		<action name="login" class="struts2.login.LoginAction">
			<result name="success">result.jsp</result>
		</action>

		<action name="login2" class="struts2.login.Login2Action">
			<result name="success">result.jsp</result>
		</action>

		<action name="login3" class="struts2.login.Login3Action">
			<result name="success">result.jsp</result>
		</action>

		<action name="login4" class="struts2.login.Login4Action">
			<result name="success">result.jsp</result>
		</action>
	</package>


</struts>    

getrequest.jsp

<%@ page contentType="text/html; charset=gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<a href="login.action">servlet 不相关的 非IoC 取得Request等对象的方式</a>
<br>
<a href="login2.action">servlet 不相关的 IoC 取得Request等对象的方式</a>
<br>
<a href="login3.action">servlet 相关的 非IoC 取得Request等对象的方式</a>
<br>
<a href="login4.action">servlet 相关的 IoC 取得Request等对象的方式</a>
<br>
</form>
</html>

result.jsp

<%@ page contentType="text/html; charset=gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<html>
request 属性是 ${requestScope.attribute}
<br> session 属性是 ${sessionScope.attribute}
<br> application 属性是 ${applicationScope.attribute}
<br>
</html>
/*servlet 不相关的 IoC 取得Request等对象的方式  
这种方法,是实现特定的接口,由container来设定request等对象。 
  
Action文件: 
 */
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;
	}
}
/*servlet 相关的 非IoC 取得Request等对象的方式  
这种方法可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。  
取出方法,  
  
Action文件:
 */
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;
	}

}
/*servlet 相关的 IoC 取得Request等对象的方式  
  
这种方法也可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。  
但是取出方法是通过接口进行实现,也就是由struts2的container来设定。  
  
Action文件:
*/  
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;  
    }  
}   
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;  
    }  
}  

发表评论

电子邮件地址不会被公开。 必填项已用*标注