2011年11月15日 星期二
迴圈取得Map(Key,Value)
JDK1.5
Map m = new HashMap();
for (Object o : map.keySet()) {
map.get(o);
}
JDK1.4
Map map = new HashMap() ;
Iterator it = map.entrySet().iterator() ;
while (it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next() ;
Object key = entry.getKey() ;
Object value = entry.getValue() ;
}
標籤:
JAVA
2011年11月7日 星期一
ConversionException: No value specified for 'Date'
轉載自 http://www.mail-archive.com/user@commons.apache.org/msg02246.html
原因:當JavaBean用到了Date等非內建型態時,如果Object為NULL則會出現此異常。
解決方法:
You have a choice of two flavours for copying properties in BeanUtils:
1) Usng PropertyUtils copyProperties() method[1] - this just copies
any properties unchanged
2) Using BeanUtils copyProperties() method[2] - copies AND converts
properties - so for example, if theres a property called "foo" of type
String in the source and a property called "foo" of type Integer in
the destination then it tries to convert the String to an Integer
using the Converter registered for Integers.
If you don't need conversion then you can switch to using
PropertyUtils. If however you have some conversion needs, but you
don't want it to throw an exception when a value is "null" (which is
the default behaviour) - then you can configure a converter for Date
which has a default value. So for example, if you wanted a default of
"null" for Dates, then something like the following:
java.util.Date defaultValue = null;
DateConverter converter = new DateConverter(defaultValue);
ConvertUtils.register(converter, java.util.Date.class);
Then when you call BeanUtils.copyProperties() it should no longer
throw an exception.
[1] http://tinyurl.com/4rc5ho
[2] http://tinyurl.com/44csva
原因:當JavaBean用到了Date等非內建型態時,如果Object為NULL則會出現此異常。
解決方法:
You have a choice of two flavours for copying properties in BeanUtils:
1) Usng PropertyUtils copyProperties() method[1] - this just copies
any properties unchanged
2) Using BeanUtils copyProperties() method[2] - copies AND converts
properties - so for example, if theres a property called "foo" of type
String in the source and a property called "foo" of type Integer in
the destination then it tries to convert the String to an Integer
using the Converter registered for Integers.
If you don't need conversion then you can switch to using
PropertyUtils. If however you have some conversion needs, but you
don't want it to throw an exception when a value is "null" (which is
the default behaviour) - then you can configure a converter for Date
which has a default value. So for example, if you wanted a default of
"null" for Dates, then something like the following:
java.util.Date defaultValue = null;
DateConverter converter = new DateConverter(defaultValue);
ConvertUtils.register(converter, java.util.Date.class);
Then when you call BeanUtils.copyProperties() it should no longer
throw an exception.
[1] http://tinyurl.com/4rc5ho
[2] http://tinyurl.com/44csva
標籤:
JAVA
2011年11月3日 星期四
檔案下載時檔名在ie和firefox下面表現不一致問題
String agent = request.getHeader("USER-AGENT");
String fileName = desc+".xls";
if (null != agent && -1 != agent.indexOf("MSIE")){
fileName = java.net.URLEncoder.encode(fileName, "UTF8");
}else if (null != agent && -1 != agent.indexOf("Mozilla")){
fileName = MimeUtility.encodeText(fileName, "UTF8", "B");
}
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
//response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Content-disposition" , "attachment; filename=" + fileName);
標籤:
JAVA
訂閱:
文章 (Atom)