<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > velocity的幾層窗戶(hù)紙

velocity的幾層窗戶(hù)紙

作者: 時(shí)間:2012-05-04 來(lái)源:網(wǎng)絡(luò ) 收藏

這幾天有一個(gè)項目要使用來(lái)生成文件,遇到了一些問(wèn)題,最后也輾轉找到了解決的方法。用一個(gè)朋友的話(huà)說(shuō),這就是一層。

本文引用地址:http://dyxdggzs.com/article/149137.htm

開(kāi)發(fā)環(huán)境:Tomcat5.5,Velocity-1.4

1、模版和配置文件的位置

為了測試方便,我首先把根據模版生成文件的源代碼單獨拿出來(lái),代碼(省去了無(wú)關(guān)的代碼)如下:

//FileManager.java

//初始化方法,獲得屬性配置

privatevoidinit(){

try{

Velocity.init(.properties);

}catch(Exceptione){

System.out.println(初始化時(shí)發(fā)生錯誤!);

e.printStackTrace();

}

}

/**

*根據模板建立文件

*/

publicbooleancreateFile(Stringbt,Stringcontent,String[]tp,StringfileName){

//是否創(chuàng )建成功

booleanisCreateSuccess=true;

try{

//建立模版

VelocityContextcontext=newVelocityContext();

//設置各個(gè)變量

//…

//得到模版

Templatetemplate=Velocity.getTemplate(news.vm);

//寫(xiě)入文件

//…

}catch(Exceptione){

//異常處理

//…

}

}

這里配置文件是:velocity.properties,模版文件:news.vm,它們和FileManager的package在同一個(gè)目錄下,如:

FileManager的package為com.bokee.mydeman,FileManager.class的位置是C:1combokeemydemanFileManager.class,那么velocity.properties和news.vm的路徑就是C:1velocity.properties和C:1news.vm;如果FileManager沒(méi)有聲明package,那么配置文件和模版文件就和FileManager.class在同一個(gè)目錄下。

按照以上規則,測試通過(guò)。于是移植到Tomcat下面,可是配置文件和模版文件應該放在什么地方呢?按照上面的規則,它們就應該在WEB-INF/classes下,可是實(shí)際運行卻報出無(wú)法找到資源的異常。于是就把它們分別放在了web工程根目錄下、WEB-INF下等幾個(gè)位置,均報同樣的異常。google一下,發(fā)現竟然要放在system32下,呵呵,這樣放置程序如何移植?于是決定自己指定它們的路徑,修改后的程序如下:

//

publicclassFileManager{

privateStringconfig=;

//構造函數中指定配置文件和模版文件所在的路徑

publicFileManager(Stringconfig){

this.config=config;

init();

}

/**

*初始化模版

*/

privatevoidinit(){

//…

Velocity.init(config+/velocity.properties);

//…

}

/**

*根據模板建立文件

*/

publicbooleancreateFile(Stringbt,Stringcontent,String[]tp,StringfileName){

//…

Templatetemplate=Velocity.getTemplate(config+/news.vm);

//…

}

}

保存測試,init()方法順利通過(guò),可是模版仍然報出無(wú)法找到資源的異常。嗚呼,只有g(shù)oogle了,說(shuō)是要使用Velocity的ResourceEngine來(lái)加載資源,否則找不到vm文件。按照指示修改源代碼,如下:

VelocityEngineengine=newVelocityEngine();

Propertiesproperties=newProperties();

properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,config);

engine.init(properties);

Templatetemplate=engine.getTemplate(news.vm);

重新運行測試,OK!

2、模版的中文問(wèn)題

Java的中文問(wèn)題像一個(gè)幽靈一樣總是在程序員的周?chē)D來(lái)轉去,不定什么時(shí)候就會(huì )出來(lái)騷擾一下。

news.vm中使用了中文的字符串,在生成的文件中總是會(huì )變成莫名其妙的亂碼。查閱Velocity的Developer'sGuide文檔,在VelocityConfigurationKeysandValues一節中給出了具體的解決方法,使用input.encoding和output.encoding指定輸入和輸出的編碼格式(默認的是ISO-8859-1),于是在velocity.properties中分別指定二者的值:

input.encoding=GBK

output.encoding=GBK

單獨測試通過(guò),可以正確輸出中文??墒窃赥omcat中無(wú)論如何都出不來(lái)中文,把這兩個(gè)屬性值在ISO-8859-1、UTF-8和GBK中變換了無(wú)數次,仍然是亂碼。最后向高人求教,修改源代碼為:

Templatetemplate=engine.getTemplate(news.vm,GBK);

呵呵,一切OK!中文正常顯示,向其道謝,笑稱(chēng)“一層”。

3、$velocityCount變量

vm文件中需要使用$velocityCount,并且文件包含了數個(gè)循環(huán),而按照Velocity的DOC的說(shuō)明,$velocityCount是循環(huán)的索引值,于是便擔心$velocityCount在下一個(gè)循環(huán)中,會(huì )不會(huì )以上一個(gè)循環(huán)的結束值為初始值,實(shí)際運行發(fā)現這種擔心是沒(méi)有必要,每次$velocityCount都是從1開(kāi)始計數。

在循環(huán)中要用到$velocityCount的前一個(gè)值和后一個(gè)值,于是直接使用$velocityCount-1和$velocityCount+1。怪現象出現了,加1的情況可以正確計算出結果,并且能夠正確顯示,可是減1的語(yǔ)句直接把$velocityCount-1作為一個(gè)字符串輸出了。把表達式單獨提出來(lái),仍然如此。于是改為#set($temp=$velocityCount-1),呵呵,依然如此,$temp并沒(méi)有被賦為整數值,還是$velocityCount-1字符串。再次修改:

#set($temp=$velocityCount)

$temp-1



關(guān)鍵詞: 窗戶(hù)紙 velocity

評論


技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>