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

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

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

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

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

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


    解決方法:
    在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)限使用目錄,可由配置文件定義。

    問(wèn)題分析:

    1、2點(diǎn)不用說(shuō)了,說(shuō)說(shuō)第3點(diǎn)。一開始我在調(diào)用模板時(shí)使用絕對(duì)路徑和相對(duì)路徑,可是怎么測(cè)試就是不行,總是報(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 );
        }


    可見(jiàn)你一定要設(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
    主站蜘蛛池模板: 国产成人va亚洲电影| sihu国产精品永久免费| 国产网站免费观看| 国产精品综合专区中文字幕免费播放| 在线观看亚洲精品福利片| 亚洲免费在线视频| 亚洲人成网站色7799| 国产亚洲无线码一区二区 | a视频免费在线观看| 久久国产亚洲精品无码| 全免费a级毛片免费**视频| a毛片免费播放全部完整| 2020天堂在线亚洲精品专区| 免费吃奶摸下激烈视频| 1000部啪啪未满十八勿入免费| WWW亚洲色大成网络.COM| 亚洲AV成人一区二区三区AV| 国产无遮挡吃胸膜奶免费看视频| 在线观看片免费人成视频播放| 亚洲AV无码一区二区三区在线| 2048亚洲精品国产| 999久久久免费精品国产| 国产精品免费αv视频| 亚洲中文无码卡通动漫野外| 亚洲av永久无码精品网站| 日本高清免费不卡在线| 亚州免费一级毛片| 国产日韩AV免费无码一区二区| 亚洲人av高清无码| 亚洲经典在线观看| 国产精品亚洲一区二区三区在线| 国产精品麻豆免费版| 午夜免费1000部| 免费无码一区二区三区蜜桃| 国产亚洲精品精品精品| 亚洲av极品无码专区在线观看| 亚洲人成人网站色www| 国产一区二区三区免费视频| 丁香花免费高清视频完整版| 无码国产精品一区二区免费vr| 一个人免费观看www视频|