velocity判斷null,用velocity得不到list的長度的解決辦法

Velocity中如何判斷null


遇到一個棘手問題,用velocity得到從action傳過來的String值(裏面的內容用", "隔開),把此String值split後得到數組,雖然可以遍歷此數組,但是不能得到此數組的長度。

#set($two = ${question.questionTwo.split(", ")})
#foreach($c in $two)
$!{c}
#end
<pre name="code" class="html">$!{two.length()}
#end

c可以遍歷出來,但是數組的長度得不到。而且瀏覽器加載緩慢,刷新多次纔出現。這是利用velocity的問題。所以,儘量讓velocity少得有業務邏輯,把邏輯寫在後臺。

經過潘雲飛調試。改動如下:

在Question.java中新增此方法

	public List<String> getQuestionTwoList(){
		List<String> certification = new ArrayList<String>();
		if(this.questionTwo != null&& !"".equals(questionTwo)){
			String [] a = questionTwo.split(", ");
			for(int i=0; i<a.length; i++){
				certification.add(a[i]);
			}
			return certification;
		}else{
			return null;
		}
	}
謝了此方法後,questionTwoList可以當做屬性讀取,這個以後得研究一下。
</pre>把questionTwo內的值轉爲List,把List傳出來。<p></p><p>前臺questions.html改動如下:</p><p></p><pre name="code" class="html">#if(!${question.questionTwo})
<span>同問 0</span>
#else
#set($two = ${question.questionTwoList})
<span>同問 $two.size() </span> 
#end



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章