问题
In Struts2 tutorial for s:select tag I've seen:
<s:select label="Pets"
name="petIds"
list="petDao.pets"
listKey="id"
listValue="name"
multiple="true"
size="3"
required="true"
value="%{petDao.pets.{id}}"
/> ^ ^
and my question: why value="%{petDao.pets.{id}}"
? why not simply value="%{petDao.pets.id}"
? what do those trailing curly braces mean?
回答1:
This is an OGNL list projection to get all the id
values as a list from petDao.pets
, meaning all values in this <s:select>
will be pre-selected.
回答2:
It isn't necessary; I suspect it was the result of an error in the tag's source file.
It works with it, but isn't needed, will fail IDE validation (if the IDE supports S2 and/or OGNL, e.g., IntelliJ), and I've made a note to update.
回答3:
The principal reason is because %{} syntax is used to force OGNL evaluation where Struts would otherwise be treating the value as a String literal.
For example,
<s:property value="name" />
will look for a name property in the value stack i.e. is a value retrieved by calling getName().
If you wanted to force it to use literal value "name", you will need to use the %{} syntax -
<s:property value="%{'name'}" />
Source: http://www.coderanch.com/t/420711/Struts/Struts
来源:https://stackoverflow.com/questions/14565387/struts2-sselect-value-ognl-expression