1. 數據傳輸:OGNL和類型轉換html
OGNL和struts2java
OGNL:Object-Graph Navigation Language.apache
OGNL是集成進struts框架中比較強大的技術有助於數據傳輸和類型轉換.OGNL由表達式語言和類型轉換器組成.session
內置類型轉換器app
用ognl表達式映射表單字段名到屬性框架
<s:form action="Register">dom
<s:textfield name="user.username" label="Username"/>jsp
<s:password name="user.password" label="Password"/>ide
<s:textfield name="user.portfolioName" label="Enter a name "/>post
<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
地址轉換器
- 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