需要一种策略来查找动态生成的下拉组件的属性
我需要为 dropDownChoice 组件设置空值字符串。 null 选择的默认值是“Choose One”,它是硬编码在 Wicket AbstractSingleChoice 类中的。
覆盖它的通常方法是定义属性资源。
但是,我的 DropdownChoice 组件是在运行时动态生成的,并添加到面板(位于数据表内)。标记中未具体引用 DropDownChoice。
我正在寻找有关如何在这种情况下以编程方式覆盖默认空值字符串的建议。如果 DropDownChoice 构造函数为此有一个附加参数,那就太好了。
我需要以编程方式创建新的属性资源吗?
希望这是有道理的。
I need to set the null value string for a dropDownChoice component. The default value for null choice is "Choose One", which is hardcoded in the Wicket AbstractSingleChoice class.
The usual way of overriding this is to define a property resource.
However my DropdownChoice component is dynamically generated at runtime, and added to a panel (which is within a datatable). The DropDownChoice is not specifically referenced in the markup.
I'm looking for suggestions on how to programmatically override the default null value string in this situation. It would be nice if the DropDownChoice constructor had an additional parameter for this.
Do I need to programmatically create a new property resource?
Hope this makes sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以覆盖
AbstractSingleSelectChoice.getNullKey()
和AbstractSingleSelectChoice。 getNullValidKey()
以使 Wicket 从 .properties 或 .xml 文件中检索本地化资源没有将组件 ID 作为密钥的一部分。此 JIRA 问题对此进行了记录:Open DropDownChoice null value Internationalization key例如:
将从
customDdcNullValue
属性中检索的文本。
这些方法似乎已在版本 1.4.4 中添加,如果您使用的是 1.3,则始终可以使用
IChoiceRenderer
和DropDownChoice
来返回正确的字符串null
值的情况。请注意,您可以从资源中检索它(使用getString()
或StringResourceModel
),或者根据需要从数据库/缓存中获取值。这可能取决于您如何本地化非空值的选择。另外,您可以使用
AbstractSingleSelectChoice.getDefaultChoice()
使用此行为滚动您自己的DropDownChoice
。更新:处理此问题的另一种方法是提供您自己的表示
null
或无选择的自定义值。只需记住在DropDownChoice
上使用setNullValid(false)
来防止在有选定值时出现null
,并使用以下命令初始化 ddc 的模型当您仍然不知道其值时,您的无选择值,以避免 Wicket 在getDefaultChoice()
调用中提供null
。定义不选择选项的值时,请特别注意
AbstractSingleSelectChoice.getDefaultChoice()
:考虑到
getNoSelectionValue()
返回-1
,所以请不要使用-1
或空字符串作为自定义 null 值。我是通过惨痛的教训才学会这一点的,相信我,这并不令人愉快。You can override
AbstractSingleSelectChoice.getNullKey()
andAbstractSingleSelectChoice.getNullValidKey()
in order to make Wicket retrieve a localized resource from a .properties or .xml file that does not have the component id as part of the key. This is documented in this JIRA issue: Open DropDownChoice null value internationalization keyFor instance:
Will retrieve the
<option>
's text from thecustomDdcNullValue
property.These methods seem to have been added in version 1.4.4, if you're on 1.3, you could always use an
IChoiceRenderer
with theDropDownChoice
that returns the proper String in the case ofnull
value. Note that you could retrieve it from resources (withgetString()
, orStringResourceModel
), or grab the value from database/cache if necessary. This may depend on how are you already localizing the choices for non-null values.Also, you could use the source code of
AbstractSingleSelectChoice.getDefaultChoice()
to roll your ownDropDownChoice
with this behavior.UPDATE: Another way of handling this is to provide your own custom value that represents
null
, or no-choice. Just remember to usesetNullValid(false)
on theDropDownChoice
to preventnull
from appearing when there is a selected value, and initialize the ddc's model with your no-selection value when you still don't know its value in order to avoid Wicket providenull
in thegetDefaultChoice()
call.When defining the value for your no-selection option, pay special attention to this part of
AbstractSingleSelectChoice.getDefaultChoice()
:Take into account that
getNoSelectionValue()
returns-1
, so please do not use-1
, or an empty string, as your custom null value. I learnt this the hard way, believe me, it's not pleasing.您应该使用本地化功能来实现它。有一个方法 org.apache.wicket.resource.loader.IStringResourceLoader.loadStringResource(org.apache.wicket.Component,java.lang.String) 会返回一个字符串值以显示给定组件。该字符串将是一个资源键,对于您的情况,它是“nullValid”字符串,IIRC。
因此,您可以实现自定义资源管理器,它将从数据库而不是
.properties
文件中选取数据。You should use localisation feature to achieve it. There is a method
org.apache.wicket.resource.loader.IStringResourceLoader.loadStringResource(org.apache.wicket.Component,java.lang.String)
which returns a string value to display for a given component. The string will be a resource key, for your case it is the "nullValid" string, IIRC.So, you can implement your custom resource manager which will pick data from a database instead of
.properties
files.