struts2或webwork嵌套集合类型转换
给出例子代码
public class TestAction{ private List users; ////// getter setter } .... public class User{ private String username; ////getter setter }
Element_users = com.xxxx.User(早期用Collection_)
<input name="users[0].username"/>
public class User{ private String username; private List groups; ////getter setter }
public class Group{ private String gname; ////getter setter }
<input name=users[0][0].gname />
public class XWorkListPropertyAccessor extends ListPropertyAccessor{ public XWorkListPropertyAccessor() { _sAcc = new XWorkCollectionPropertyAccessor(); } public Object getProperty(Map context, Object target, Object name) throws OgnlException { if(OgnlContextState.isGettingByKeyProperty(context) || name.equals("makeNew")) return _sAcc.getProperty(context, target, name); if(name instanceof String) return super.getProperty(context, target, name); OgnlContextState.updateCurrentPropertyPath(context, name); Class lastClass = (Class)context.get("last.bean.accessed"); String lastProperty = (String)context.get("last.property.accessed"); if((name instanceof Number) && OgnlContextState.isCreatingNullObjects(context) && XWorkConverter.getInstance().getObjectTypeDeterminer().shouldCreateIfNew(lastClass, lastProperty, target, null, true)) { List list = (List)target; int index = ((Number)name).intValue(); int listSize = list.size(); if(lastClass == null || lastProperty == null) return super.getProperty(context, target, name); Class beanClass = XWorkConverter.getInstance().getObjectTypeDeterminer().getElementClass(lastClass, lastProperty, name); if(listSize <= index) { Object result = null; for(int i = listSize; i < index; i++) list.add(null); try { list.add(index, result = ObjectFactory.getObjectFactory().buildBean(beanClass, context)); } catch(Exception exc) { throw new XworkException(exc); } return result; } if(list.get(index) == null) { Object result = null; try { list.set(index, result = ObjectFactory.getObjectFactory().buildBean(beanClass, context)); } catch(Exception exc) { throw new XworkException(exc); } return result; } } return super.getProperty(context, target, name); } //////////setProperty private XWorkCollectionPropertyAccessor _sAcc;}
public class MoreXWorkListPropertyAccessor extends XWorkListPropertyAccessor{ public Object getProperty(Map context, Object target, Object name) throws OgnlException { if(OgnlContextState.isGettingByKeyProperty(context) || name.equals("makeNew")) return _sAcc.getProperty(context, target, name); if(name instanceof String) return super.getProperty(context, target, name); OgnlContextState.updateCurrentPropertyPath(context, name); Class lastClass = (Class)context.get("last.bean.accessed"); String lastProperty = (String)context.get("last.property.accessed"); if((name instanceof Number) && OgnlContextState.isCreatingNullObjects(context) && XWorkConverter.getInstance().getObjectTypeDeterminer().shouldCreateIfNew(lastClass, lastProperty, target, null, true)) { List list = (List)target; int index = ((Number)name).intValue(); int listSize = list.size(); if(lastClass == null || lastProperty == null) return super.getProperty(context, target, name); Class beanClass = XWorkConverter.getInstance().getObjectTypeDeterminer().getElementClass(lastClass, lastProperty, name); if(listSize <= index) { Object result = null; for(int i = listSize; i < index; i++) list.add(null); try { list.add(index, result = ObjectFactory.getObjectFactory().buildBean(beanClass, context)); } catch(Exception exc) { throw new XworkException(exc); } if(result instanceof List){ OgnlContextState.setLastBeanPropertyAccessed(context,lastProperty+"_i"); } return result; } if(list.get(index) == null) { Object result = null; try { list.set(index, result = ObjectFactory.getObjectFactory().buildBean(beanClass, context)); } catch(Exception exc) { throw new XworkException(exc); } if(result instanceof List){ OgnlContextState.setLastBeanPropertyAccessed(context,lastProperty+"_i"); } return result; } } return super.getProperty(context, target, name); // 单例 instance = new MoreXWorkListPropertyAccessor(); private MoreXWorkListPropertyAccessor{} public MoreXWorkListPropertyAccessor getInstance(){return instance;} }
public class MoreDimensesSupportInterceptor extends AroundInterceptor{ public void before(ActionInvocation invocation)throws Exception{ OgnlRuntime.setPropertyAccessor(List.class,MoreXWorkListPropertyAccessor.getInstance()); }}