ブール型プロパティが持つ可能性のある3つの状態すべてに応じて、3つの異なるケースをフォークするにはどうすればよいですか?このためのJavaコードは単純に見えます。
public class Foo {
public Boolean getBool() { return null /* this would be dynamic in RL */; }
}
// somewhere in the servlet code:
if (foo.getBool() == null) {
resp.getWriter().print("not yet set");
} else if (foo.getBool()) {
resp.getWriter().print("set to TRUE");
} else {
resp.getWriter().print("set to FALSE");
}
仕様にはnullリテラルがなく、単純化のためにブール/非nullの等価性チェックはある程度代替可能であるため、速度はここでは明らかに失敗しているようです。もちろん、このジレンマを回避するための2つの解決策があります(以下を参照)が、いくつかの簡単でクリーンな方法はありますか?
次のように、Fooクラスにゲッターを追加するだけです。
boolean isBoolSet(){return getBool()!= null; }
VTLコードは次のようになります。
#if(!$foo.boolSet)
not yet set
#else
#if($foo.bool)
set to TRUE
#else
set to FALSE
#end
#end
そのように、いくつかのnull値をフェッチします。
Object getTheNull(){nullを返す; }
VTLは次のようになります。
#if($foo.bool == $foo.theNull)
not yet set
#else
#if($foo.bool)
set to TRUE
#else
set to FALSE
#end
#end