如何解释EL表达式中方括号([])运算符的用法 [英] How to explain usages of square brackets ([]) operator in EL expressions
问题描述
当我阅读 Spring PetClinic 示例应用程序时,我发现他们总是将 add 和 modify 功能放入单个JSP文件中,并且使用 $ {owner ['new']}
表达式可自定义当前页面上的元素,例如标签的 New 所有者 或所有者 。
When I read Spring PetClinic sample application, I found they always put the add and modify functionalities into a single JSP file, and they use ${owner['new']}
expression to customize elements on current page, for example "New Owner" or "Owner" for a label.
在JSP(春季)环境中, []
运算符还有其他用法吗?
Are there any other usages of []
operator in JSP (Spring) environment?
Controller文件包含以下代码段:
The Controller file has the following snippet:
@RequestMapping(value = "/owners/new", method = RequestMethod.GET)
public String initCreationForm(Map<String, Object> model) {
Owner owner = new Owner();
model.put("owner", owner);
return "owners/createOrUpdateOwnerForm";
}
@RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.GET)
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
Owner owner = this.clinicService.findOwnerById(ownerId);
model.addAttribute(owner);
return "owners/createOrUpdateOwnerForm";
}
JSP文件包含以下代码段:
The JSP file has the following snippet:
<h2>
<c:if test="${owner['new']}">New </c:if> Owner
</h2>
推荐答案
[]
将允许您:
${car['type']}
这将等效于 car.getType();
(或 car.isType()
(如果 type
字段是布尔值
)。
This will be equivalent to car.getType();
(or car.isType()
if the type
field is a boolean
).
${carMap['Volvo']}
这等效于 carMap.get('Volvo');
当 carMap
是 Map
。
${cars[1]}
如果,这等效于
是一个汽车[1]
cars 数组
或等效于 cars.get(1)
(如果汽车
是列表
。
This is equivalent to cars[1]
if cars
is an array
or equivalent to cars.get(1)
if cars
is a List
.
更多详细信息/来源: http://docs.oracle.com/javaee/6/tutorial/doc/bnahu .html
More details/source: http://docs.oracle.com/javaee/6/tutorial/doc/bnahu.html
编辑:
您的问题的表达方式( $ {owner ['new']}
)属于第一种情况。在petclinick应用程序中, Owner
类是 Person
这是一个 BaseEntity
。并且 BaseEntity
具有方法 isNew()
(因此 Owner
也有这种方法。)
Your question's expression (${owner['new']}
) falls into the first case. In the petclinick app, the Owner
class is a subclass of Person
which is a subclass of BaseEntity
. And BaseEntity
has a method isNew()
(so Owner
has that method as well).
这样,代码段 $ {owner ['new']}
等同于 owner.isNew()
。
这篇关于如何解释EL表达式中方括号([])运算符的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!