類型轉換器
類型轉換器的作用:
把表單傳到action的數據在中途中進行更改。
一、局部類型轉換器
流程:
表單提交數據->調用配置文件properties->調用自定義的數據轉換類->action接受數據->welcome.jsp輸出數據。
login.jsp
<form action="login.action" method="post">
學好:<input type="text" name="id"><br>
時間:<input type="text" name="bri"><br>
<input type="submit" value="提交">
</form>
LoginAction.java
package com.action;
import java.util.Date;
public class LoginAction {
private int id;
private Date bri;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getBri() {
return bri;
}
public void setBri(Date bri) {
this.bri = bri;
}
public String execute(){
System.out.println(id);
System.out.println(bri);
return "success";
}
}
定義類型轉換類:DateConverter
package com.converter;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
public class DateConverter extends DefaultTypeConverter{
@Override
public Object convertValue(Object arg0, Class arg1) {
String str = ((String[])arg0)[0];
System.out.println("11111");
if(arg1==Date.class){
try {
SimpleDateFormat sd = new SimpleDateFormat("yyyy/mm/dd");
return sd.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
配置文件:LoginAction-conversion.properties
bri=com.converter.DateConverter //數據轉換類的具體路徑
該配置文件必須在action的同級目錄下,且名稱必須爲:action名-conversion.properties。
注意:
1、定義類型轉換類時,使用Date類型是,會自動導入java.sql.Date包,要改爲:java.util.Date。
2、在實現抽象類時,Object類型參數是個數組。
二、全局類型轉換器
相對於局部類型轉換器,全局類型轉換器只需要更改properties配置文件即可。
新建配置文件必須在src目錄下,且名稱必須爲:xwork-conversion.properties。
配置文件:xwork-conversion.properties
java.util.Date=com.converter.DateConverter
三、轉換器中異常信息的處理
修改轉換器代碼:
package com.converter;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import com.opensymphony.xwork2.conversion.TypeConversionException;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
public class DateConverter extends DefaultTypeConverter{
@Override
public Object convertValue(Object arg0, Class arg1) {
String str = ((String[])arg0)[0];
System.out.println("11111");
if(arg1==Date.class){
try {
SimpleDateFormat sd = new SimpleDateFormat("yyyy/mm/dd");
if(!str.matches("^\\d{4}/\\d{2}/\\d{2}$")){
throw new TypeConversionException();
}
return sd.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
修改action繼承關係:
package com.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private int id;
private Date bri;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getBri() {
return bri;
}
public void setBri(Date bri) {
this.bri = bri;
}
public String execute(){
System.out.println(id);
System.out.println(bri);
return "success";
}
}
修改表單頁面,添加錯誤提示信息:
${fieldErrors.bri[0] }
<form action="login.action" method="post">
學好:<input type="text" name="id"><br>
時間:<input type="text" name="bri"><br>
<input type="submit" value="提交">
</form>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.