What is wrong with headerKey=“-1”?

老子叫甜甜 提交于 2019-11-28 02:17:43

问题


What is wrong with:

<s:select list="fruits" name="fruitSelect" id="fruitSelect"
listKey="fid" listValue="fname" headerKey="-1" headerValue="Pick a fruit!" />

Plenty of online examples use this value: http://www.coderanch.com/t/439139/Struts/wrong-select http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/

BUT the actual documentation (you can view this on the tooltip in Eclipse by hovering over the symbol) says

Key for first item in list. Must not be empty! '-1' and '' is correct, '' is bad.

-1 seems pretty logical to me and it shouldn't be a valid option anyway. " is correct, " doesn't make sense on any level to me so I am not too concerned by it.


回答1:


Nothing wrong with the key but may be a little bit with docs: -1 is an integer key, '-1' is a string, '' is an empty char, "" is an empty string, but the key should not be empty. Thus -1, '-1', ' ', " " are valid values. From the docs:

'1' is a char, '01' is a String, "1" is a String. This is important since if the value returned by your "value" attribute is NOT the same type as the key in the "list" attribute, they WILL NOT MATCH, even though their String values may be equivalent. If they don't match, nothing in your list will be auto-selected.

You should provide to the listKey attribute a corresponding field type, so -1 is for integer of numeric types, rather than ' ' and " " are for character and string types.

The framework uses a type conversion when comparing keys and values of the select tag and to avoid a typecast errors occurred somewhere at the OGNL runtime you'd better provide the type correctly and two key attributes have the same type. And if the key value, which should be not empty, matches the value specified in the both attributes the header value option will be populated.



来源:https://stackoverflow.com/questions/22723496/what-is-wrong-with-headerkey-1

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