ITeye博客

利用Jmesa处理List<Map>方法

yaofeng911 · 2009-04-11 16:03 · JQuery · Apache · Spring · Python · Bean · Excel · Markdown

<本篇文章是与从同事的博客上拿来学习,代码做一点我的.呵....> Jmesa中处理Map的List对象 目前需求, 客户上传excel文件, 有列名, 列名不固定, 想预览数据, 使用Jmesa做table, 有两种实现方法. 第一种使用动态类, 封装map对象. 第二种是一种巧妙的方法. 下面先介绍第一种方法: 使用动态类: [code="java"] package com.founder.cst.action; import Java.util.ArrayList; import Java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BasicDynaBean; import org.apache.commons.beanutils.BasicDynaClass; import org.apache.commons.beanutils.DynaBean; import org.apache.commons.beanutils.DynaClass; import org.apache.commons.beanutils.DynaProperty; import org.jmesa.core.filter.MatcherKey; import org.jmesa.facade.TableFacade; import org.jmesa.facade.TableFacadeFactory; import org.jmesa.view.html.component.HtmlTable; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import com.founder.cst.common.StringFilterMatcher; @Controller @RequestMapping public class DynaController { @RequestMapping public String books(final HttpServletRequest request, HttpServletResponse response, ModelMap model){ DynaClass bookClass = createBasicDynaClass(); try { //get parsed result List> sdfList = (List>)sdfMap.get("result"); String[] keys = (String[]) sdfMap.get("fields"); //set fields to dynamic class DynaClass structureClass = createBasicDynaClass(keys); //set property to dynamic object. List results = new ArrayList(); for (Map structure : sdfList) { DynaBean stru = structureClass.newInstance(); for (String key : keys) { stru.set(key, structure.get(key)); } results.add(stru); } TableFacade tableFacade = TableFacadeFactory.createTableFacade("booksTable", request); tableFacade.setColumnProperties("id", "name", "price"); tableFacade.setMaxRows(10); tableFacade.setMaxRowsIncrements(10, 20, 30); tableFacade.setItems(results); HtmlTable table = (HtmlTable) tableFacade.getTable(); table.getTableRenderer().setWidth("558px"); table.getRow().setUniqueProperty("id"); String html = tableFacade.render(); model.addAttribute("html", html); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "books"; } private DynaClass createBasicDynaClass(String[] keys) { DynaClass dynaClass = null; //create basic field for dynaClass int columnSize = keys.length; DynaProperty[] dynaProps = new DynaProperty[columnSize]; for (int i = 0; i < columnSize; i++) { dynaProps[i] = new DynaProperty(keys[i], String.class); } //create map filed for dynaClass dynaClass = new BasicDynaClass("Structures", BasicDynaBean.class, dynaProps); return dynaClass; } } [/code] 下面第二种: [code="java"] @RequestMapping public String bookslist(final HttpServletRequest request, HttpServletResponse response, ModelMap model){ List> books = new ArrayList>(); Map book1 = new HashMap(); book1.put("id", "1"); book1.put("name", "Spring"); book1.put("price", "18.29"); books.add(book1); Map book2 = new HashMap(); book2.put("id", "2"); book2.put("name", "Hibernate"); book2.put("price", "28.98"); books.add(book2); Map book3 = new HashMap(); book3.put("id", "3"); book3.put("name", "Python"); book3.put("price", "38.22"); books.add(book3); model.addAttribute("books", books); return "booklist"; } [/code] 页面为: [code="html"] [/code] 这里注意property中的值. 今天查看源码, 和昨天想象一样, jmesa在渲染单元格的时候, 分两种, 一种就是map类型, 使用get(key)来取值, 另一种就是普通的javabean对象, 使用getPropertyName()取值渲染. [code="java"] /* * Copyright 2004 original author or authors. * * 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 org.jmesa.util; import Java.util.Collection; import Java.util.Map; import org.apache.commons.beanutils.PropertyUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * General utilities to process the Collecton of Beans or the Collection of * Maps. Most methods wrap or add value to the commons Beanutils. * * @since 2.1 * @author Jeff Johnston */ public class ItemUtils { private static final Logger logger = LoggerFactory.getLogger(ItemUtils.class); public static final String JMESA_ITEM = "jmesa-item"; private ItemUtils() { // hide constructor } /** * Get the value from the Bean or Map by property. * * @param item The Bean or Map. * @param property The Bean attribute or Map key. * @return The value from the Bean or Map. */ public static Object getItemValue(Object item, String property) { Object itemValue = null; try { if (item instanceof Map) { itemValue = ((Map) item).get(property); if (itemValue != null) { return itemValue; } // ports such as the tags will store the original bean Object bean = ((Map) item).get(JMESA_ITEM); if (bean == null) { logger.debug("the map does not have property " + property); return null; } itemValue = getItemValue(bean, property); } else { itemValue = PropertyUtils.getProperty(item, property); } } catch (Exception e) { logger.debug("item class " + item.getClass().getName() + " does not have property " + property); } return itemValue; } /** * Get the Class for the property. * * @param items The Collection of Beans or Maps. * @param property The Bean attribute or Map key. * @return The Class for the property. */ public static Class getPropertyClassType(Collection items, String property) throws Exception { Object item = items.iterator().next(); if (item instanceof Map) { for (Object object : items) { Map map = (Map) object; Object val = map.get(property); if (val == null) { continue; } return val.getClass(); } } return PropertyUtils.getPropertyType(item, property); } } [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。