Velocity中的“比較”的思考
Velocity的比較本來很簡單,但是它在基本類型比較的時候不僅比較值的相等,同時也比較其類型的相同性。我們可以通過下面的例子來看:
我的一個新聞發佈系統:
發佈類:(部分代碼)
//-------------------- initialize the context ---------------------//
VelocityContext context = new VelocityContext();
//熱點新聞的代碼
context.put("HOT_NEWS_NUM", (long)News.HOT_NEWS);
//焦點新聞列表
context.put("FOCUSNEW", focusNew);
//圖片新聞的信息列表
context.put("PICKEY", keyList);
context.put("PICSET", picNewList);
//普通新聞列表
context.put("LIST0", list0);
context.put("LIST1", list1);
context.put("LIST2", list2);
context.put("LIST3", list3);
//------------ merge the context and template together --------//
String template = "news/_include_hot_news1.jsp";
String destFile = "/news/news/_include_hot_news1.jsp";
releaseNews(template, destFile, context);
我的模版(部分代碼):
#set($num1=$PICKEY.get(1)%4)
#foreach($news1 in $LIST1)
<tr height="16">
<td>
<img src="images/dot.gif">
<a href="<%=request.getContextPath()%>/news/ViewHotNews.sh?url=$news1.getUrl()">
$HOT_NEWS_NUM -- $news1.getPriority()
#if($news1.getNewsid() ==$PICKEY.get(1))
<strong>$news1.getTitle()</strong>
#elseif($HOT_NEWS_NUM==$news1.getPriority())
<span class="fontb">$news1.getTitle()</span>
#else
$news1.getTitle()
#end
</a>
<font color="#999999">[$news1.getIssueDate()]</font>
#if($velocityCount<=2)
<img src="images/shousuojieguoa_31.gif" width="23" height="13">
#elseif($news1.getPriority()==$HOT_NEWS_NUM)
<img src="images/shousuojieguoa_17.gif" width="26" height="11">
#else
#end
</td></tr>
#end
我加粗的部分是兩個不同類型的變量
$news1.getPriority()是long
$HOT_NEWS_NUM是int
但是值都是0
所以,如果您也需要這樣的比較的話,一定要注意了。在velocity中好象不能進行類型強制轉換,你也可以試一下。
後來,在JSP中我也遇到了同樣的問題,所以思考一下之後,我覺得應該是java本身就是這種機制:如果你進行比較的話就需要進行同種類型的比較,如果不是同種類型的話就需要進行類型轉換。認識較淺,還望大家多指點。希望和大家共同學習,共同進步。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.