近來做了一個(gè)簡單的信息發(fā)布模塊,功能如下:
          l       
          信息顯示
          l       
          信息操作
            n       
          新增
            n       
          修改
            n       
          刪除
          l       
          信息查詢
          l       
          模板管理
            n       
          下載
            n        上傳

          基本思路:
                   在信息新增時(shí)對(duì)信息進(jìn)行兩個(gè)方面的保存:1、保存到數(shù)據(jù)庫,目的便于信息的修改;2、使用velocity生成靜態(tài)的html文件,以便瀏覽。用戶可以下載velocity的“.vm”模板,由程序轉(zhuǎn)變成“.html”文件給用戶,用戶修改(可以加上css修飾)后將“.html”文件上傳,由程序轉(zhuǎn)變成“.vm”文件放到velocity調(diào)用的模板目錄中。

          遇到問題:
          1、log文件生成問題;
          2、編碼問題;
          3、模板路徑問題;


          解決方法:
          在velocity初始化時(shí),添加以下屬性配置:


            // 設(shè)置velocity的log
            Velocity.setProperty(Velocity.RUNTIME_LOG, mainPath + File.separator + "velocity.log");

            // 設(shè)置velocity的輸入輸出編碼
            Velocity.setProperty(Velocity.INPUT_ENCODING, "GBK");
            Velocity.setProperty(Velocity.OUTPUT_ENCODING, "GBK");

            // / 設(shè)置velocity的模板路徑(必要)
            Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, mainPath + File.separator + "template";

            // 初始化velocity引擎
            try {
             Velocity.init();
            } catch (Exception e) {   
             e.printStackTrace();
            }


          其中mainPath 指你的應(yīng)用發(fā)布目錄或其他任何一個(gè)有權(quán)限使用目錄,可由配置文件定義。

          問題分析:

          1、2點(diǎn)不用說了,說說第3點(diǎn)。一開始我在調(diào)用模板時(shí)使用絕對(duì)路徑和相對(duì)路徑,可是怎么測試就是不行,總是報(bào):Unable to find resource。查看了一下源碼,velocity調(diào)用的是FileResourceLoader,部分源碼如下:


          public void init( ExtendedProperties configuration)
              {
                  rsvc.info("FileResourceLoader : initialization starting.");
                 
                  paths = configuration.getVector("path");
                 
                  /*
                   *  lets tell people what paths we will be using
                   */

                  int sz = paths.size();

                  for( int i=0; i < sz; i++)
                  {
                      rsvc.info("FileResourceLoader : adding path '" + (String) paths.get(i) + "'");
                  }

                  rsvc.info("FileResourceLoader : initialization complete.");
              }

              /**
               * Get an InputStream so that the Runtime can build a
               * template with it.
               *
               * @param name name of template to get
               * @return InputStream containing the template
               * @throws ResourceNotFoundException if template not found
               *         in the file template path.
               */
              public synchronized InputStream getResourceStream(String templateName)
                  throws ResourceNotFoundException
              {
                  /*
                   * Make sure we have a valid templateName.
                   */
                  if (templateName == null || templateName.length() == 0)
                  {
                      /*
                       * If we don't get a properly formed templateName then
                       * there's not much we can do. So we'll forget about
                       * trying to search any more paths for the template.
                       */
                      throw new ResourceNotFoundException(
                          "Need to specify a file name or file path!");
                  }

                  String template = StringUtils.normalizePath(templateName);
                  if ( template == null || template.length() == 0 )
                  {
                      String msg = "File resource error : argument " + template +
                          " contains .. and may be trying to access " +
                          "content outside of template root.  Rejected.";

                      rsvc.error( "FileResourceLoader : " + msg );
               
                      throw new ResourceNotFoundException ( msg );
                  }

                  /*
                   *  if a / leads off, then just nip that :)
                   */
                  if (template.startsWith("/"))
                  {
                      template = template.substring(1);
                  }

                  int size = paths.size();
                  for (int i = 0; i < size; i++)
                  {
                      String path = (String) paths.get(i);
                      InputStream inputStream = findTemplate(path, template);
                     
                      if (inputStream != null)
                      {
                          /*
                           * Store the path that this template came
                           * from so that we can check its modification
                           * time.
                           */

                          templatePaths.put(templateName, path);
                          return inputStream;
                      }               
                  }
             
                  /*
                   * We have now searched all the paths for
                   * templates and we didn't find anything so
                   * throw an exception.
                   */
                   String msg = "FileResourceLoader Error: cannot find resource " +
                    template;
             
                   throw new ResourceNotFoundException( msg );
              }


          可見你一定要設(shè)置


          // / 設(shè)置velocity的模板路徑(必要)
            Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, mainPath + File.separator + "template";



          posted on 2006-03-08 21:09 野草 閱讀(1139) 評(píng)論(0)  編輯  收藏 所屬分類: 2shtv
          主站蜘蛛池模板: 义乌市| 南城县| 唐河县| 类乌齐县| 宁南县| 宽甸| 商洛市| 白河县| 濮阳市| 兴隆县| 盈江县| 青阳县| 伊宁县| 合肥市| 宜兰市| 武胜县| 沭阳县| 嵩明县| 辽宁省| 兴宁市| 合作市| 洮南市| 义乌市| 石棉县| 化隆| 呼和浩特市| 鹤山市| 当雄县| 吴旗县| 宝鸡市| 荣昌县| 卓尼县| 福贡县| 易门县| 洞口县| 六安市| 轮台县| 云阳县| 漠河县| 河西区| 垦利县|