Created
June 9, 2014 03:00
-
-
Save A-pZ/188e6aff5665e817c5d1 to your computer and use it in GitHub Desktop.
Struts2の自動型変換(2014 summer version) ref: http://qiita.com/alpha_pz/items/478f963b7d2d5e9e2da6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BigDecimalConverter extends StrutsTypeConverter { | |
public Object convertFromString( | |
Map context, | |
String[] values, | |
Class toClass) { | |
// 画面→Actionクラス変換 | |
} | |
public String convertToString(Map paramMap, Object paramObject) { | |
// 画面←Actionクラス変換(正常系のみ) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BigDecimalConverter extends StrutsTypeConverter { | |
/** | |
* リクエストパラメータからの置換を行う。 | |
* @param Map コンテキスト情報Map | |
* @param String[] パラメータの配列 | |
* @param Class 変換後のクラス | |
* @return 型変換後のパラメータ(オブジェクト) | |
*/ | |
@Override | |
public Object convertFromString( | |
Map context, String[] values, Class toClass) { | |
Object result = null; | |
log.debug("convertFromString -> " + context + ", " | |
+ values.toString() + ", " + toClass.getName()); | |
if (values != null) { | |
if (values.getClass().isArray() && toClass.isArray()) { | |
Class<?> componentType = toClass.getComponentType(); | |
result = Array.newInstance(componentType, | |
Array.getLength(values)); | |
for (int i = 0, icount = Array.getLength(values); i < icount; i++) { | |
Array.set(result, i, | |
convertBigDecimalValue( | |
context , | |
Array.get(values, i) , | |
toClass | |
) | |
); | |
} | |
} else { | |
// カンマを取り除く | |
result = convertBigDecimalValue(context , values[0] , toClass); | |
} | |
} | |
return result; | |
} | |
/** | |
* BigDecimalへの型変換。 | |
* | |
* @param Map コンテキスト情報Map | |
* @param Object 変換対象の文字列 | |
* @param Class 変換後のクラス | |
* @return 型変換した後のオブジェクト | |
*/ | |
protected Object convertBigDecimalValue( | |
Map context ,Object paramArrayOfString ,Class toClass | |
) { | |
BigDecimal result; | |
String value = (String)paramArrayOfString; | |
value = value.replace(",", ""); | |
try { | |
// Struts2標準で持っているBigDecimal変換 | |
result = bigDecValue(value); | |
} catch (NumberFormatException e) { | |
// 型変換エラーをStruts2へ通知する = JSPは変換前を表示 | |
super.performFallbackConversion(context, paramArrayOfString, toClass); | |
return value; | |
} | |
return result; | |
} | |
/** | |
* BigDecimalからStringへの変換を行う。 | |
* @param paramMap コンテキスト情報Map | |
* @param paramObject Actionクラスのフィールド | |
* @return 画面に表示する文字列 | |
*/ | |
public String convertToString(Map paramMap, Object paramObject) { | |
if ( paramObject instanceof BigDecimal ) { | |
NumberFormat fmt = NumberFormat.getInstance(); | |
BigDecimal dec = new BigDecimal(paramObject.toString()); | |
return fmt.format(dec); | |
} else { | |
return paramObject.toString(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# BigDecimal | |
java.math.BigDecimal=lumi2.converter.BigDecimalConverter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment