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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top