struts2 s:select value ognl expression

亡梦爱人 提交于 2019-12-12 19:18:04

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!