Struct2入门二

来源:互联网 发布:mac上的安卓模拟器 编辑:程序博客网 时间:2024/05/17 03:16

Action访问Servlet
1、 在Action 中解耦合方式 间接访问 Servlet API ——— 使用 ActionContext 对象
在struts2 中 Action API 已经与 Servlet API 解耦合 (没有依赖关系 )
* Servlet API 常见操作 : 表单提交请求参数获取,向request、session、application三个范围存取数据

actionContext = ActionContext.getContext();
1) actionContext.getParameters(); 获得所有请求参数Map集合
2) actionContext.put(“company”, “传智播客”); / actionContext.get(“company”) 对request范围存取数据
3) actionContext.getSession(); 获得session数据Map,对Session范围存取数据
4) actionContext.getApplication(); 获得ServletContext数据Map,对应用访问存取数据

2、 使用接口注入的方式,操作Servlet API (耦合)
ServletContextAware : 注入ServletContext对象
ServletRequestAware :注入 request对象
ServletResponseAware : 注入response对象

  • 程序要使用哪个Servlet的对象,实现对应接口

3、 在Action中直接通过 ServletActionContext 获得Servlet API
ServletActionContext.getRequest() : 获得request对象 (session)
ServletActionContext.getResponse() : 获得response 对象
ServletActionContext.getServletContext() : 获得ServletContext对象
* 静态方法没有线程问题,ThreadLocal
Result结果类型
Action处理请求后, 返回字符串(逻辑视图名), 需要在struts.xml 提供 元素定义结果页面
1、 局部结果页面 和 全局结果页面


/demo6/result.jsp



/demo6/result.jsp

2、 结果页面跳转类型
* 在struts-default.xml 定义了 一些结果页面类型
* 使用默认type 是 dispatcher 转发 (request.getRequestDispatcher.forward)

1) dispatcher :Action 转发给 JSP
2) chain :Action调用另一个Action (同一次请求)
hello hello是一个Action的name
3) redirect : Action重定向到 JSP
4) redirectAction :Action重定向到另一个Action
hello
总结
1、 struts2 环境搭建 (导入jar包、web.xml、 struts.xml )
2、 struts2 运行流程
3、 配置文件加载顺序
4、 元素配置
5、 Action书写三种方式
6、 指定method方法调用、通配符、动态方法调用
7、 Action访问Servlet API
8.关于result标签的 type属性取值.
作业
登陆练习完成
CREATE TABLE user (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(20) NOT NULL,
password varchar(20) NOT NULL,
PRIMARY KEY (id)
)

INSERT INTO user VALUES (‘1’, ‘admin’, ‘123’);

Action处理请求参数
struts2 和 MVC 定义关系
StrutsPrepareAndExecuteFilter : 控制器
JSP : 视图
Action : 可以作为模型,也可以是控制器

struts2 Action 接受请求参数 :属性驱动 和 模型驱动
Action处理请求参数三种方式
第一种 :Action 本身作为model对象,通过成员setter封装 (属性驱动 )
页面:
用户名

Action :
public class RegistAction1 extends ActionSupport {
private String username;
public void setUsername(String username) {
this.username = username;
}
}
问题一: Action封装数据,会不会有线程问题 ?
* struts2 Action 是多实例 ,为了在Action封装数据 (struts1 Action 是单例的 )
问题二: 在使用第一种数据封装方式,数据封装到Action属性中,不可能将Action对象传递给 业务层
* 需要再定义单独JavaBean ,将Action属性封装到 JavaBean

第二种 :创建独立model对象,页面通过ognl表达式封装 (属性驱动)
页面:
用户名
—– 基于OGNL表达式的写法
Action:
public class RegistAction2 extends ActionSupport {
private User user;
public void setUser(User user) {
this.user = user;
}

        public User getUser() {            return user;        }    }

问题: 谁来完成的参数封装

第三种 :使用ModelDriven接口,对请求数据进行封装 (模型驱动 ) —– 主流
页面:
用户名

Action :
public class RegistAction3 extends ActionSupport implements ModelDriven {
private User user = new User(); // 必须手动实例化
public User getModel() {
return user;
}
}
* struts2 有很多围绕模型驱动的特性
* 为模型驱动提供了更多特性

对比第二种、第三种 : 第三种只能在Action中指定一个model对象,第二种可以在Action中定义多个model对象


封装数据到Collection和Map
1) 封装数据到Collection 对象
页面:
产品名称

Action :
public class ProductAction extends ActionSupport {
private List products;

        public List<Product> getProducts() {            return products;        }        public void setProducts(List<Product> products) {            this.products = products;        }    }

2) 封装数据到Map 对象
页面:
产品名称
======= one是map的键值
Action :
public class ProductAction2 extends ActionSupport {
private Map