Jfinal 关于JspRender的完善,jfinaljsprender,/** * Copyri
分享于 点击 20537 次 点评:102
Jfinal 关于JspRender的完善,jfinaljsprender,/** * Copyri
/** * Copyright (c) 2011-2012, laojianhua. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.jfinal.render;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.Map.Entry;import javax.servlet.http.HttpServletRequest;import com.jfinal.plugin.activerecord.CPI;import com.jfinal.plugin.activerecord.Model;import com.jfinal.plugin.activerecord.Page;import com.jfinal.plugin.activerecord.Record;import com.jfinal.util.StringKit;/** * JspRender. */@SuppressWarnings( { "serial", "rawtypes", "unchecked" })class JspRender extends Render { private transient static boolean isSupportActiveRecord = true; static void setSupportActiveRecord(boolean supportActiveRecord) { JspRender.isSupportActiveRecord = supportActiveRecord; } public JspRender(String view) { this.view = view; } public void render() { try { if (isSupportActiveRecord) supportActiveRecord(request); request.getRequestDispatcher(view).forward(request, response); } catch (Exception e) { throw new RenderException(e); } } private void supportActiveRecord(HttpServletRequest request) { for (Enumeration<String> attrs = request.getAttributeNames(); attrs .hasMoreElements();) { String key = attrs.nextElement(); Object value = request.getAttribute(key); if (value instanceof Model) { request.setAttribute(key, handleModel((Model) value)); } else if (value instanceof Record) { request.setAttribute(key, handleRecord((Record) value)); } else if (value instanceof List) { request.setAttribute(key, handleList((List) value)); } else if (value instanceof Model[]) { request.setAttribute(key, handleModelArray((Model[]) value)); } else if (value instanceof Record[]) { request.setAttribute(key, handleRecordArray((Record[]) value)); } else if (value instanceof Page) { request.setAttribute(key, handlePage((Page) value)); }else if (value instanceof Map) { request.setAttribute(key, iteratorMap((Map)value)); } } } private Map<String, Object> handleModel(Model model) { return CPI.getAttrs(model); } private Map<String, Object> handleRecord(Record record) { return record.getColumns(); } private List<Map<String, Object>> handleModelList(List<Model> list) { List<Map<String, Object>> result = new ArrayList<Map<String, Object>>( list.size()); for (Model model : list) result.add(CPI.getAttrs(model)); return result; } private List<Map<String, Object>> handleRecordList(List<Record> list) { List<Map<String, Object>> result = new ArrayList<Map<String, Object>>( list.size()); for (Record record : list) result.add(record.getColumns()); return result; } private List<Map<String, Object>> handleModelArray(Model[] array) { List<Map<String, Object>> result = new ArrayList<Map<String, Object>>( array.length); for (Model model : array) result.add(CPI.getAttrs(model)); return result; } private List<Map<String, Object>> handleRecordArray(Record[] array) { List<Map<String, Object>> result = new ArrayList<Map<String, Object>>( array.length); for (Record record : array) result.add(record.getColumns()); return result; } //-----------------------------add by laojianhua---------------------------------------------- private List<Map<String, Object>> handleList(List value){ List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); List temp = (List) value; if (temp.size() > 0) { Object o = temp.get(0); if (o instanceof Model) result = handleModelList((List<Model>) value); else if (o instanceof Record) result = handleRecordList((List<Record>) value); else if (o instanceof Map) result = handleMapList((List<Map<String, Object>>) value); else{ for(int i=0;i<value.size();i++){ Object tempList = value.get(i); result.add(iteratorObject(tempList)); } } } return result; } private List<Map<String, Object>> handleMapList(List<Map<String, Object>> list) { List<Map<String, Object>> result = new ArrayList<Map<String, Object>>( list.size()); for (Map<String, Object> map : list) { for (Entry<String, Object> e : map.entrySet()) { if (e.getValue() instanceof Model) { map.put(e.getKey(), CPI.getAttrs((Model) e.getValue())); }else{ map.put(e.getKey(), e.getValue()); } } result.add(map); } return result; } private Map<String, Object> handlePage(Page Object) { return iteratorObject(Object); } // 遍历实体对像 private Map<String, Object> iteratorObject(Object o) { Map<String, Object> map = new HashMap<String, Object>(); Method[] sourceMethods = o.getClass().getMethods(); for (int i = 0; i < sourceMethods.length; i++) { if (sourceMethods[i].getName().startsWith("get")) { String typeString = sourceMethods[i].getReturnType().getName(); // 类型 int index = typeString.lastIndexOf(".") + 1; String type = typeString.substring(index); String packageStr = ""; if(index == 0){ packageStr = "java.lang"; }else{ packageStr = typeString.substring(0,index-1); } String filed = StringKit.firstCharToLowerCase(sourceMethods[i].getName().substring(3)); // 属性 try { Object value; if (sourceMethods[i].invoke(o, null) == null) { value = ""; } else { value = sourceMethods[i].invoke(o, null); } if(value instanceof Model){ map.put(filed, handleModel((Model) value)); }else if(value instanceof List){ map.put(filed,handleList((List)value)); }else if(value instanceof Map){ map.put(filed,iteratorMap((Map)value)); }else if(packageStr.equals("java.lang")||packageStr.equals("java.util")||packageStr.equals("java.math")){ map.put(filed, value); }else{ map.put(filed, iteratorObject(value)); } // System.out.println(type + " " + filed + "=" + value); } catch (Exception e) { e.printStackTrace(); } } } return map; } // 遍历Map对像 private Map<String, Object> iteratorMap(Map map) { Map<String, Object> result = new HashMap<String, Object>(); Set set = map.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Map.Entry mapentry = (Map.Entry) iterator.next(); String objectType = mapentry.getValue().getClass().toString(); int lastIndex = objectType.lastIndexOf("."); String type = objectType.substring(lastIndex + 1, objectType.length()); String packageStr = objectType.substring(0,lastIndex); if (packageStr.equals("java.lang")||packageStr.equals("java.util")||packageStr.equals("java.math")) { if(type.equals("Map")){ result.put(mapentry.getKey().toString(),iteratorMap((Map)mapentry.getValue())); }else if(type.equals("List")){ map.put(mapentry.getKey().toString(),handleList((List)mapentry.getValue())); }else{ result.put(mapentry.getKey().toString(), mapentry.getValue()); } //System.out.println(mapentry.getKey() + " = " + mapentry.getValue() + " " + mapentry.getValue().getClass()); } else { //自定义对像 result.put(mapentry.getKey().toString(),iteratorObject(mapentry.getValue())); //System.out.println("键为:" + mapentry.getKey() + " ;对像为:"+ mapentry.getValue().getClass()); } } return result; }}//该片段来自于http://byrx.net
用户点评