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本身就是这种机制:如果你进行比较的话就需要进行同种类型的比较,如果不是同种类型的话就需要进行类型转换。认识较浅,还望大家多指点。希望和大家共同学习,共同进步。
|
|