Struts2自定義局部類型轉換器
一、AddUActionpackage com;
import java.util.Date;
public class AddUAction {
private Date birdate;
public Date getBirdate() {
return birdate;
}
public void setBirdate(Date birdate) {
this.birdate = birdate;
}
public String addU(){
System.out.println(birdate);
return "ok";
}
}
二、struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "<struts>
<package name="te" namespace="/te" extends="struts-default">
<action name="teMan" class="com.AddUAction" method="addU">
<result name="ok">/index.jsp</result>
</action>
</package>
</struts>
三、DateConverter
package com;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Map;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
public class DateConverter extends DefaultTypeConverter {
@Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMdd");
try{
if(toType==Date.class){
String[] params=(String[]) value;
return dateFormat.parse(params[0]);
}else{
Date date=(Date) value;
return dateFormat.format(date);
}
}catch (Exception e) {
return null;
}
}
}
四、AddUAction-conversion.properties
birdate=com.DateConverter
五、包結構

六、在地址欄輸入
http://localhost:8080/test/te/teMan!addU?birdate=20010205
七、結果會在jsp頁面顯示出來
This is my JSP page.
Mon Feb 05 00:00:00 CST 2001
posted on 2012-07-20 14:38 何云隆 閱讀(1345) 評論(0) 編輯 收藏 所屬分類: Struts2