Struts2---基础总结二

来源:互联网 发布:算法设计的要求 编辑:程序博客网 时间:2024/05/16 01:28

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

struts2 Action 接受请求参数 :属性驱动 和 模型驱动
Action处理请求参数三种方式

第一种 :Action 本身作为model对象,通过成员setter封装 (属性驱动 )    页面:        用户名  <input type="text" name="username" /> <br/>    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表达式封装 (属性驱动)    页面:         用户名  <input type="text" name="user.username" /> <br/>  ----- 基于OGNL表达式的写法    Action:        public class RegistAction2 extends ActionSupport {            private User user;            public void setUser(User user) {                this.user = user;            }            public User getUser() {                return user;            }        }

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

<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
第三种 :使用ModelDriven接口,对请求数据进行封装 (模型驱动 ) ----- 主流    页面:        用户名  <input type="text" name="username" /> <br/>      Action :        public class RegistAction3 extends ActionSupport implements ModelDriven<User> {            private User user = new User(); // 必须手动实例化            public User getModel() {                return user;            }        }
  • struts2 有很多围绕模型驱动的特性
    * 为模型驱动提供了更多特性

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

<input type="text" name="user.username" /> <input type="text" name="product.info" />

封装数据到Collection和Map

1) 封装数据到Collection 对象     页面:        产品名称 <input type="text" name="products[0].name" /><br/>    Action :        public class ProductAction extends ActionSupport {            private List<Product> products;            public List<Product> getProducts() {                return products;            }            public void setProducts(List<Product> products) {                this.products = products;            }        }
2) 封装数据到Map 对象     页面:        产品名称 <input type="text" name="map['one'].name" /><br/>  =======  one是map的键值    Action :        public class ProductAction2 extends ActionSupport {            private Map<String, Product> map;            public Map<String, Product> getMap() {                return map;            }            public void setMap(Map<String, Product> map) {                this.map = map;            }        }   
0 0