火炏炎焱燚

火炏炎焱燚

统计

留言簿(1)

使用ASP.NET AJAX 1.0框架PreviewGlitz进行网页动画编程

阅读排行榜

评论排行榜

Struts2 和 Servlet 耦合

=============================

首选 ActionConext 其次 ServletActionContext 最后是实现接口

http://scorpio-jh.blog.sohu.com/83673437.html

-- 1 --
 ### ActionContext ###  --- 不能获得response对象

static ActionContext getContext()   获得当前线程的向下文
                           
                                    相当于
Object get(Object key)               ---- getAttribute()

void put( Object key, Object value ) ---- setAttribute()

Map getParameter()    相当于Servlet中的getParameterMap()

Map getApplication()   对应于ServletContext


如:

action
ActionContext.getContext().put("zhangsan","hello");

jsp
张三:${ requestScope.zhangsan }

 

-- 2 --
通过接口使用 --- 典型的依赖注入

### ServletContextAware ### 接口

void setServletContext( ServletContext )

### ServletRequestAware ### 接口

void setServletRequest( HttpServletRequest )

### ServletResponseAware ### 接口

void setServletResponse( HttpServletResponse )


例:
public class LoginAction extends ActionSupport
 implements ServletRequestAware,
  ServletResponseAware {

 private String username;
 private String password;
 //setter...getter...

 private HttpServletRequest request;
 private HttpServletResponse response;

 public String execute() throws Exception {

  //使用request对象 
  request.setAttribute("zhangshan","hello");

  //使用response对象
  Cookie cookie = new Cookie("username",
      this.getUsername());
  cookie.setMaxAge(1000);
  response.addCookie(cookie);

  return "success";
 }

 //这个方法由Strust2框架来自动调用
 //调用后,便可以使用容器的request对象了
 public void setServletRequest(HttpServletRequest request) {

  this.request = request;
 }

 public void setServletResponse(HttpServletResponse responset) {

  this.response = response;
 }
}


---------------------
jsp页面中显示cookie

 cookie: ${ cookie.username.value }

--------------------


-- 3 --

使用 org.apache.struts2.ServletActionContext
---继承于 com.opensymphony.xwork2.ActionContext

内部全部为static的方法

static ActionContext getActionContext(HttpServletRequest req)

static PageContext getPageContext() 

static HttpServletRequest getRequest()
 
static HttpServletResponse getResponse() 

static ServletContext getServletContext() 

static ValueStack getValueStack(HttpServletRequest req) 

-----

static void setRequest(HttpServletRequest request)

static void setResponse(HttpServletResponse response)

static void setServletContext(ServletContext servletContext)

 

例:
public class LoginAction extends ActionSupport {

 private String username;
 private String password;
 //setter...getter...

 

 public String execute() throws Exception {

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

  return "success";
 }
}



Web应用程序开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息, 甚至需要直接对JavaServlet Http的请求(HttpServletRequest)、响应(HttpServletResponse)操作。
我们需要在Action中取得request请求参数“username”的值:
ActionContext context = ActionContext.getContext();
Map params = context.getParameters();
String username = (String) params.get(“username”);
ActionContextcom.opensymphony.xwork.ActionContext)是Action执行时的上下文,上下文可以看作是一个容器(其实我们这里的容器就是一个Map而已),它存放放的是Action在执行时需要用到的对象
一般情况,我们的ActionContext都是通过:ActionContext context = (ActionContext) actionContext.get();来获取的。我们再来看看这里的actionContext对象的创建:static ThreadLocal actionContext = new ActionContextThreadLocal();ActionContextThreadLocal是实现ThreadLocal的一个内部类。ThreadLocal可以命名为“线程局部变量”,它为每一个使用该变量的线程都提供一个变量值的副本,使每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突。这样,我们ActionContext里的属性只会在对应的当前请求线程中可见,从而保证它是线程安全的。
下面我们看看怎么通过ActionContext取得我们的HttpSession
Map session = ActionContext.getContext().getSession()
ServletActionContextcom.opensymphony.webwork. ServletActionContext),这个类直接继承了我们上面介绍的ActionContext,它提供了直接与JavaServlet相关对象访问的功能,它可以取得的对象有:
1、   javax.servlet.http.HttpServletRequestHTTPservlet请求对象
2、   javax.servlet.http.HttpServletResponse;HTTPservlet相应对象
3、   javax.servlet.ServletContextServlet 上下文信息
4、   javax.servlet.ServletConfigServlet配置对象
5、   javax.servlet.jsp.PageContextHttp页面上下文
下面我们看看几个简单的例子,让我们了解如何从ServletActionContext里取得JavaServlet的相关对象:
1、   取得HttpServletRequest对象:
HttpServletRequest request = ServletActionContext. getRequest();
2、   取得HttpSession对象:
HttpSession session = ServletActionContext. getRequest().getSession();
ServletActionContextActionContext有着一些重复的功能,在我们的Action中,该如何去抉择呢?我们遵循的原则是:如果ActionContext能够实现我们的功能,那最好就不要使用ServletActionContext,让我们的Action尽量不要直接去访问JavaServlet的相关对象。在使用ActionContext时有一点要注意:不要在Action的构造函数里使用ActionContext.getContext(),因为这个时候ActionContext里的一些值也许没有设置,这时通过ActionContext取得的值也许是null

posted on 2009-07-20 10:55 火炏炎焱燚 阅读(1104) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。