近來做了一個簡單的信息發布模塊,功能如下:
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
野草 閱讀(1142)
評論(0) 編輯 收藏 所屬分類:
2shtv