一:简单数据的封装
一类:属性驱动
1.1 : 在Action中提供 一个与表单项对应的 set 方法. (属性驱动)
//页面:
用户名:<input type="text" name="username"><br/>
//Action:
private String username;
public void setUsername(String username) {
this.username = username;
}
* 缺点:自己封装 实体类对象
1.2 :采用OGNL的表达式写法 直接封装到一个对象中.(属性驱动)
*** 第一:必须继承ActionSupport*** 第二:对user 提供一个 get方法 一个set方法.
页面:
<span style="font-size:18px;"> * 用户名:<input type="text" name="user.username"><br/>
// user : user对象的username</span>
//Action
private User user;
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
***** 在Action中 必须提供对象的get方法
如果不提供get方法 Struts2得不到User对象,那么封装数据 就会类似于
User[username=之哥,password=null,age=”0“]
因为没有get方法 所以拿不到对象 每次都只能new
二类:模型驱动
1.3 : Action 实现 一个接口 ModelDriven 封装数据.(模型驱动.)* 在接口中提供一个方法getModel() 返回就是要封装的对象.
***** 前提 手动构建该对象 User user = new User();
<span style="white-space:pre"> </span>//页面:
<span style="white-space:pre"> </span>用户名:<input type="text" name="username"><br/>
//Action:
public class LoginAction3 extends ActionSupport implements ModelDriven<User>{
private User user = new User();
public User getModel() {
return user;
}
}
***** 在企业中应用比较多的是 第三种. struts2中 有很多内容围绕着模型驱动来实现.
* 第三种有单独的拦截器
<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
比较第二种 和 第三种封装形式?
第三种 只能将数据 封装到一个实体对象.
第二种 向多个实体中封装数据 . user.username product.price
***** 是谁完成的数据封装?
<interceptor name="params"class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
二:复杂数据的封装.(了解)
1 封装到一个集合中 Collection Map
封装List 页面:
<form action="${ pageContext.request.contextPath }/product1.action" method="post">
<span style="white-space:pre"> </span>商品名称<Input type="text" name="products[0].name"><br/>
商品价格<Input type="text" name="products[0].price"><br/>
<span style="white-space:pre"> </span>商品名称<Input type="text" name="products[1].name"><br/>
商品价格<Input type="text" name="products[1].price"><br/>
<input type="submit" value="提交">
</form>
Action:
private List<Product> products;
public void setProducts(List<Product> products) {
<span style="white-space:pre"> </span>this.products = products;
}
public List<Product> getProducts() {
return products;
}
封装到Map
页面:
<form action="${ pageContext.request.contextPath }/product2.action" method="post">
商品名称<Input type="text" name="map['one'].name"><br/>
商品价格<Input type="text" name="map['one'].price"><br/>
商品名称<Input type="text" name="map['two'].name"><br/>
商品价格<Input type="text" name="map['two'].price"><br/>
<input type="submit" value="提交">
</form>
Action:
private Map<String,Product> map;
public Map<String, Product> getMap() {
return map;
}
public void setMap(Map<String, Product> map) {
this.map = map;
}
2 参数封装过程中,数据类型转换问题 1.Struts2的内部提供了一组 类型转化器.
Struts2内置了常见数据类型多种转换器
boolean 和 Boolean
char和 Character
int 和 Integer
long 和 Long
float 和 Float
double 和 Double
Date 可以接收 yyyy-MM-dd格式字符串
数组 可以将多个同名参数,转换到数组中
集合 支持将数据保存到 List 或者 Map 集合
类型转换出错的异常:
正常输入 年龄 23 可以进行类型转换的
setAge(int age) --- 完成数据封装
错误输入 年龄 abc 抛出一个异常.
setAge(int age) --- > setAge(String str)
* java.lang.NoSuchMethodException ---- 类型出错了.
完成自定义类型
* 实现 TypeConverterconvertValue(java.util.Map<java.lang.String,java.lang.Object> context, java.lang.Object target, java.lang.reflect.Member member, java.lang.String propertyName, java.lang.Object value, java.lang.Class toType)
* 继承 DefaultTypeConverter
convertValue(java.util.Map<java.lang.String,java.lang.Object> context, java.lang.Object target, java.lang.reflect.Member member,java.lang.String propertyName, java.lang.Object value, java.lang.Class toType)
convertValue(java.util.Map<java.lang.String,java.lang.Object> context, java.lang.Object value, java.lang.Class toType)
* 继承StrutsTypeConverter
convertFromString(java.util.Map context, java.lang.String[] values, java.lang.Class toClass)
convertToString(java.util.Map context, java.lang.Object o)
**** 类型转换 本身就是一个双向
JSP --- > Action --- > JSP
步骤1:创建一个类 继承 DefaultTypeConverter
步骤2: 重写一个方法convertValue(三个参数)
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
// 类型转换 双向 JSP ---> Action String[] --- Date
// 在JSP中回显数据 Action ---- > JSP Date --- > String
DateFormat format = new SimpleDateFormat("yyyy/MM/dd");
if(toType == Date.class){
// JSP ---> Action
String[] arrs = (String[]) value;
String s = arrs[0];
try {
Date d = format.parse(s);
return d;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
// ACtion --- > jSP
Date d = (Date) value;
String s = format.format(d);
return s;
}
return null;
}
步骤3:注册类型转化器.
两种方式注册:
局部方式注册: 针对一个Action中的某个字段生效
* 在当前的Action包下 新建一个文件 :格式 Action的类名-conversion.properties
* 属性文件的key 就是 要针对页面的表单元素的名称的值 .
* birthday=cn.zhige.struts2.demo3.MyDateConverter
全局方式注册: 针对所有Action 中 Date类型.
* 在src新建文件 xwork-conversion.properties
* 属性文件的key 类型名
java.util.Date=cn.zhige.struts2.demo3.MyDateConverter
类型转换的错误处理:
报错误:
No result defined for action cn..zhigestruts2.demo3.CustomerAction and result input
分析错误流程
显示错误信息 : 显示成中文.
* 在Action包下 新建一个 Action类名.properties
* key : invalid.fieldvalue.属性名 = 错误信息!