@jfinal
项目中用到了分表 没有使用Model 也就不能使用ActiveRecordPlugin 使用Db+Record模式 分表的时候每个用户开户的时候生成一套表结构 比如student5 grade11这种类似。
在页面表单中提交的数据不能用getModel得到 所以仿照getModel的写法写了一套getRecord的实现,方便分表的项目用Db+Record的方式 接受表单提交的数据。
import java.util.Map; import java.util.Map.Entry; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import com.jfinal.kit.StrKit; import com.jfinal.plugin.activerecord.Record; public class RecordInjector { public static <T> T inject(Class<?> recordClass, HttpServletRequest request, boolean skipConvertError) { String recordName = recordClass.getSimpleName(); return inject(recordClass, StrKit.firstCharToLowerCase(recordName), request, skipConvertError); } @SuppressWarnings({ "unchecked" }) public static final <T> T inject(Class<?> recordClass, String recordName, HttpServletRequest request, boolean skipConvertError) { Record record = null; try { record = (Record) recordClass.newInstance(); } catch (Exception e) { } injectRecord(record, recordName, request, recordClass, skipConvertError); return (T)record; } private static final void injectRecord(Record record, String recordName, HttpServletRequest request, Class<?> recordClass, boolean skipConvertError) { Map<String, String[]> paramMap=request.getParameterMap(); String start =recordName+"."; String[] value=null; for (Entry<String, String[]> param: paramMap.entrySet()) { if(!param.getKey().startsWith(start)){ continue; } value=param.getValue(); if (ArrayUtils.isNotEmpty(value)&&StringUtils.isNotBlank(value[0])) { if(value.length==1){ record.set(StringUtils.removeStart(param.getKey(), start), value[0]); }else{ record.set(StringUtils.removeStart(param.getKey(), start), value); } }else{ record.set(StringUtils.removeStart(param.getKey(), start),null); } } } }
用法:自己封装一个BaseController 在里面添加getRecord实现
@SuppressWarnings("unchecked") public <T> T getRecord(String recordName) { return (T) getRecord(Record.class,recordName); }
public <T> T getRecord(Class<T> recordClass, String recordName) { return (T)RecordInjector.inject(recordClass, recordName, this.getRequest(), false); }
JFinal极速开发实战教程在线观看