JavaWeb -- Struts 数据传输:OGNL和类型转换
1. 数据传输:OGNL和类型转换
OGNL和struts2
OGNL:Object-Graph Navigation Language.
OGNL是集成进struts框架中比较强大的技术有助于数据传输和类型转换.OGNL由表达式语言和类型转换器组成.
内置类型转换器
用ognl表达式映射表单字段名到属性
<s:form action="Register">
<s:textfield name="user.username" label="Username"/>
<s:password name="user.password" label="Password"/>
<s:textfield name="user.portfolioName" label="Enter a name "/>
<s:textfield name="user.age" label="Enter your age as a double "/>
<s:textfield name="user.birthday" label="birthday. (mm/dd/yy)"/>
<s:submit/>
</s:form>
private User user;//get/set
User{
private String username;
private String password;
private String portfolioName;
private Double age;
private Date birthday;//get/set
内置类型转换器
List
private List lastNames ;
private List middleNames ;
不必预初始化集合.默认转换成字符串.
<s:textfield name="middleNames[0]" label="middleNames"/>
<s:textfield name="middleNames[1]" label="middleNames"/>
<s:textfield name="middleNames[2]" label="middleNames"/>
<s:textfield name="lastNames" label="lastNames"/>
<s:textfield name="lastNames" label="lastNames"/>
<s:textfield name="lastNames" label="lastNames"/>
若想显式指定数据类型,可通过属性文件的方式指定[XxxAction-conversion.properties].属性文件和Action类放在一起.
内置类型转换器
Map<String,String>
<s:textfield name="maidenNames.mary" label="Maiden Name"/>
<s:textfield name="maidenNames.jane" label="Maiden Name"/>
<s:textfield name="maidenNames.hellen" label="Maiden Name"/>
<s:textfield name="maidenNames['beth']" label="Maiden Name"/>
<s:textfield name="maidenNames['sharon']" label="Maiden Name"/>
<s:textfield name="maidenNames['martha']" label="Maiden Name"/>
private Map maidenNames ;
Map<String,User>
Element_myUsers=manning.utils.User
<s:textfield name="myUsers['chad'].username" label="names"/>
<s:textfield name="myUsers.chad.birthday" label="birthday"/>
private Map myUsers ;
自定义类型转换器
实现ognl.TypeConverter接口
继承类org.apache.struts2.util.StrutsTypeConverter.
public abstract Object convertFromString(Map context, String[] values,Class toClass);
public abstract String convertToString(Map context, Object o);
在字符串和Date对象间转换(续)
public class DateTypeConverter extends StrutsTypeConverter {
public Object convertFromString(Map context, String[] values, Class toClass) {
String date = values[0];
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
return sdf.parse(date);
}
public String convertToString(Map context, Object o) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
return sdf.format((Date)o);
}
}
配置自定义类型转换器
属性相关:
属性文件同action类放在一起, action中list map使用泛型定义,无需配置转换类型
private Date birthday;
[CustomConverterTest-conversion.properties]
birthday=manning.utils.DateTypeConverter
全局类型转换: 放在 src目录下
[classpath]/xwork-conversion.properties
manning.utils.Circle=manning.utils.DateTypeConverter
---------------- 实例 address 转换, 输入 aa-bb-cc 转为 Address-----------
address.java domain
/** * 地址类 */ public class Address { private String province; private String city; private String street; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String toString() { return province + "-" + city + "-" + street; } }
注册提交Jsp, reg,jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>reg.jsp</title> </head> <body> <s:debug /> <s:form namespace="/ognl" action="OgnlAction_reg" method="post"> <s:textfield name="names" label="UserName" /> <s:textfield name="names" label="UserName" /> <s:textfield name="ages[0]" label="UserAge" /> <s:textfield name="ages[1]" label="UserAge" /> <s:textfield name="namesListNoGen" label="namesListNoGen" /> <s:textfield name="namesListNoGen" label="namesListNoGen" /> <s:textfield name="agesListNoGen[0]" label="agesListNoGen" /> <s:textfield name="agesListNoGen[1]" label="agesListNoGen" /> <s:textfield name="userList[0].name" label="userList[0].name" /> <s:textfield name="userList[1].name" label="userList[1].name" /> <s:textfield name="userList[0].age" label="userList[0].age" /> <s:textfield name="userList[1].age" label="userList[1].age" /> <s:textfield name="userMap[100].name" label="userMap[100].name" /> <s:textfield name="userMap[100].age" label="userMap[100].age" /> <s:textfield name="userMap[200].name" label="userMap[200].name" /> <s:textfield name="userMap[200].age" label="userMap[200].age" /> <s:textfield name="comAddress" label="comAddress"/> <s:submit /> </s:form> <s:property value="comAddress"/> <s:url includeContext="true" /> </body> </html>
action
/** * OgnlAction */ public class OgnlAction extends ActionSupport { private static final long serialVersionUID = -6933309304624396640L; private String[] names = new String[2]; private Integer[] ages = new Integer[2]; /** * 不带泛型的集合 */ private List namesListNoGen ; private List agesListNoGen ; /* javabean集合 */ private List userList ; /* map */ private Map userMap; /* 公司地址 */ private Address comAddress ; public Address getComAddress() { return comAddress; } public void setComAddress(Address comAddress) { this.comAddress = comAddress; } public List getUserList() { return userList; } public void setUserList(List userList) { this.userList = userList; } public Integer[] getAges() { return ages; } public void setAges(Integer[] ages) { this.ages = ages; } public String[] getNames() { return names; } public void setNames(String[] names) { this.names = names; } public String reg() { return "success"; } public String toRegView() { System.out.println("toRegView"); return "loginView"; } /** * 保存数据, */ public String saveData(){ ServletActionContext.getRequest().setAttribute("username", "requestTom"); ServletActionContext.getRequest().getSession().setAttribute("username", "sessionTom"); ServletActionContext.getServletContext().setAttribute("username", "applicationTom"); ServletActionContext.getContext().getValueStack().set("username", "vsTom"); // popUserList(); return "showView" ; } /** * 组装用户集合 */ private void popUserList(){ userList = new ArrayList<User>(); User u = null ; for(int i = 0 ; i < 10 ; i ++){ u = new User(); u.setId(1 + i); u.setName("tom" + i); u.setAge(20 + i); userList.add(u); } } public List getNamesListNoGen() { return namesListNoGen; } public void setNamesListNoGen(List namesListNoGen) { this.namesListNoGen = namesListNoGen; } public List getAgesListNoGen() { return agesListNoGen; } public void setAgesListNoGen(List agesListNoGen) { this.agesListNoGen = agesListNoGen; } public Map getUserMap() { return userMap; } public void setUserMap(Map userMap) { this.userMap = userMap; } }
地址转换器
/** * 地址类型转换器 */ public class AddressConverter extends StrutsTypeConverter { /** * 将字符串转换成地址对象 */ public Object convertFromString(Map context, String[] values, Class toClass) { if(values != null && values.length > 0){ String str = values[0]; String[] ss = str.split("-"); if(ss != null && ss.length > 2){ Address a = new Address(); a.setProvince(ss[0]); a.setCity(ss[1]); a.setStreet(ss[2]); return a ; } } return null; } /** * 将地址对象转换成字符串信息 */ public String convertToString(Map context, Object o) { if(o instanceof Address){ return o.toString(); } return null; } }
OgnlAction-conversion.properties 转换类型配置, 放在action同目录,命名方式固定
Element_agesListNoGen=java.lang.Integer Element_userList=cn.itcast.struts2.model.User Element_userMap=cn.itcast.struts2.model.User Key_userMap=java.lang.Integer #comAddress=cn.itcast.struts2.ognl.AddressConverter
xwork-conversion.properties 类型转换,全局配置文件,放在src目录下,命名固定
cn.itcast.struts2.model.Address=cn.itcast.struts2.ognl.AddressConverter