<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 431,  comments - 344,  trackbacks - 0

    目前需求, 客戶上傳excel文件, 有列名, 列名不固定, 想預(yù)覽數(shù)據(jù), 使用Jmesa做table, 有兩種實(shí)現(xiàn)方法. 第一種使用動態(tài)類, 封裝map對象. 第二種是一種巧妙的方法. 下面先介紹第一種方法:
    使用動態(tài)類:

    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 {
       List<DynaBean> results = new ArrayList<DynaBean>();
       DynaBean book11 = bookClass.newInstance();
       book11.set("id", "1");
       book11.set("name", "Spring");
       book11.set("price", "18.29");
       results.add(book11);
       DynaBean book22 = bookClass.newInstance();
       book22.set("id", "2");
       book22.set("name", "Hibernate");
       book22.set("price", "12.29");
       results.add(book22);
       DynaBean book33 = bookClass.newInstance();
       book33.set("id", "3");
       book33.set("name", "Python");
       book33.set("price", "17.32");
       results.add(book33);
       
       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";
     }
     
     public DynaClass createBasicDynaClass() {
      DynaClass dynaClass = null;
            //create basic field for dynaClass
            DynaProperty[] dynaProps = new DynaProperty[3];
            dynaProps[0] = new DynaProperty("id");
            dynaProps[1] = new DynaProperty("name", String.class);
            dynaProps[2] = new DynaProperty("price", String.class);
            //create map filed for dynaClass
            dynaClass = new BasicDynaClass("first", 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在渲染單元格的時(shí)候, 分兩種, 一種就是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);
        }
    }

    posted on 2009-03-19 17:43 周銳 閱讀(1309) 評論(1)  編輯  收藏 所屬分類: Ajax
    主站蜘蛛池模板: 99久久免费精品视频| A级毛片高清免费视频在线播放| 四虎在线最新永久免费| 亚洲图片一区二区| 免费无遮挡无码永久视频| 亚洲色图.com| 免费黄色网址网站| 亚洲一区二区三区成人网站| 午夜寂寞在线一级观看免费| 在线91精品亚洲网站精品成人| 日本最新免费不卡二区在线| 在线视频观看免费视频18| 亚洲中文字幕久久精品无码2021 | 一级做受视频免费是看美女| 亚洲?V乱码久久精品蜜桃 | 国产精品内射视频免费| 亚洲自偷自偷在线制服 | 大陆一级毛片免费视频观看| 亚洲国产成人久久精品大牛影视| 热99re久久精品精品免费| 免费的黄网站男人的天堂 | 精品久久久久久久久免费影院| 99热亚洲色精品国产88| 国产精品国产午夜免费福利看| 黄色免费网址大全| 国产AV无码专区亚洲精品| 噼里啪啦免费观看高清动漫4| 日本亚洲高清乱码中文在线观看 | 亚洲AV人无码综合在线观看| 性短视频在线观看免费不卡流畅| 亚洲乱妇老熟女爽到高潮的片 | 国产精品永久免费视频| 亚洲第一成年男人的天堂| 国产精品视频永久免费播放| 日亚毛片免费乱码不卡一区 | 国产国拍亚洲精品mv在线观看| 国产成人精品免费视频大全麻豆| 亚洲乱码中文字幕在线| 国产亚洲一区二区三区在线| 免费无码A片一区二三区| 韩国免费A级毛片久久|