去看
org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader
的API文档。
org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader
的API文档。
解决方案 »
- htmlpaser抓取博客正文内容
- JavaWeb EJB Hibernate Sping Struts 之间的区别和联系
- java 数据库
- struts怎么发布到AIX,AIX要怎么配置
- eclipse的奇怪错误,好长时间没有解决,大家帮忙呀(分不够再加)
- structs+spring+hibernate的问题? 急!!!!!!!!!
- 埃,失业中,郁闷,写了几个连接数据库的类,请高手们帮我看看?
- 用WebServices返回除了String类型以外的对象为什么会报错误,100分送出
- 100分求weblogic7.0.1的weblogic_sp.jar文件!
- 为什么我在SUN的J2EE1.3.1下发布一个JSP+BEAN的应用,运行JSP时老说找不到我的BEAN
- 请问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();
} }