velocity中模版不用文件是否可行 去看org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader的API文档。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 晕死了,APi文档里有DataSourceResourceLoader的详细介绍,居然jar包里没有....给个简单实现罢:package com.saro.custom.velocity;import org.apache.velocity.runtime.resource.loader.ResourceLoader;import org.apache.velocity.runtime.resource.Resource;import org.apache.velocity.exception.ResourceNotFoundException;import org.apache.commons.collections.ExtendedProperties;import java.io.InputStream;import java.io.ByteArrayInputStream;/** * User: Saro * Date: 2006-3-17 * Time: 19:02:29 */public class StringResourceLoader extends ResourceLoader { public void init( ExtendedProperties configuration){ } public synchronized InputStream getResourceStream( String templateString ) throws ResourceNotFoundException{ InputStream result = null; if (templateString == null || templateString.length() == 0) { throw new ResourceNotFoundException ("No template string provided"); } result=new ByteArrayInputStream(templateString.getBytes()); return result; } public boolean isSourceModified(Resource resource) { return false; } public long getLastModified(Resource resource) { return 0; }}然后,测试代码: public static void testVelocity() { try { //Velocity.init("WEB-INF/velocity.properties"); Properties p=new Properties(); p.put("input.encoding","UTF-8"); p.put("output.encoding","UTF-8"); p.put("resource.loader","srl"); p.put("srl.resource.loader.class","com.saro.custom.velocity.StringResourceLoader"); Velocity.init(p); VelocityContext context = new VelocityContext(); context.put("coolman", "Rodjohnson"); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("r.txt"),"UTF-8")); Template tt=Velocity.getTemplate("coolman is ${coolman}","UTF-8"); tt.merge(context,writer); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } 我用本地sql查询怎么不行啊?老报错 jar 运行 报错链接hibernate3属性文件错误。急。。。急。。求解 JavaScript问题 求Hibernate 自定义主键生成器 生成不重复的数字 hibernate多对多拆分成两个一对多的插入和更新问题 学习JBuilder+Weblogic 开发CORBA Hibernate+mysql 查询中文时出错 如何在JBoss下配置JMS? 能不能介绍一个好的Framework? 【求助】请帮忙分析一下这两套开发环境,越详细越好。谢谢! 请问domain object与pojo有什么区别吗? 跪求hibernate初级问题
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.commons.collections.ExtendedProperties;import java.io.InputStream;
import java.io.ByteArrayInputStream;/**
* User: Saro
* Date: 2006-3-17
* Time: 19:02:29
*/
public class StringResourceLoader extends ResourceLoader { public void init( ExtendedProperties configuration){
} public synchronized InputStream getResourceStream( String templateString )
throws ResourceNotFoundException{
InputStream result = null; if (templateString == null || templateString.length() == 0) {
throw new ResourceNotFoundException ("No template string provided");
}
result=new ByteArrayInputStream(templateString.getBytes());
return result;
} public boolean isSourceModified(Resource resource) {
return false;
} public long getLastModified(Resource resource) {
return 0;
}
}然后,测试代码:
public static void testVelocity() {
try {
//Velocity.init("WEB-INF/velocity.properties");
Properties p=new Properties();
p.put("input.encoding","UTF-8");
p.put("output.encoding","UTF-8");
p.put("resource.loader","srl");
p.put("srl.resource.loader.class","com.saro.custom.velocity.StringResourceLoader");
Velocity.init(p);
VelocityContext context = new VelocityContext(); context.put("coolman", "Rodjohnson");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("r.txt"),"UTF-8"));
Template tt=Velocity.getTemplate("coolman is ${coolman}","UTF-8");
tt.merge(context,writer);
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
} }