Velocity 1.7を使用して文字列をフォーマットしていますが、デフォルト値で問題が発生しました。Velocity自体には、値が設定されておらず、別のデフォルト値を使用する場合の特別な構文はありません。Velocityを使用すると、次のようになります。
#if(!${name})Default John#else${name}#end
これは私の場合には不便です。グーグルした後、DisplayToolを見つけました。ドキュメントによると、次のようになります。
$display.alt($name,"Default John")
そのため、Mavenの依存関係を追加しましたが、メソッドにDisplayToolを追加する方法がわからず、そのための手順を見つけるのは困難です。多分誰かがアドバイスを手伝ったり、役に立つリンクを与えることができますか?..
私の方法:
public String testVelocity(String url) throws Exception{
Velocity.init();
VelocityContext context = getVelocityContext();//gets simple VelocityContext object
Writer out = new StringWriter();
Velocity.evaluate(context, out, "testing", url);
logger.info("got first results "+out);
return out.toString();
}
送るとき
String url = "http://www.test.com?withDefault=$display.alt(\"not null\",\"exampleDefaults\")&truncate=$display.truncate(\"This is a long string.\", 10)";
String result = testVelocity(url);
「http://www.test.com?withDefault=$display.alt(\"notnull\",\"exampleDefaults\")&truncate=$display.truncate(\"これは長い文字列です。\" 、10) "変更なし、ただし取得する必要があります
"http://www.test.com?withDefault=not null&truncate=This is...
私が欠けているものを教えてください。ありがとう。