您的位置:首页 > 移动开发

在 Struts2 中访问 Request、Session 和 Application

2011-02-04 09:43 363 查看
我们经常需要在 Action 中传入一些参数到 JSP 页面,可以用 来获取参数,但是这些参数都仅限于特定的数据,如果我们想要像 JSP 中使用 request、Session 和 application ,我们怎么办呢?
一、
我们可以通过 ActionContext 对象得到 Request 等的 Map 类型对象进行赋值和参数的传递,代码参考如下:
1:  public String execute(){


2:


3:      ((Map)ActionContext.getContext().get("request")).put("r1", "r1");//Request 的 Map 对象


4:      ActionContext.getContext().getSession().put("s1", "s1");// Session 的 Map 对象


5:      ActionContext.getContext().getApplication().put("a1", "a1");//Application 的 Map 对象


6:      return SUCCESS;


7:  }


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

通过上面这种方式定义 Action 就可以传递参数了,对此,我们在前台 JSP 页面获取参数的方法有两种:

一种是通过 标签来访问参数:

1:  value="#request.r1"/>


2:  value="#session.s1"/>


3:  value="#application.a1"/>


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

这种访问方式和在 value 的参数前面加了一个 # ,因为这些都是 Stack Context 里面的 key。

另一种访问参数的方法是我们熟悉的:

1:  <%=request.getAttribute("r1") %>


2:  <%=session.getAttribute("s1") %>


3:  <%=application.getAttribute("a1") %>


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

或许有些不能理解,Map 型的 Request、Session、Application 可以这样访问?!是的,Struts2 做到了,应该是在内部做了一些转化的,将 Map 转型为 HttpServletRequest、HttpSession 和 ServletContext 。

这种方式是依赖 Struts2 环境的,因为使用了 ActionContext 类。

 

二、

这种方法是通过依赖注入( DI, Dependency Injection ),或者叫控制反转 ( IoC, Inverse of Control ) 实现的。这种方式最常用。

原理是定义 Action 实现 RequestAware、SessionAware、ApplicationAware 接口,实现里面定义的 setXxxxx( Map xxx ) 方法,之后等待 Struts2 Context 给我们定义的 Action 注入 request、 session 、 application 等等。(控制反转的理解可以认为本来 request、session、application 这些变量是由 Action 自己控制的,实现这些 Aware 接口后控制权就移交给 Struts2 Context 了)

Action 代码如下,其他代码如上所示:

1:  package com.cdp.struts2;


2:   


3:  import java.util.Map;


4:   


5:  import org.apache.struts2.interceptor.ApplicationAware;


6:  import org.apache.struts2.interceptor.RequestAware;


7:  import org.apache.struts2.interceptor.SessionAware;


8:   


9:  import com.opensymphony.xwork2.ActionSupport;


10:   


11:  public class Action2 extends ActionSupport implements RequestAware,


12:          SessionAware, ApplicationAware {


13:


14:      private Map request;


15:      private Map session;


16:      private Map application;


17:   


18:      public String execute() {


19:   


20:          request.put("r1", "r1");


21:          session.put("s1", "s1");


22:          application.put("a1", "a1");


23:          return SUCCESS;


24:      }


25:   


26:      public void setRequest(Map request) {


27:          this.request = request;


28:   


29:      }


30:   


31:      public void setSession(Map session) {


32:          this.session = session;


33:   


34:      }


35:   


36:      public void setApplication(Map application) {


37:          this.application = application;


38:   


39:      }


40:   


41:  }


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

需要注意的是,这种方式是最常用的。

三、

第三种方式是直接创建真实的而非 Map 类型的 request 、 session 、 application 对象,方法如下:

1:  package com.cdp.struts2;


2:   


3:  import javax.faces.application.Application;


4:  import javax.servlet.ServletContext;


5:  import javax.servlet.http.HttpServletRequest;


6:  import javax.servlet.http.HttpSession;


7:   


8:  import org.apache.struts2.ServletActionContext;


9:   


10:  import com.opensymphony.xwork2.ActionSupport;


11:   


12:  public class Action3 extends ActionSupport{


13:   


14:      private HttpServletRequest request;


15:      private HttpSession session;


16:      private ServletContext application;


17:


18:      public String execute(){


19:          request=ServletActionContext.getRequest();


20:          session=request.getSession();


21:          application=session.getServletContext();


22:          request.setAttribute("r1", "r1");


23:          session.setAttribute("s1", "s1");


24:          application.setAttribute("a1", "a1");


25:          return SUCCESS;


26:      }


27:


28:  }


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

四、

最后一种也是依赖注入实现的,Action 实现的是 ServletRequestAware 接口,让后通过 request 得到 session,通过 session 得到 application:

1:  package com.cdp.struts2;


2:   


3:  import javax.faces.application.Application;


4:  import javax.servlet.ServletContext;


5:  import javax.servlet.http.HttpServletRequest;


6:  import javax.servlet.http.HttpSession;


7:   


8:  import org.apache.struts2.ServletActionContext;


9:  import org.apache.struts2.interceptor.ServletRequestAware;


10:   


11:  import com.opensymphony.xwork2.ActionSupport;


12:   


13:  public class Action4 extends ActionSupport implements ServletRequestAware{


14:   


15:      private HttpServletRequest request;


16:      private HttpSession session;


17:private ServletContext application;


18:


19:      public String execute(){


20:          request.setAttribute("r1", "r1");


21:          session.setAttribute("s1", "s1");


22:          application.setAttribute("a1", "a1");


23:          return SUCCESS;


24:      }


25:   


26:      public void setServletRequest(HttpServletRequest request) {


27:    this.request=request;


28:          this.session=request.getSession();


29:          this.application=session.getServletContext();


30:      }


31:


32:  }


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: