IChoiceRenderer 的使用
在 wicket 中,DropDownChoice 的 IChoiceRenderer 的使用方式如下:
IChoiceRenderer renderer = new IChoicerRenderer() {
public Object getDisplayValue(Object object) {
return ((Country) object).getName();
}
public String getIdValue(Object object, int index) {
return ((Country) object).getId() + "";
}
};
countries.setChoiceRenderer(renderer);
IChoiceRenderer 类的规范指出:
呈现一个选择。将用于内部表示的“id”值与“显示值”分开,“显示值”是向使用此渲染器的组件的用户显示的值。
getDisplayValue()的描述是:
获取向最终用户显示的值。
这意味着它有助于显示国家/地区的名称。正确的?
getIdValue()的描述是:
调用此方法是为了获取对象的 id 值(用作选择元素的 value 属性) id 可以像主键一样从对象中提取,或者如果列表稳定,您可以只返回索引的 toString。
这是什么意思?
一般来说,各种 wicket 组件(例如这里的 DropDownChoice)的模型的 id 属性是 Long 类型。 getIdValue() 有助于排序吗?
或者帮助生成 HTML 的 id 标签?
上述的“主键”是什么概念呢?
谢谢和问候。
In wicket IChoiceRenderer for DropDownChoice is used like :
IChoiceRenderer renderer = new IChoicerRenderer() {
public Object getDisplayValue(Object object) {
return ((Country) object).getName();
}
public String getIdValue(Object object, int index) {
return ((Country) object).getId() + "";
}
};
countries.setChoiceRenderer(renderer);
The specification of the IChoiceRenderer class state that:
Renders one choice. Separates the 'id' values used for internal representation from 'display values' which are the values shown to the user of components that use this renderer.
The description of getDisplayValue() is:
Get the value for displaying to an end user.
That means it helps to display the name of the country. Right?
And the description of getIdValue() is:
This method is called to get the id value of an object (used as the value attribute of a choice element) The id can be extracted from the object like a primary key, or if the list is stable you could just return a toString of the index.
What does it mean?
In general id property of the models of various wicket component like DropDownChoice here, is of the type Long. Is getIdValue() helps to sort it?
Or helps to generate id tag for HTML?
What is concept of the aforesaid "Primary Key"?
Thanks and Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想象一下,对象将被放入映射中,其中 id 是键,值是您希望它引用的对象。如果您的两个对象共享相同的 id,或者对象的 id 发生更改,您的地图将无法正常工作。
这就是他们所说的应该是主键的意思。
顺便说一句,您不必在简单的情况下从头开始实现
IChoiceRenderer
,在您的情况下,您可以使用new ChoiceRenderer( "name", "id" );
Imagine that the objects will be put in a map, where the id is the key and the value is the object you want it to refer to. If two of your objects share the same id, or if the id of an object changes, your map won't work properly.
This is what they mean by saying it should be a primary key.
By the way, you don't have to implement
IChoiceRenderer
from scratch in simple situations, in your case you can usenew ChoiceRenderer( "name", "id" );
在下拉列表中,您将看到键值对的项目。因此,使用您的国家/地区示例,请考虑以下国家/地区到国家/地区代码的映射:
如果用户选择阿尔及利亚,则使用键
DZ
来唯一标识他们的选择。因此,如果主对象是具有countryOfCitizenship
属性的Person
,则该属性将设置为 ID 为DZ< 的
Country
/代码>。 Wicket 使用 id 将下拉列表中的选择设置为属性值。它还使用 id 来确定在为设置了该属性的对象显示页面时从下拉列表中选择哪个值。In a drop down list you will have items that are key value pairs. So using your countries example, consider the following mapping of country to country code:
If the user selects Algeria then the key
DZ
gets used to uniquely identify their choice. So if the main object is aPerson
with acountryOfCitizenship
property, that property would be set to theCountry
with the idDZ
. Wicket uses the id to set the selection from the drop down as the value for the property. It also uses the id to determine which value to select from the drop down when the page is displayed for an object that has that property set.