headerKey =“-1”有什么问题? [英] What is wrong with headerKey="-1"?

查看:170
本文介绍了headerKey =“-1”有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么问题:

 < s:select list =fruitsname =fruitSelectid = fruitSelect
listKey =fidlistValue =fnameheaderKey = - 1headerValue =选择一个水果! />

大量在线示例使用此值:
http://www.coderanch.com/t/439139/Struts/wrong-select
< a href =http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/ =nofollow> http://www.mkyong.com/struts2/struts -2 -select-drop-down-box-example /

但是实际的文档(你可以在Eclipse中的工具提示上查看它,将鼠标悬停在符号)表示


列表中第一项的键。一定不能是空的! '-1'和''是正确的,''是
坏。


-1对我来说似乎很合理,无论如何它不应该是一个有效的选项。 是正确的,对我来说没有任何意义,所以我不会太在意。

文档中可能有一点点错误: -1 是一个整数键,' - 1'是一个字符串,''是一个空字符,是一个空字符串,但该键不应该是空。因此 -1 ' - 1'''是有效的值。从文档:


<1>是一个字符,'01'是一个字符串,1是一个字符串。这很重要,因为如果
由value属性返回的值与list属性中的键不同,它们
不会匹配,即使它们的字符串值可能相等。如果他们不匹配,您的名单
中的任何内容都不会被自动选择。


您应该提供 listKey 为相应的字段类型赋予属性,所以 -1 用于数字类型的整数,而不是 ''用于字符和字符串类型。

当比较 select 标签的键和值时,框架使用类型转换,并避免发生类型转换错误在OGNL运行时的某处,最好提供正确的类型,并且两个关键属性具有相同的类型。如果键值不应该为空,则匹配两个属性中指定的值,则会填充标题值选项。

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.

这篇关于headerKey =“-1”有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆