2016-12-09 17:13

2016-12-08 13:31

处理好了,在自己的BaseService中重写了getModel方法的实现,给对象属性赋值时不再只取数组的第一个元素,先将数组转换成字符串,然后在进行赋值,这样的话多元素数组需要对应字符串类型的属性,方法如下

@SuppressWarnings("unchecked")
public static final T injectModel(Class modelClass, String modelName, HttpServletRequest request, boolean skipConvertError) {
Object temp = createInstance(modelClass);
if (temp instanceof Model == false) {
throw new IllegalArgumentException("getModel only support class of Model, using getBean for other class.");
}

Model model = (Model)temp;
Table table = TableMapping.me().getTable(model.getClass());
if (table == null) {
throw new ActiveRecordException("The Table mapping of model: " + modelClass.getName() +
" not exists or the ActiveRecordPlugin not start.");
}

String modelNameAndDot = StrKit.notBlank(modelName) ? modelName + "." : null;
Map parasMap = request.getParameterMap();
// 对 paraMap进行遍历而不是对table.getColumnTypeMapEntrySet()进行遍历,以便支持 CaseInsensitiveContainerFactory
// 以及支持界面的 attrName有误时可以感知并抛出异常避免出错
for (Entry entry : parasMap.entrySet()) {
String paraName = entry.getKey();
String attrName;
if (modelNameAndDot != null) {
if (paraName.startsWith(modelNameAndDot)) {
attrName = paraName.substring(modelNameAndDot.length());
} else {
continue ;
}
} else {
attrName = paraName;
}

Class colType = table.getColumnType(attrName);
if (colType == null) {
if (skipConvertError) {
continue ;
} else {
throw new ActiveRecordException("The model attribute " + attrName + " is not exists.");
}
}

try {
String[] paraValueArray = entry.getValue();
String paraValue = "";
if(paraValueArray != null && paraValueArray.length > 0){
//当数组长度大于1时,对应属性的类型必须为字符串,否则抛出类型转换异常
for(int i= 0; i < paraValueArray.length; i++){
paraValue += "," + paraValueArray[i];
}
}
if(!paraValue.isEmpty()){
paraValue = paraValue.substring(1);
model.set(attrName, TypeConverter.convert(colType, paraValue));
} else {
model.set(attrName, null);
}
} catch (Exception e) {
if (skipConvertError == false) {
throw new RuntimeException("Can not convert parameter: " + paraName, e);
}
}
}

return (T)model;
}

2016-12-08 11:43

好的,谢谢提示,弄好以后一定及时分享