博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2 与Servlet API解耦,耦合的访问方式
阅读量:7049 次
发布时间:2019-06-28

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

一.与Servlet API解耦的访问方式

1.为了避免与Servlet API耦合在一起,方便Action类做单元测试,

  Struts2对HttpServletRequest、HttpSession和ServletContext进行了封装,
  构造了三个Map对象来替代这三种对象,在Action中,
  直接使用HttpServletRequest、HttpSession、ServletContext对应的Map对象来保存和读取数据。
  要获得这三个Map对象,可以使用com.opensymphony.xwork2.ActionContext类

ActionContext - 请求上下文 。就是struts2封装的request.包含了request,session,application上面这三个都是Map.

public class LoginAction implements Action{    private String username;    private String password;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }        public String execute() throws Exception {        if (username.equals("1")&&password.equals("1")) {            //解耦合 记录session            Map
session = ActionContext.getContext().getSession(); session.put("uname", username); return SUCCESS; }else { return LOGIN; } }}

登陆成功后,记录用户名到Session中,登陆失败,跳会登陆页面

-->        
s.jsp
login.jsp

2:向Action中注入ServletAPI对象对应的Map对象

public class LoginAction implements Action,SessionAware {    private String username;    private String password;    private Map
map; public String execute() throws Exception { if (username.equals("1")&&password.equals("1")) { //注入map集合 map.put("uname", username); return SUCCESS; }else { return LOGIN; } } public void setSession(Map
map) { this.map=map; }}

二.与Servlet API耦合的访问方式

直接访问 Servlet API将使Action类与Servlet API耦合在一起,Servlet API对象均由Servlet容器来构造,与这些对象绑定在一起,测试过程中就必须有Servlet容器,这样不便于Action类的测试,但有时候,确实需要访问这些对象,Struts2同样提供了直接访问ServletAPI对象的方式。

要直接获取Servlet API对象可以使用org.apache.struts2.ServletActionContext类,该类是ActionContext类的子类。

//通过ActionContext的子类ServletActionContext实现

public class LoginAction implements Action {    private String username;    private String password;    public String execute() throws Exception {        if (username.equals("1")&&password.equals("1")) {           HttpSession session = ServletActionContext.getRequest().getSession();            session.setAttribute("uname", username);            return SUCCESS;        }else {            return LOGIN;        }    }

//向Action实例注入Servlet API对象

public class LoginAction implements Action,ServletRequestAware{    private HttpServletRequest Request;        private String username;    private String password;    public String execute() throws Exception {        if (username.equals("1")&&password.equals("1")) {         //记录session          HttpSession session=request.getSession();          session.setAttribute("uname",username);            return SUCCESS;        }else {            return LOGIN;        }    }        public void setServletRequest(HttpServletRequest arg0) {        this.request=request;            }

 

转载于:https://www.cnblogs.com/s1294/p/5834360.html

你可能感兴趣的文章
使用 CXF 做 webservice 简单例子
查看>>
Spring MVC之@RequestMapping 详解
查看>>
使用STS和Gradle创建Restful服务-Hello World
查看>>
网络服务器开发总结
查看>>
关于redis的主从、哨兵、集群
查看>>
Extjs Form用法详解
查看>>
ExecutorService线程池
查看>>
OD使用及快捷键
查看>>
将Mule ESB Http项目转换为Tomcat项目(3) ESB项目运行
查看>>
IE9开始支持SVG格式(VML终结)
查看>>
php set_time_limit
查看>>
一种Android的多平台的安装包打包方法探究
查看>>
观察者模式
查看>>
【转】PHP中的Hash算法
查看>>
SqlLite的工具类SQLiteOpenHelper
查看>>
chgrp chown chmod
查看>>
nodejs中安装express
查看>>
2014软件表
查看>>
Struts2教程3:struts.xml常用配置解析
查看>>
(转帖)Implementing custom JavaFx Bindings
查看>>