- 浏览: 252357 次
- 性别:
- 来自: 苏州
文章分类
最新评论
-
a542550187:
很受用,最近正好学习软件工程方面的知识
如何建立领域模型(转) -
liiyee:
谢谢!中文版有些地方的翻译实在是挺对付的。
hibernate in action 2 英文版 -
HFLdragon:
下来学习一下
ajax upload file -
wendellup_account1:
thanks....
Spring 3 mvc Validation的错误 -
zhangjq5:
中文名乱码……
ajax upload file
<本篇文章是与从同事的博客上拿来学习,代码做一点我的.呵....>
Jmesa中处理Map的List对象
目前需求, 客户上传excel文件, 有列名, 列名不固定, 想预览数据, 使用Jmesa做table, 有两种实现方法. 第一种使用动态类, 封装map对象. 第二种是一种巧妙的方法. 下面先介绍第一种方法:
使用动态类:
下面第二种:
页面为:
这里注意property中的值.
今天查看源码, 和昨天想象一样, jmesa在渲染单元格的时候, 分两种, 一种就是map类型, 使用get(key)来取值, 另一种就是普通的javabean对象, 使用getPropertyName()取值渲染.
Jmesa中处理Map的List对象
目前需求, 客户上传excel文件, 有列名, 列名不固定, 想预览数据, 使用Jmesa做table, 有两种实现方法. 第一种使用动态类, 封装map对象. 第二种是一种巧妙的方法. 下面先介绍第一种方法:
使用动态类:
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<Map<String,String>> sdfList = (List<Map<String, String>>)sdfMap.get("result"); String[] keys = (String[]) sdfMap.get("fields"); //set fields to dynamic class DynaClass structureClass = createBasicDynaClass(keys); //set property to dynamic object. List<DynaBean> results = new ArrayList<DynaBean>(); for (Map<String, String> 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; } }
下面第二种:
@RequestMapping public String bookslist(final HttpServletRequest request, HttpServletResponse response, ModelMap model){ List<Map<String, String>> books = new ArrayList<Map<String, String>>(); Map<String, String> book1 = new HashMap<String, String>(); book1.put("id", "1"); book1.put("name", "Spring"); book1.put("price", "18.29"); books.add(book1); Map<String, String> book2 = new HashMap<String, String>(); book2.put("id", "2"); book2.put("name", "Hibernate"); book2.put("price", "28.98"); books.add(book2); Map<String, String> book3 = new HashMap<String, String>(); book3.put("id", "3"); book3.put("name", "Python"); book3.put("price", "38.22"); books.add(book3); model.addAttribute("books", books); return "booklist"; }
页面为:
<jmesa:tableFacade id="booksTable" items="${books}" maxRows="10" maxRowsIncrements="10,20,30" var="book"> <jmesa:htmlTable width="630px"> <jmesa:htmlRow> <c:forEach items="${book}" var="b"> <jmesa:htmlColumn property="${b.key}" title="${b.key}" filterable="false"/> </c:forEach> </jmesa:htmlRow> </jmesa:htmlTable> </jmesa:tableFacade>
这里注意property中的值.
今天查看源码, 和昨天想象一样, jmesa在渲染单元格的时候, 分两种, 一种就是map类型, 使用get(key)来取值, 另一种就是普通的javabean对象, 使用getPropertyName()取值渲染.
/* * 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); } }
发表评论
-
web页面打印
2012-04-26 14:25 700Web页面打印: [code="html&q ... -
JQuery--点击返回页面顶部
2012-04-12 14:07 882function goTop() { $('html, ... -
jQuery 参考
2011-08-18 14:48 54950个jQuery代码段帮你成为更好的JavaScript开发 ... -
使用Jquery做的滚动新闻
2010-06-03 11:24 1832滚动新闻代码从是网上找的,自己做了一些修改,特别是新闻比较长时 ... -
使用CSS处理文本溢出时显示省略号
2010-06-03 11:17 1671在多数情况下要显示文 ... -
ajax upload file
2010-06-03 09:18 6976AJAX Uplad File 是个简单而美观的上传文件插件 ... -
jquery plugin autocomplete
2010-04-28 17:50 628http://bassistance.de/jquery-pl ... -
jquery取得iframe元素的方法
2009-12-19 22:42 23325收集利用Jquery取得iframe中元素的几种方法 : ... -
jQuery.typeof and jQuery.isEmpty Utilities
2009-05-15 15:15 2446One of the best aspects of jQ ... -
ajax请求时的缓存的问题
2009-05-05 17:35 934ajax请求同一个url地址时,如果发现url地址没有变而缓存 ... -
checkbox disable时取值方法
2009-04-23 10:01 3277在使用checkbox时,我们有时间不想让用户勾选,但又需要显 ... -
jQuery Form Plugin
2009-04-22 14:29 2051Form Plugin API http://mals ... -
Jquery的表单插件
2009-04-03 13:30 63821. 本篇文章是从我网上搜集整理的有关Jquery Form的 ... -
jQuery中对异步提交JSON和XML数据的处理方式
2009-03-23 23:22 2519最近在项目中用到Jquery,感觉真的不错,开源的插件也比 ...
相关推荐
List<Bean>转换为List<Map>, List<Object>转换为List<Object> list转化为 JsonArray ,list转换JsonArray
一般使用springjdbc、hibernate的sql查询,库获取到的数据都是List<Map<String, Object>>结果集,如果我们要转化为JavaBean,则需要做一系列的map.get(),然后obj.set()。 此工程中就是解决List<Map<String, Object>...
将list<Map>转换成JavaBean的工具类
spring,cxf,restful发布webservice传递复杂对象,例如List,Map,List<Map>
支持一个List<Map>按照MAP中的一个或者多个Key的value值的中英文来排序,自动识别字符和数字(包括[a-zA-z]?[0-9]*)排序
List<Long>转一维数组 Long[](csdn)————程序
基于新版本的POI编写的读取Excel文件数据的工具类,可根据绝对路径、File对象、InputSteam对象读取解析Excel文件内容,并返回List<List<String>>格式结果,其中包含对单元格公式的处理。
可将list<T>转化成JSON字符串 使用方法 例: List<自定义类> l; ListChangeToJson.ListChangeToJson classJson = new ListChangeToJson.ListChangeToJson(); classJson.ArrayToJsonAll(l, "数组名字");
List<map>,List<Map<String, Object>>,多字段组合排序。提供一个简易的思路,如果需要进行参考。
javascript 模拟 java中的 List,Map<br>js文件为 js/utils.js<br>IE6.0 测试通过<br><br>List:<br>add(var obj) //添加一个元素<br>remove(var index) //删除一个元素<br>get(var index) //获取一个元素<br>remove...
List<Map<String, List<String>>> data保存到SharedPreference和读取
拆分list,将list<bean>中的内容根据某一属性的值对list进行拆分,分组
<P>map集合转化成xml字符串</P> <P>xml字符串转化成map集合</P> <P>list集合转化成xml字符串</P> <P>xml字符串转化成list集合</P>
List<Object>转Json的jar包
C#+List<T>+GridControl实现主从表嵌套
7_4.有集合A={1,2,3,4}和B={1,3,7,9,11},编写一个应用程序输出A和B交集、并集和差集...编写一个应用程序,要求将LinkedList<E>创建的对象写入到文件,然后读出一个LinkedList<E>对象,并遍历LinkedList<E>节点中的数据
事情: 从java后台返回List<String>类型数据,用于界面显示。但js中想获取它并操作它。直接使用EL表达式,js把它识别成字符串了。不是我想要的啊。。网上搜了搜大家的解决方案…最好的当然是把List集合转成json格式...
一、List<T>对象中的T是值类型的情况(int 类型等) 对于值类型的List直接用以下方法就可以复制: List<T> oldList = new List<T>(); oldList.Add(..); List<T> newList = new List<T>(oldList); 二、List<T>对象...
使用两个list<Map<String,String>> 求交集和补集。
List集合转换成String,String转List,Map转String,String转Map等 集合与字符串相互转换,可以自己扩展源码,带有注释