近来做了一个简单的信息发布模块,功能如下:
l 信息显示
l 信息操作
n 新增
n 修改
n 删除
l 信息查询
l 模板管理
n 下载
n 上传
基本思路:
在信息新增时对信息进行两个方面的保存:1、保存到数据库,目的便于信息的修改;2、使用velocity生成静态的html文件,以便浏览。用户可以下载velocity的“.vm”模板,由程序转变成“.html”文件给用户,用户修改(可以加上css修饰)后将“.html”文件上传,由程序转变成“.vm”文件放到velocity调用的模板目录中。
遇到问题:
1、log文件生成问题;
2、编码问题;
3、模板路径问题;
解决方法:
在velocity初始化时,添加以下属性配置:
// 设置velocity的log
Velocity.setProperty(Velocity.RUNTIME_LOG, mainPath + File.separator + "velocity.log");
// 设置velocity的输入输出编码
Velocity.setProperty(Velocity.INPUT_ENCODING, "GBK");
Velocity.setProperty(Velocity.OUTPUT_ENCODING, "GBK");
// / 设置velocity的模板路径(必要)
Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, mainPath + File.separator + "template";
// 初始化velocity引擎
try {
Velocity.init();
} catch (Exception e) {
e.printStackTrace();
}
其中mainPath 指你的应用发布目录或其他任何一个有权限使用目录,可由配置文件定义。
问题分析:
1、2点不用说了,说说第3点。一开始我在调用模板时使用绝对路径和相对路径,可是怎么测试就是不行,总是报:Unable to find resource。查看了一下源码,velocity调用的是FileResourceLoader,部分源码如下:
public void init( ExtendedProperties configuration)
{
rsvc.info("FileResourceLoader : initialization starting.");
paths = configuration.getVector("path");
/*
* lets tell people what paths we will be using
*/
int sz = paths.size();
for( int i=0; i < sz; i++)
{
rsvc.info("FileResourceLoader : adding path '" + (String) paths.get(i) + "'");
}
rsvc.info("FileResourceLoader : initialization complete.");
}
/**
* Get an InputStream so that the Runtime can build a
* template with it.
*
* @param name name of template to get
* @return InputStream containing the template
* @throws ResourceNotFoundException if template not found
* in the file template path.
*/
public synchronized InputStream getResourceStream(String templateName)
throws ResourceNotFoundException
{
/*
* Make sure we have a valid templateName.
*/
if (templateName == null || templateName.length() == 0)
{
/*
* If we don't get a properly formed templateName then
* there's not much we can do. So we'll forget about
* trying to search any more paths for the template.
*/
throw new ResourceNotFoundException(
"Need to specify a file name or file path!");
}
String template = StringUtils.normalizePath(templateName);
if ( template == null || template.length() == 0 )
{
String msg = "File resource error : argument " + template +
" contains .. and may be trying to access " +
"content outside of template root. Rejected.";
rsvc.error( "FileResourceLoader : " + msg );
throw new ResourceNotFoundException ( msg );
}
/*
* if a / leads off, then just nip that :)
*/
if (template.startsWith("/"))
{
template = template.substring(1);
}
int size = paths.size();
for (int i = 0; i < size; i++)
{
String path = (String) paths.get(i);
InputStream inputStream = findTemplate(path, template);
if (inputStream != null)
{
/*
* Store the path that this template came
* from so that we can check its modification
* time.
*/
templatePaths.put(templateName, path);
return inputStream;
}
}
/*
* We have now searched all the paths for
* templates and we didn't find anything so
* throw an exception.
*/
String msg = "FileResourceLoader Error: cannot find resource " +
template;
throw new ResourceNotFoundException( msg );
}
可见你一定要设置
// / 设置velocity的模板路径(必要)
Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, mainPath + File.separator + "template";
posted on 2006-03-08 21:09
野草 阅读(1134)
评论(0) 编辑 收藏 所属分类:
2shtv