Struts2 ActionContext

chinalwb 2013-05-05 08:25:23
各位大侠,

我有一个小问题,关于ActionContext对象,他是什么时候被创建的呢,在同一个请求中会出现多个ActionContext吗?

更详细的情况是这样的。
在一个action中,我include了另外一个action,在include之前呢我打印ActionContext 和 include之后打印的ActionContext居然不是同一个对象!

代码如下:
struts.xml:

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.devMode" value="false" />


<package name="default" extends="struts-default" namespace="/">
<action name="test" class="com.bv.test.TestAction1" >
<result name="success">/index.jsp</result>
</action>
<action name="x" class="com.bv.test.TestActionX">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>


TestAction1.java

package com.bv.test;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;


public class TestAction1 {

public String execute() {

ActionContext ac = ActionContext.getContext();
System.out.println("Before including: the action context is : " + ac);
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
System.out.println("Before including: the response is : " + response);

ResponseImpl myResponse = new ResponseImpl(response);
RequestDispatcher rd = request.getRequestDispatcher("/x.action");
try {
rd.include(request, myResponse);
String s = myResponse.getOutput();
System.out.println("get from response: " + s);
}
catch (Exception e) {
e.printStackTrace();
}

ac = ActionContext.getContext();
System.out.println("After including : the action context is : " + ac);
response = ServletActionContext.getResponse();
System.out.println("After including : the response is : " + response);


ServletActionContext.setResponse(response);
return "success";
}
}


ResponseImpl.java:

package com.bv.test;

import java.util.Locale;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.Cookie;
import javax.servlet.jsp.JspWriter;

/**
*
*
*/
public class ResponseImpl extends HttpServletResponseWrapper {

//=========================================================
// Private fields.
//=========================================================

private ServletOutputStream outputStream = null;

private ByteArrayOutputStream byteArrayOutputStream = null;

private StringWriter stringWriter = null;

private PrintWriter printWriter = null;

private HttpServletResponse _response = null;

private String contentType= "text/html";

private String encoding = "UTF-8";

/**
*
*/
class ServletOutputStream extends javax.servlet.ServletOutputStream {

private OutputStream outputStream = null;

/**
*
*/
ServletOutputStream(ByteArrayOutputStream outputStream) {
super();
this.outputStream = outputStream;
}

/**
*
*/
public void write(int b) throws IOException {
this.outputStream.write(b);
}
}

//=========================================================
// Public constructors and methods.
//=========================================================

/**
*
*/
public ResponseImpl(HttpServletResponse response) {
super(response);
this._response = response;
}

/**
*
*/
public String getOutput() {
if (this.stringWriter != null) {
return this.stringWriter.toString();
}

if (this.byteArrayOutputStream != null) {
try {
return this.byteArrayOutputStream.toString(this.encoding);
}
catch (UnsupportedEncodingException e) {
}
return this.byteArrayOutputStream.toString();
}

return null;
}

//=========================================================
// Implements HttpServletResponse interface.
//=========================================================

public void addCookie(Cookie cookie) {
}

public void addDateHeader(String name, long date) {
}

public void addHeader(String name, String value) {
}

public void addIntHeader(String name, int value) {
}

public boolean containsHeader(String name) {
return false;
}

public String encodeRedirectURL(String url) {
if (null != this._response) {
url = this._response.encodeRedirectURL(url);
}
return url;
}

public String encodeURL(String url) {
if (null != this._response) {
url = this._response.encodeURL(url);
}
return url;
}

public void sendError(int sc) {
}

public void sendError(int sc, String msg) {
}

public void sendRedirect(String location) {
}

public void setDateHeader(String name, long date) {
}

public void setHeader(String name, String value) {
}

public void setIntHeader(String name, int value) {
}

public void setStatus(int sc) {
}

public void resetBuffer() {
}

//=========================================================
// Implements deprecated HttpServletResponse methods.
//=========================================================

public void setStatus(int sc, String sm) {
}

//=========================================================
// Implements deprecated HttpServletResponse methods.
//=========================================================

public String encodeRedirectUrl(String url) {
return encodeRedirectURL(url);
}

public String encodeUrl(String url) {
return encodeURL(url);
}

//=========================================================
// Implements ServletResponse interface.
//=========================================================

public void flushBuffer() {
}

public int getBufferSize() {
return 0;
}

public String getCharacterEncoding() {
return this.encoding;
}

public String getContentType() {
return this.contentType;
}

public Locale getLocale() {
return null;
}

public javax.servlet.ServletOutputStream getOutputStream() {
if (this.outputStream == null) {
this.byteArrayOutputStream = new ByteArrayOutputStream();
this.outputStream =
new ServletOutputStream(this.byteArrayOutputStream);
}
return this.outputStream;
}

public PrintWriter getWriter() {
if (this.printWriter == null) {
this.stringWriter = new StringWriter();
this.printWriter = new PrintWriter(this.stringWriter);
}
return this.printWriter;
}

public boolean isCommitted() {
return false;
}

public void reset() {
}

public void setBufferSize(int size) {
}

public void setCharacterEncoding(String charset) {
}

public void setContentLength(int len) {
}

public void setContentType(String type) {
int needle = type.indexOf(";");
if (-1 == needle) {
this.contentType = type;
}
else {
this.contentType = type.substring(0, needle);
String pattern = "charset=";
int index = type.indexOf(pattern, needle);
if (-1 != index) {
this.encoding = type.substring(index + pattern.length());
}
}
}

public void setLocale(Locale locale) {
}
}


TestActionX:

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class TestActionX {

public String execute() {
ActionContext ac = ActionContext.getContext();
System.out.println("In TestAction X : the action context is : " + ac);

HttpServletResponse response = ServletActionContext.getResponse();
System.out.println("In TestAction X : the response is : " + response);
return "success";
}
}


访问/myapp/test后,控制台打印如下:
Before including: the action context is : com.opensymphony.xwork2.ActionContext@f0b51d
Before including: the response is : org.apache.catalina.connector.ResponseFacade@1320a41
In TestAction X : the action context is : com.opensymphony.xwork2.ActionContext@7a3e72
In TestAction X : the response is : com.bv.test.ResponseImpl@199ae9c
get from response: <h1>Test!</h1>
After including : the action context is : com.opensymphony.xwork2.ActionContext@1cda7e7
After including : the response is : com.bv.test.ResponseImpl@199ae9c


还望各位不吝赐教!在include前后,ActionContext 为什么会不一样呢?怎么样才能一样呢?

谢谢各位!
...全文
117 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinalwb 2013-05-06
  • 打赏
  • 举报
回复
ActionContext struts2ActionContext = ActionContext.getContext(); // include here! ActionContext.setContext(struts2ActionContext); 问题解决。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧