亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

velocity使用中出現org.apache.velocity.except

系統 2436 0
在使用velocity過程中,總是出現.ResourceNotFoundException異常,找不到模板.vm文件。現公布解決辦法:
    
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import java.io.StringWriter;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
public class VelocityTest {

	public static void main(String[] args) {
		try {
			/* first, get and initialize an engine */
			VelocityEngine ve = new VelocityEngine();
			Properties properties = new Properties();
			String basePath = "src/com/worthtech/app/test";//這里需要這樣寫路徑!!!
			// 設置模板的路徑
			properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath);
			// 初始花velocity 讓設置的路徑生效
			ve.init(properties);
			/* next, get the Template */
			Template t = ve.getTemplate("vm.vm");
			/* create a context and add data */
			VelocityContext context = new VelocityContext();
			context.put("name", "DingDangXiaoMa");
			context.put("site", "http://www.sun.com");
			/* now render the template into a StringWriter */
			StringWriter writer = new StringWriter();
			t.merge(context, writer);
			/* show the World */
			System.out.println(writer.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

  



velocity使用中出現org.apache.velocity.exception.ResourceNotFoundException的解決辦法

研究velocity源碼:
    
public synchronized InputStream getResourceStream(String name)
    throws ResourceNotFoundException
  {
    InputStream result = null;

    if ((name == null) || (name.length() == 0))
    {
      throw new ResourceNotFoundException("No template name provided");
    }

    try
    {
      ClassLoader classLoader = super.getClass().getClassLoader();
      result = classLoader.getResourceAsStream(name);
    }
    catch (Exception fnfe)
    {
      throw new ResourceNotFoundException(fnfe.getMessage());
    }

    return result;
  }



  

關鍵是
ClassLoader classLoader = super.getClass().getClassLoader();
result = classLoader.getResourceAsStream(name);
這兩行代碼。是在你的Classpath下去load, 所以必須保證vm文件存在于WEB-INF/classes 下

velocity使用中出現org.apache.velocity.exception.ResourceNotFoundException的解決辦法


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久视频精品线视频在线网站 | 久久久精品免费热线观看 | 综合色综合 | 国产嘿咻| 欧美freesex10一|3 | 日韩精品视频一区二区三区 | 午夜免费福利社 | 一级骚片超级骚在线观看 | 国产日韩欧美在线观看免费视频 | 国产精品大全国产精品 | 免费观看一区二区 | 免费一级毛片在线播放视频 | 国产成人亚洲影视在线 | 奇米影视7777777 | 波多野结衣绝顶大高潮 | 一级毛片大全 | 婷婷综合网站 | 手机在线国产精品 | 久久综合香蕉 | 免费观看日本污污ww网站精选 | 天天天操天天天干 | 免费真实播放国产乱子伦 | 一区二区三区四区国产精品 | 欧美一区二区三区久久久 | 国产亚洲精品国产福利在线观看 | 九九热精 | 国产欧美一区二区精品仙草咪 | 99精品久久久久中文字幕 | 四虎四虎1515hhcom | 天天操天天摸天天射 | 国产精品无 | 亚洲和欧美毛片久久久久 | 在线成人aa在线看片 | 国产成人精品免费视频大全办公室 | 日本在线亚洲 | 黄视频网站免费看 | 亚洲视频在线一区 | 午夜在线网址 | 四虎影院.com | 欧美午夜性春猛交 | 成人午夜|