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

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條評論
主站蜘蛛池模板: 99在线观看免费 | 狠狠se | 97色在线视频 | 99re这里只有精品国产精品 | 亚洲免费高清视频 | 国产精彩视频在线 | 日韩经典欧美一区二区三区 | 久久久久亚洲香蕉网 | 国产一国产一级毛片视频在线 | 欧美色老头oldvideos | 国产一区二区三区免费看 | 国产成人啪午夜精品网站男同 | 中文字幕一区二区三区亚洲精品 | 国产在线观看自拍 | 国产精品边做奶水狂喷小说 | 四虎影视国产在线观看精品 | 国产亚洲人成a在线v网站 | 99热播 | 成人国产亚洲欧美成人综合网 | 日韩精品午夜视频一区二区三区 | 亚洲欧美日韩国产专区一区 | 97精品视频在线 | 国产69精品久久久久9999 | 4hu在线| 精品国产免费一区二区三区五区 | 卡通动漫亚洲综合 | 97影院理论 | 亚洲国产精品日韩高清秒播 | 亚洲精品久久久成人 | 五月天色区 | 成年女人毛片免费视频 | 青草91视频免费观看 | 美国一级毛片片免费 | 另类亚洲图片 | 在线欧美视频 | 在线高清一级欧美精品 | heyzo在线播放4k岛国 | 欧美日韩一卡二卡 | 久久精品男人影院 | 国产精品日韩欧美在线第3页 | 99精品视频免费在线观看 |