Mapã¨ã®ãã©ã¡ã¼ã¿ãã¤ã³ãã£ã³ã°
å ¥åã³ã³ãã¼ãã³ããåçã«å¢æ¸ãããã©ã¼ã ãä½ãããå ´åãããã¾ãããªã¹ãç¶ã®é ç®ãã1ã¤ã®ç»é¢å ã§ä¸æ¬ç·¨éãããã¨ããªã©ã§ãã
ããããã¨ãã¯ãªã¯ã¨ã¹ããã©ã¡ã¼ã¿åãåçã«å¤åããã®ã§ããã©ã¡ã¼ã¿ãMapã«ãã¤ã³ãã£ã³ã°ã§ããã°ä¾¿å©ã ã¨æããCubbyã®å¦çãã«ã¹ã¿ãã¤ãºãã¦ã¿ã¾ããã
FormWrapperFactoryã®ã«ã¹ã¿ãã¤ãºï¼Map â HTMLãã©ã¼ã ï¼
ã¢ã¯ã·ã§ã³å ã§Mapåã®ãã£ã¼ã«ãã«ã»ããããåæå¤ããCubbyã®ãã©ã¼ã ç¨ã«ã¹ã¿ã ã¿ã°ï¼
org.seasar.cubby.controller.impl.FormWrapperFactoryImplã以ä¸ã®ããã«ä¸é¨æ¸ãæãã¦ãcubby.diconã«ç»é²ãã¾ãã
public class MappableFormWrapperFactory implements FormWrapperFactory { ... private class FormWrapperImpl implements FormWrapper { public String[] getValues(String name) { ... // å¤æ´ãããã Object value; if (form instanceof Map) { value = ((Map<?, ?>) form).get(name); } // å¤æ´ããã¾ã§ // 以ä¸ã¯BeanDescã使ã£ãå¾æ¥éãã®å¦ç else { BeanDesc beanDesc = BeanDescFactory .getBeanDesc(form.getClass()); if (!beanDesc.hasPropertyDesc(name)) { return null; } PropertyDesc propertyDesc = beanDesc.getPropertyDesc(name); value = propertyDesc.getValue(form); } ... } } ...
RequestParameterBinderã®ã«ã¹ã¿ãã¤ãºï¼HTMLãã©ã¼ã â Mapï¼
ã¢ã¯ã·ã§ã³ã®submitæã@Formã¢ããã¼ã·ã§ã³ã§ãã¤ã³ãã£ã³ã°å ã¨ãã¦Mapåã®ãã£ã¼ã«ããæå®ãããå ´åã«ã¯ãMapã«ãã©ã¡ã¼ã¿åã¨å¤ã®ãã¢ãæ ¼ç´ããããã«ãã¾ããå¤ã¯ã¨ãããããç¡æ¡ä»¶ã§Stringã«å¤æãã¦ãã¾ãã
ãã®ã¯ã©ã¹ããorg.seasar.cubby.controller.impl.RequestParameterBinderImplã®ä»£ããã«cubby.diconã«ç»é²ãã¾ãã
public class MappableRequestParameterBinder implements RequestParameterBinder { private RequestParameterBinderImpl defaultBinder; private ConverterFactory converterFactory; private ConversionHelper conversionHelper; public MappableRequestParameterBinder() { defaultBinder = new RequestParameterBinderImpl(); } public void setConverterFactory(ConverterFactory converterFactory) { defaultBinder.setConverterFactory(converterFactory); this.converterFactory = converterFactory; } public void setConversionHelper(ConversionHelper conversionHelper) { defaultBinder.setConversionHelper(conversionHelper); this.conversionHelper = conversionHelper; } public void bind(Map<String, Object[]> parameterMap, Object dest, Class<? extends Action> actionClass, Method method) { if (!(dest instanceof Map)) { defaultBinder.bind(parameterMap, dest, actionClass, method); return; } Map<Object, Object> map = (Map<Object, Object>) dest; for (Entry<String, Object[]> entry : parameterMap.entrySet()) { String sourceName = entry.getKey(); map.put(sourceName, convert(entry.getValue())); } } private Object convert(Object[] values) { Converter converter = converterFactory.getConverter(values[0] .getClass(), String.class); if (converter != null) { return converter.convertToObject(values[0], String.class, conversionHelper); } else { return values[0]; } } }
submitããããã©ã¡ã¼ã¿ãä¸å¾ã§Stringã«ãªã£ã¦ãã¾ã£ããããã¾ãã¡ãªæãããã¾ãããããã§å¤å°ãåçãªãã©ã¼ã ãä½ãããããªãã¾ãã