velocity官方的examples运行报各种错误
1. app_example1 是一个通过velocity.properties初始华velocity
import org.apache.velocity.app.Velocity; import org.apache.velocity.VelocityContext; import org.apache.velocity.Template; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.ResourceNotFoundException; import java.io.*; import java.util.ArrayList; public class Example { public Example(String templateFile) { try { /* * setup */ Velocity.init("velocity.properties"); VelocityContext context = new VelocityContext(); context.put("list", getNames()); Template template = null; try { template = Velocity.getTemplate(templateFile); } catch( ResourceNotFoundException rnfe ) { System.out.println("Example : error : cannot find template " + templateFile ); } catch( ParseErrorException pee ) { System.out.println("Example : Syntax error in template " + templateFile + ":" + pee ); } BufferedWriter writer = writer = new BufferedWriter( new OutputStreamWriter(System.out)); if ( template != null) template.merge(context, writer); writer.flush(); writer.close(); } catch( Exception e ) { System.out.println(e); } } public ArrayList getNames() { ArrayList list = new ArrayList(); list.add("ArrayList element 1"); list.add("ArrayList element 2"); list.add("ArrayList element 3"); list.add("ArrayList element 4"); return list; } public static void main(String[] args) { Example t = new Example("example.vm"); } }
运行的错误信息:org.apache.velocity.exception.VelocityException: Error reading properties from 'velocity.properties'
这个问题我解决不了,也不知道是什么问题.
问怎么才能加载velocity.properties
2.app_example2
运行错误信息: org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'example2.vm'
import java.io.StringWriter; import java.util.Properties; import org.apache.velocity.app.Velocity; import org.apache.velocity.VelocityContext; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.MethodInvocationException; public class Example2 { public static void main( String args[] ) { /* first, we init the runtime engine. Defaults are fine. */ try { Velocity.init(); } catch(Exception e) { System.out.println("Problem initializing Velocity : " + e ); return; } /* lets make a Context and put data into it */ VelocityContext context = new VelocityContext(); context.put("name", "Velocity"); context.put("project", "Jakarta"); /* lets render a template */ StringWriter w = new StringWriter(); try { Velocity.mergeTemplate("example2.vm", "ISO-8859-1", context, w ); } catch (Exception e ) { System.out.println("Problem merging template : " + e ); } System.out.println(" template : " + w ); String s = "We are using $project $name to render this."; w = new StringWriter(); try { Velocity.evaluate( context, w, "mystring", s ); } catch( ParseErrorException pee ) { System.out.println("ParseErrorException : " + pee ); } catch( MethodInvocationException mee ) { System.out.println("MethodInvocationException : " + mee ); } catch( Exception e ) { System.out.println("Exception : " + e ); } System.out.println(" string : " + w ); } }
这个问题自己能解决
Properties p=new Properties(); p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); Velocity.init(p);
问默认的file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FilesourceLoader 的模板怎么加载
召唤@红薯
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
路径改成这样: ./src/velocity.properties
FileResourceLoader 装载器的 getTamplate("这里怎么写")
那为什么Properties p=new Properties(); p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); Velocity.init(p); 这里能仿问到啊
vm文件我是和.class文件放一起的
很明显吗 路径不对 找不到
路径改成这样: ./src/velocity.properties