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

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大香线蕉 | 四虎国产精品一区二区 | 国产精品久久久久影院色 | 天天干天天拍天天射 | 久久高清免费 | 外国成人网在线观看免费视频 | 日日操日日射 | 日本中文一二区有码在线观看 | 欧美在线日韩在线 | 成人网视频在线观看免费 | 国产精品福利一区二区久久 | 四虎永久免费影院 | 日韩精品一区二区三区视频 | 国产精品国产自线在线观看 | 亚洲国产成人成上人色 | 免费人成黄页网站在线观看 | 美女被羞羞的网站 | 操日日| 久久亚洲人成国产精品 | 奇米第四色在线观看 | 精品色综合 | 国产精品一区二区四区 | 亚洲欧美成人中文在线网站 | 欧美成人一区二区三区 | 亚洲欧美日韩高清一区二区三区 | 欧美成人交tv免费观看 | 久久影片| 香蕉久久成人网 | 亚州综合 | 四虎影视网址 | 国产精品日韩一区二区三区 | 久久国产三级 | 免费观看成人羞羞视频网站观看 | h片在线看 | 久久久99精品久久久久久 | 国产亚洲综合在线 | 久久久免费 | 国产精品久久久久久久免费 | 亚洲精品国产第一综合99久久 | 色婷婷久久综合中文久久一本` |