所有的頁面與控制器傳遞的數據都是String類型,在對其進行處理時可能會用到各種的數據類型,程序無法自動完成數據類型的轉換,這就需要我們在代碼中進行手手動操作,這個過程就稱為類型轉換。
內置類型轉換器
在Web應用程序中,用戶在視圖層輸入的數據都是字符串,業務控制層在處理這些數據時,就必須把從視圖層傳遞過來的字符串進行類型轉換。Struts2提供了簡單易用的數據類型轉換機制,struts2提供的類型轉換如下:
1)String:將int、long、double、boolean、String類型的數組對象轉換為字符串
2)boolean/Boolean:在字符串和布爾值之間進行轉換
3)char/Character:在字符串和字符之間進行轉換
4)int/Integer,float/Float、long/Long、double/Double:在字符串和數值類型的數據之間進行轉換
5)Date:在字符串和日期類之間進行轉換。對于日期類型,采用SHORT格式來處理輸入和輸出,使用當前請求關聯的Locale來確定日期格式
6)數組類型(Array):由于數組元素本身就有類型,struts2使用元素類型對應的類型轉換器,將字符串轉換為數組元素的類型,然后再設置到新的數組中
7)Collection、List、Set:struts2會將用戶提交的字符串數據使用request對象的getparameterValues(string str)方法,將返回的字符串數據轉換成集合類型
OGNL表達式
Struts2框架支持OGNL表達式,通過OGNL表達式可以將用戶請求轉換為復合類型。
使用類型轉換注解
Struts2提供了一些類型轉換注解來配置轉換器,使得能夠代替ClassName-conversion.properties文件,其中包括以下注解:
1)TypeConversion注解。該注解應用于屬性和方法級別。
2)Conversion注解。Conversion注解讓類型轉換應用到類型級別,即可以應用到類、接口或枚舉聲明。該注解只有一個參數conversions。
3)Element注解。Element注解用于指定Collection或Map中的元素類型,該注解只能用于字段或方法級別。
4)Key注解。Key注解用于指定Map中的Key的類型,該注解只能用于字段或方法級別。
5)KeyProperty注解。Keyproperty注解指定用于索引集合元素中的屬性名,該注解只適用于字段或方法級別
6)CreatelfNull注解。CreateifNull注解指定在引用的集合元素為null時,是否讓框架重新創建該集合元素。該注解只適用于字段或方法級別
一個簡單的添加商品信息的實例:
在配置好Struts2環境后,
商品類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.mxl.entity; public class Product { private String name; //商品名稱 private double price; //商品價格 private int num; //入庫數量 private String content; //商品描述 public String getName() { return name; } public void setName(String name) { this .name = name; } public double getPrice() { return price; } public void setPrice( double price) { this .price = price; } public int getNum() { return num; } public void setNum( int num) { this .num = num; } public String getContent() { return content; } public void setContent(String content) { this .content = content; } } |
Action:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.mxl.actions; import com.mxl.entity.Product; import com.opensymphony.xwork2.ActionSupport; public class ProductAction extends ActionSupport{ private Product product; public Product getProduct() { return product; } public void setProduct(Product product) { this .product = product; } @Override public String execute() throws Exception { return SUCCESS; } } |
struts.xml中的配置:
1
2
3
4
|
</ action > < action name = "pro" class = "com.mxl.actions.ProductAction" > < result >/pro_success.jsp</ result > </ action > |
添加成功頁面:
<%@ taglib prefix="s" uri="/struts-tags" %>
1
2
3
4
|
商品名稱:< s:property value = "product.name" />< br />< br /> 商品價格:< s:property value = "product.price" />< br />< br /> 入庫數量:< s:property value = "product.num" />< br />< br /> 商品描述:< s:property value = "product.content" /> |
自定義類型轉換器實例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.mxl.converter; import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; import com.mxl.entity.Product; public class ProductConverter extends StrutsTypeConverter{ @Override public Object convertFromString(Map context, String[] values, Class toClass) { Product pro = new Product(); //實例化該類 String[] proValues = values[ 0 ].split( "/" ); //將傳遞過來的數組中的第一個元素以“/”分隔并組成新的數組 pro.setName(proValues[ 0 ]); //將新數組中的第一個元素賦值給product類中name屬性 pro.setPrice(doubleValue(proValues[ 1 ])); //將新數組中的第二個元素賦值給product類中price屬性 pro.setNum(Integer.parseInt(proValues[ 2 ])); //將新數組中的第三個元素賦值給product類中num屬性 pro.setContent(proValues[ 3 ]); //將新數組中的第4個元素賦值給product類中content屬性 return pro; } @Override public String convertToString(Map context, Object obj) { Product pro = (Product)obj; return "" ; } } |
商品類使用的是上邊的那個類,Action,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.mxl.actions; import com.mxl.entity.Product; import com.opensymphony.xwork2.ActionSupport; public class ProConverterAction extends ActionSupport{ private Product product1; private Product product2; public Product getProduct1() { return product1; } public void setProduct1(Product product1) { this .product1 = product1; } public Product getProduct2() { return product2; } public void setProduct2(Product product2) { this .product2 = product2; } @Override public String execute() throws Exception { return SUCCESS; } } |
配置:
1
2
3
|
< action name = "proConverter" class = "com.mxl.actions.ProConverterAction" > < result >/pro_list.jsp</ result > </ action > |
添加一個全局類型轉換器:
xwork-conversion.properties,
com.mxl.entity.Product=com.mxl.converter.ProductConverter
添加界面:
1
2
3
4
5
6
|
< font style = "font-size:12px; color:red" >在文本框中依次輸入商品的名稱、價格入庫數量和描述之間使用“/”分隔</ font > < s:form action = "proConverter.action" method = "post" cssStyle = "margin-top:0px;" > < s:textfield name = "product1" label = "商品1" size = "50" /> < s:textfield name = "product2" label = "商品2" size = "50" /> < s:submit value = "確認入庫" align = "left" /> </ s:form > |
添加成功后的跳轉界面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
< ul id = "heng" class = "addPro" > < li style = "font-weight:bold;" >商品名稱</ li > < li style = "font-weight:bold;" >商品價格</ li > < li style = "font-weight:bold;" >商品數量</ li > < li style = "font-weight:bold;" >商品描述</ li > </ ul > < ul id = "heng" class = "addPro" > < li >< s:property value = "product1.name" /></ li > < li >< s:property value = "product1.price" /></ li > < li >< s:property value = "product1.num" /></ li > < li >< s:property value = "product1.content" /></ li > </ ul > < ul id = "heng" class = "addPro" > < li >< s:property value = "product2.name" /></ li > < li >< s:property value = "product2.price" /></ li > < li >< s:property value = "product2.num" /></ li > < li >< s:property value = "product2.content" /></ li > </ ul > |
復合類型轉換異常處理實例:
User類,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package com.mxl.entity; import java.util.Date; public class User { private String username; //用戶名 private String password; //密碼 private String realname; //真實姓名 private int age; //年齡 private Date birthday; //生日 private String address; //家庭住址 public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } public String getRealname() { return realname; } public void setRealname(String realname) { this .realname = realname; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this .birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this .address = address; } } |
配置:
1
2
3
4
|
< action name = "userException" class = "com.mxl.actions.UserExceptionAction" > < result >/user_success.jsp</ result > < result name = "input" >/user_regist.jsp</ result > </ action > |
添加局部資源文件:
User-ExceptionAction.properties,
內容:
1
2
|
invalid.fieldvalue.user.age=會員年齡必須為整數 invalid.fieldvalue.user.birthday=會員出生日期必須為日期格式 |
注冊頁面Z:
1
2
3
4
5
6
7
8
9
10
|
[html] view plain copy print? < s:form action = "userException.action" method = "post" > < s:textfield name = "user.username" label = "用戶名" size = "15" /> < s:password name = "user.password" label = "密碼" size = "15" /> < s:textfield name = "user.realname" label = "姓名" size = "15" /> < s:textfield name = "user.age" label = "年齡" size = "15" /> < s:textfield name = "user.birthday" label = "出生日期" size = "15" /> < s:textfield name = "user.address" label = "家庭住址" size = "15" /> < s:submit type = "button" value = "提交" /> </ s:form > |
跳轉界面:
1
2
3
4
5
6
|
用戶名:< s:property value = "user.username" />< br />< br /> 密碼:< s:property value = "user.password" />< br />< br /> 真實姓名:< s:property value = "user.realname" />< br />< br /> 年齡:< s:property value = "user.age" />< br />< br /> 出生日期:< s:property value = "user.birthday" />< br />< br /> 家庭住址:< s:property value = "user.address" />< br />< br /> |
總結
以上就是本文關于struts2中類型轉換實例代碼的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。也希望朋友們對本站多多支持!
原文鏈接:http://blog.csdn.net/wojiaohuangyu/article/details/51508376