删除org.apache.commons.lang模板中的$ {var}以获取空值

时间:2016-02-11 22:41:19

标签: java apache-commons-lang3

我是org.apache.commons库的新用户。我在我的项目中使用它来做几个模板。 对于其中一个要求,我们从HTML表单获取输入并按照公共模板进行渲染。我已经创建了一个这样的示例模板 -

Hello ${user} how are you doing. Here is your order ${order}

当我从表单(用户和订单)输入两个值时,它工作正常。它给了我以下输出  你好Jhon,这是你的订单 - 两本新书。

但是,如果我只输入一个值,它会显示模板变量。像

Hello Jhon, here is your order ${order}

我需要避免这个变量。有什么建议吗?

注意:我无法控制HTML端的表单输入,即第三方表单。

谢谢!

1 个答案:

答案 0 :(得分:0)

I figures this out.I should set a default value for unresolved variables. I did like

[1] pry(main)> b = Box.first
Box Load (2.0ms)  SELECT "boxes".* FROM "boxes" LIMIT 1
=> #<Box id: 1, name: "Test", image: nil, ....                        
[2] pry(main)> b.products
Product Load (1.6ms)  SELECT "products".* FROM "products" INNER JOIN
"box_products" ON "products"."id" = "box_products"."product_id"
WHERE "box_products"."box_id" = 1
=> []
[3] pry(main)> b = Box.where("id" => 2)
Box Load (0.8ms)  SELECT "boxes".* FROM "boxes" WHERE   
"boxes"."id" = 2
=> [#<Box id: 2, name: "Yoo", image: "image.jpeg" ....
[4] pry(main)> b.products
NoMethodError: undefined method `products' for # 
<ActiveRecord::Relation:0x007f3b68c30208>
from /home/vagrant/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-3.2.13/lib/active_record/relation/delegation.rb:45:in `method_missing'

You can also give default values to unresolved variables like

Hello ${user:-} Here is your order ${order:-} 

Thanks !