`

velocity使用中出现org.apache.velocity.exception.ResourceNotFoundException的解决办法

阅读更多
在使用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源码:
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 下
  • 大小: 52.3 KB
分享到:
评论

相关推荐

    Liferay issue ResourceNotFoundException 'init.vm' 解决办法

    org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource '_SERVLET_CONTEXT_/html/themes/_unstyled/templates/init.vm' 上 Liferay 官网查了下,找到一个解决办法。

    Java的模板引擎Velocity初体验.pdf

    Velocity 的最新版本是 1.4,可以在 http://jakarta.apache.org/velocity/index.html 查找更多信息。 Velocity 的优点是它可以帮助开发人员更好地实现 MVC 架构,特别是在 Model 和 View 之间的交互中。Velocity ...

    使用velocity

    1. **下载Velocity库**:访问Velocity工程首页(http://velocity.apache.org)下载最新版本的Velocity库。 2. **创建项目**:使用IDE如Eclipse创建一个新的Java项目。 3. **导入库**:将下载的Velocity库(*.jar文件...

    itext加载velocity生成PDF

    <groupId>org.apache.velocity <artifactId>velocity <version>1.x.x</version> <!-- 使用合适的版本 --> ``` 然后,创建一个Velocity模板文件(例如:`template.vm`),在其中定义PDF的布局和内容。例如,你...

    velocity 实例

    这个例子展示了如何在Java应用程序中使用Velocity。首先,你需要下载velocity.zip并将其lib目录下的.jar文件导入到你的项目中,包括velocity.jar和velocity-dep.jar。然后创建一个名为"hellosite.vm"的模板文件,...

    Velocity之HelloWorld配置

    `VelocityContext`则用于存储模板中使用的变量。当你查看这些类的源码时,可以了解其内部工作原理,这对理解和优化 Velocity 应用非常有帮助。 **工具支持** 在开发过程中,有一些工具可以帮助你更好地管理Velocity...

    Velocity模板邮件发送例子(Spring框架)(可通用,支持多扩展)

    <prop key="velocityEngine.logger">org.apache.velocity.runtime.log.Slf4JLogChute <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> ...

    struts1 用户登录(包含验证)

    DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <form-bean name="loginForm" type="com...

    java 写入 excle、pdf、word资料

    在“自动赋值”的场景下,你可以使用Java的反射、模板引擎(如FreeMarker或Velocity)或者JavaFX等工具,根据动态数据来自动生成这些文件。例如,如果你有一个对象列表,可以遍历这个列表并自动将数据填充到相应的...

    struts2.x所有包及配置文件

    Struts2是一个基于MVC(Model... - 将这些jar包添加到项目的类路径中,并在web.xml中配置Struts2的过滤器`org.apache.struts2.dispatcher.FilterDispatcher`(在较新版本中是`org.apache.struts2.dispatcher.ng.filter....

    Struts2 环境的搭建

    "http://struts.apache.org/dtds/struts-2.5.dtd"> <action name="hello" class="com.example.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp ``` 在这个例子中,我们定义...

    commons-email-1.1.jar/mail.jar/activation.jar

    在Java开发中,电子邮件的发送是一项常见的任务,而Apache Commons Email库正是为了简化这一过程而设计的。`commons-email-1.1.jar`是Apache Commons Email项目的早期版本,它提供了一系列API来帮助开发者构建和发送...

    Java操作word模板读写

    在Java操作Word模板的实践中,还可以考虑使用FreeMarker或者Velocity这样的模板引擎,它们允许在模板文件中使用变量和控制结构,然后在运行时填充这些变量,生成最终的Word文档。这种方式更适合批量生成文档或动态...

    Struts快速学习指南

    Struts是Apache软件基金会下的一个开源项目,是一个基于MVC(Model-View-Controller)设计模式的Java Web框架。它使得开发人员能够更方便地构建动态、数据驱动的Web应用程序。Struts通过提供一系列的控制器组件和可...

    Apache click 中文文档

    ### Apache Click 中文文档知识点概览 #### 一、Apache Click 概述 Apache Click 是一个基于 Java 的 Web 应用框架,它提供了一种简洁高效的方式来开发 Web 应用程序。该框架的设计目的是简化 Web 开发过程,使得...

    基于Java的邮件发送模板

    import org.apache.commons.mail.*; Email email = new HtmlEmail(); email.setHostName("smtp.example.com"); email.setSmtpPort(587); email.setAuthenticator(new DefaultAuthenticator("your-email@example....

    Action in struts中文版

    Struts是Apache软件基金会旗下的一款开源Java框架,主要用于构建企业级Web应用程序。它基于Model-View-Controller(MVC)设计模式,旨在简化开发过程,提高代码的可维护性和可扩展性。"Action in Struts"是Struts...

    Struts2.1+Spring3.0+JPA1.0 .pdf

    "http://struts.apache.org/dtds/struts-2.0.dtd"> <constant name="struts.devMode" value="true"/> <result>/hello.jsp ``` 其中,`struts.devMode`用于指定是否启用开发模式。如果设置为`true`...

    自定义mystruts

    你可以创建自己的Action类,继承自`org.apache.struts2.dispatcher.ng.ExecuteAction`或`com.opensymphony.xwork2.ActionSupport`。通过覆盖`execute()`方法来实现业务逻辑,并通过返回不同的常量(如SUCCESS、ERROR...

Global site tag (gtag.js) - Google Analytics