struts2参数封装---【小白系列】0基础到熟练应用struts2框架(五)

来源:互联网 发布:软件 开发 价格 编辑:程序博客网 时间:2024/06/05 05:10

web阶段我们封装参数,是使用BeanUtils 和request.getparamsMap来进行封装的,实际的本质就是,被封装对象的属性名字,与参数是否同名

同名的话就封装进去,不同名就放弃

匹配map中的key 是否与对象中的属性一致

匹配表单项的name 是否与对象中的属性 一致

1.属性封装(属性驱动)

普通属性:


实体属性:



注意:

1)属性驱动封装实体时 该实体在Action中的get和set方法都要提供,如果不提供get,每次封装一个参数,都会new一个对象。

2)属性驱动可以一次性封装多个实体

3)

属性驱动封装实体时,页面的表单项内部的name需要写成ognl形式

需要在name中执行该值要封装给哪个实体模型

2.模型封装(模型驱动)

模型驱动是在Ation中直接指定该模型是谁

通过接口ModelDriven<T>  T指的就是模型的类型



比较模型驱动与属性驱动?

                       1、模型驱动在Action中直接指定模型是谁,页面表单不用再次执行实体是谁

                             模型驱动可以简化页面name属性值的写法

                       2、属性驱动在表单中指定实体(模型)是谁  属性驱动可以一次性封装多个实体

                             模型驱动一次性只能封装一个实体

                       3、注意点:

                             属性驱动的实体必须提供get和set方法

                             模型驱动的实体必须手动实例化

3.属性驱动封装复杂数据(集合数据)(了解)

<h1>提交普通参数到Action中</h1><form action="${pageContext.request.contextPath }/param1_show.action" method="post">用户名<input type="text" name="username"><br/><input type="submit" value="提交"></form><h1>提交实体参数到Action中(属性驱动)</h1><form action="${pageContext.request.contextPath }/param1_show.action" method="post"><!-- name的写法是一种ognl的写法 -->客户名称<input type="text" name="customer.cust_name"><br/>客户等级<input type="text" name="customer.cust_level"><br/>客户电话<input type="text" name="customer.cust_phone"><br/>客户手机<input type="text" name="customer.cust_mobile"><br/>城市<input type="text" name="address.city"><br/>区域<input type="text" name="address.area"><br/><input type="submit" value="提交"></form><h1>提交实体参数到Action中(模型驱动)</h1><form action="${pageContext.request.contextPath }/param2_show.action" method="post">客户名称<input type="text" name="cust_name"><br/>客户等级<input type="text" name="cust_level"><br/>客户电话<input type="text" name="cust_phone"><br/>客户手机<input type="text" name="cust_mobile"><br/><input type="submit" value="提交"></form><h1>属性驱动封装字符串的List集合</h1><form action="${pageContext.request.contextPath }/param3_show.action" method="post"><input type="text" name="strList[0]"><br/><input type="text" name="strList[1]"><br/><input type="text" name="strList[2]"><br/><input type="text" name="strList[3]"><br/><input type="submit" value="提交"></form><h1>属性驱动封装customer实体的List集合</h1><form action="${pageContext.request.contextPath }/param3_show.action" method="post"><input type="text" name="custList[0].cust_name"><br/><input type="text" name="custList[0].cust_level"><br/><input type="text" name="custList[0].cust_phone"><br/><input type="text" name="custList[0].cust_mobile"><br/><input type="text" name="custList[1].cust_name"><br/><input type="text" name="custList[1].cust_level"><br/><input type="text" name="custList[1].cust_phone"><br/><input type="text" name="custList[1].cust_mobile"><br/><input type="submit" value="提交"></form><h1>属性驱动封装customer实体的Map集合</h1><form action="${pageContext.request.contextPath }/param3_show.action" method="post"><input type="text" name="custMap['customer1'].cust_name"><br/><input type="text" name="custMap['customer1'].cust_level"><br/><input type="text" name="custMap['customer1'].cust_phone"><br/><input type="text" name="custMap['customer1'].cust_mobile"><br/><input type="text" name="custMap['customer2'].cust_name"><br/><input type="text" name="custMap['customer2'].cust_level"><br/><input type="text" name="custMap['customer2'].cust_phone"><br/><input type="text" name="custMap['customer2'].cust_mobile"><br/><input type="submit" value="提交"></form>

Param1Action

//客户端提交的参数 username=zhangsanprivate String username;public void setUsername(String username) {this.username = username;}//cust_name=zhangsan&cust_level=VIP&cust_phone=138&cust_mobile=138/* * 注意:如果属性驱动封装实体属性 并且该实体有多个属性 * 那么该实体必须通过get和set方法 */private Customer customer;public void setCustomer(Customer customer) {this.customer = customer;}public Customer getCustomer() {return customer;}private Address address;public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}public String show(){//System.out.println(username);System.out.println(customer);System.out.println(address);return NONE;}

Param2Action

//模型对象必须手动实例化private Customer customer = new Customer();@Overridepublic Customer getModel() {return customer;}public String show(){System.out.println(customer);return NONE;}

Param3Action

private List<String> strList;private List<Customer> custList;private Map<String,Customer> custMap;public void setStrList(List<String> strList) {this.strList = strList;}public List<String> getStrList() {return strList;}public void setCustList(List<Customer> custList) {this.custList = custList;}public void setCustMap(Map<String, Customer> custMap) {this.custMap = custMap;}public List<Customer> getCustList() {return custList;}public Map<String, Customer> getCustMap() {return custMap;}public String show(){System.out.println(strList);System.out.println(custList);System.out.println(custMap);return NONE;}


阅读全文
0 0
原创粉丝点击