0
添加一個字典,像這樣的價值觀:避免Nvelocity拋出異常,當辭典鍵不存在
Dictionary<string, string> CustomArray = new Dictionary<string, string>();
CustomArray.Add("customValue1", "mydata");
this.velocityContext.Put("array", CustomArray);
使用模板引擎這樣的:
Velocity.Init();
string template = FileExtension.GetFileText(templateFilePath);
var sb = new StringBuilder();
using(StringWriter sw = new StringWriter(sb))
{
using(StringReader sr = new StringReader(template))
{
Velocity.Evaluate(
this.velocityContext,
sw,
"test template",
sr);
}
}
return sb.ToString();
模板訪問的是這樣的:
('customValue1')$ array.Get_Item('customValu' e2')
customValue1檢索得很好,但customValue2拋出KeyNotFoundException,因爲該字典中不存在該鍵。如何在不刪除引發KeyNotFoundException的行的情況下生成模板?
我已經看過了Apache的速度指引,但我不知道如何添加這個(https://velocity.apache.org/tools/devel/creatingtools.html#Be_Robust)
你是絕對正確的,非常感謝! – jmelhus