博客
关于我
ActionContext和ServletActionContext小结
阅读量:798 次
发布时间:2023-03-23

本文共 2424 字,大约阅读时间需要 8 分钟。

在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们还需要在Action中直接获取请求(Request)或会话(Session)的一些信息,甚至需要对JavaServlet Http的请求(HttpServletRequest)和响应(HttpServletResponse)进行操作。以下是获取request、response和session的详细说明。

1. ActionContext

在Struts2中,ActionContext是一个非常重要的概念。它可以看作是一个容器,存储了Action执行时所需的各种对象。最常用的方式是通过以下代码获取ActionContext:

ActionContext context = ActionContext.getContext();
Map params = context.getParameters();
String username = (String) params.get("username");

ActionContext通过ThreadLocal实现了线程安全,每个线程都会有自己的独立副本,避免了多线程环境下的数据冲突。

2. ServletActionContext

ServletActionContext是ActionContext的扩展版本,直接继承了它,并提供了与Servlet相关的对象访问功能。其常见用途包括:

  • 获取HttpServletRequest对象:

    HttpServletRequest request = ServletActionContext.getRequest();
  • 获取HttpSession对象:

    HttpSession session = ServletActionContext.getRequest().getSession();

3. ActionContext与ServletActionContext的联系

在实际开发中,ActionContext和ServletActionContext的功能有一定的重叠。建议优先使用ActionContext,如果它能够满足需求,则无需使用ServletActionContext。以下原则可以帮助我们做出选择:

  • 如果ActionContext能够实现所需功能,请优先使用它。
  • 避免在Action的构造函数中使用ActionContext.getContext()或ServletRequest。
  • ServletActionContext.getRequest()等方法应放置在具体的方法中,而非构造函数或类变量中。

4. Struts2中获取request、response和session

非IoC方式

方法一:使用ActionContext类
ActionContext ctx = ActionContext.getContext();
ctx.put("liuwei", "andy"); // 等同于request.setAttribute("liuwei", "andy");
Map session = ctx.getSession(); // 获取session对象
HttpServletRequest request = ctx.get(StrutsStatics.HTTP_REQUEST);
HttpServletResponse response = ctx.get(StrutsStatics.HTTP_RESPONSE);
方法二:使用ServletActionContext类
public class UserAction extends ActionSupport {
// 不推荐直接在类中声明request
private HttpServletRequest req;
public String login() {
req = ServletActionContext.getRequest();
// 业务逻辑...
req.getSession().setAttribute("user", user);
return SUCCESS;
}
}

IoC方式(使用Struts2 Aware拦截器)

如果需要使用IoC,可以实现相应的Aware接口:

public class UserAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public String execute() {
HttpSession session = request.getSession();
return SUCCESS;
}
}

通过上述方式,我们可以在不同的场景中灵活获取request、response和session对象,确保代码的可维护性和可扩展性。

转载地址:http://hoqfk.baihongyu.com/

你可能感兴趣的文章
Objective-C实现堆排序(附完整源码)
查看>>
Objective-C实现声音录制播放程序(附完整源码)
查看>>
Objective-C实现备忘录模式(附完整源码)
查看>>
Objective-C实现复制粘贴文本功能(附完整源码)
查看>>
Objective-C实现复数类+-x%(附完整源码)
查看>>
Objective-C实现外观模式(附完整源码)
查看>>
Objective-C实现多尺度MSR算法(附完整源码)
查看>>
Objective-C实现多种方法求解定积分(附完整源码)
查看>>
Objective-C实现多组输入(附完整源码)
查看>>
Objective-C实现多项式函数在某个点的评估算法(附完整源码)
查看>>
Objective-C实现多项式哈希算法(附完整源码)
查看>>
Objective-C实现大位数乘法(附完整源码)
查看>>
Objective-C实现大根堆(附完整源码)
查看>>
Objective-C实现奇偶检验码(附完整源码)
查看>>
Objective-C实现奇偶转置排序算法(附完整源码)
查看>>
Objective-C实现奇异值分解SVD(附完整源码)
查看>>
Objective-C实现子集总和算法(附完整源码)
查看>>
Objective-C实现字符串autocomplete using trie(使用 trie 自动完成)算法(附完整源码)
查看>>
Objective-C实现字符串boyer moore search博耶摩尔搜索算法(附完整源码)
查看>>
Objective-C实现字符串IP地址转DWORD地址(附完整源码)
查看>>