Wicket中的Model,PropertyModel和CompoundPropertyModel有什么区别?

时间:2022-06-07 13:23:36

I have started learning Wicket framework and I came across wicket models and I read about Model(), CompouneModel() and CompoundPropertyModel() but I didn't get the actual difference between them. I searched on Google for this, but I didn't get any information about this.

我已经开始学习Wicket框架,我遇到了wicket模型,我读到了Model(),CompouneModel()和CompoundPropertyModel(),但我没有得到它们之间的实际区别。我在谷歌搜索了这个,但我没有得到任何关于此的信息。

This link gives description between CompoundPropertyModel and PropertyModel but still its not clear properly for difference.

此链接提供了CompoundPropertyModel和PropertyModel之间的描述,但仍然不能正确区分。

Only the thing I could distinguish between Model and other two is model can not work with dynamic fields while other two can.

只有我可以区分Model和其他两个的东西是模型不能用于动态字段,而其他两个可以。

Can someone please give the difference between these models?

有人可以给出这些模型之间的区别吗?

3 个解决方案

#1


All of them are implementations of the interface IModel.

所有这些都是接口IModel的实现。

Class Model is a basic implementation, that is almost just a 'data holder' so you can store an object in that model and get. The added value of this class is to forward to get and to set model object if the stored object is an other model (IModel).

类模型是一个基本实现,几乎只是一个“数据持有者”,因此您可以将对象存储在该模型中并获取。如果存储的对象是其他模型(IModel),则此类的附加值是转发以获取和设置模型对象。

Class PropertyModel is useful if you want to get / to set a property using property expression. See an example:

如果要使用属性表达式获取/设置属性,则类PropertyModel非常有用。看一个例子:

class Data {

    private Integer data;
    private String name;

    /* getters and setters */

}

How to get and set data using the PropertyModel:

如何使用PropertyModel获取和设置数据:

Data data = new Data();
data.setId(1);
data.setName("data entity");
IModel idModel = new PropertyModel(data, "id");
IModel nameModel = new PropertyModel(data, "name");

System.out.println(data.getId());
// prints '1'
System.out.println(idModel.getObject());
// prints '1'
System.out.println(data.getName);
// prints 'data entity'
System.out.println(nameModel.getObject());
// prints 'data entity'


data.setId(2);
nameModel.setObject("a new name");

System.out.println(data.getId());
// prints '2'
System.out.println(idModel.getObject());
// prints '2'
System.out.println(data.getName());
// prints 'a new name'
System.out.println(nameModel.getObject());
// prints 'a new name'

Class CompoundPropertyModel is useful if you want to propage properties to the components by their IDs. See an example (using the same class Data):

如果要通过其ID将组件的属性支持,则类CompoundPropertyModel非常有用。查看示例(使用相同的类Data):

Java Code (MyPanel.java):

Java代码(MyPanel.java):

class MyPanel extends Panel {

    public MyPanel(IModel<Data> model) {
        super(new CompoundPropertyModel<Data>(model));
        add(new Label("id"));
        add(new Label("data"));
    }
}

Markup (MyPanel.html):

<wicket:panel>
    <span wicket:id="id">placeholer for id</span>
    <span wicket:id="name">placeholer for id</span>
</wicket:panel>

Java Code using MyClass:

使用MyClass的Java代码:

// in a Page, Panel or an other Component
Data data = new Data();
data.setId(3);
data.setName('my name');
add(new MyPanel(Model.of(data)));

Rendered output HTML (by the panel):

渲染输出HTML(由面板):

<span>3</span>
<span>my name</span>

#2


Please check https://cwiki.apache.org/confluence/display/WICKET/Working+with+Wicket+models. It gives very good explanations.
You can also read the official docs at http://ci.apache.org/projects/wicket/guide/6.x/guide/modelsforms.html

请查看https://cwiki.apache.org/confluence/display/WICKET/Working+with+Wicket+models。它给出了非常好的解释。您还可以在http://ci.apache.org/projects/wicket/guide/6.x/guide/modelsforms.html上阅读官方文档。

#3


Very good example. Just need some correction:

很好的例子。只需要一些修正:

According to

IModel idModel = new PropertyModel(data, "id");

and

private Integer data;

I expect that In Data class supposed to be

我希望In Data类应该是

private Integer id;

instead.

And the same mistake in next place

接下来就是同样的错误

 add(new Label("data"));

expect

add(new Label("name"));

to make everything correct. Thank you for your help.

使一切正确。谢谢您的帮助。

#1


All of them are implementations of the interface IModel.

所有这些都是接口IModel的实现。

Class Model is a basic implementation, that is almost just a 'data holder' so you can store an object in that model and get. The added value of this class is to forward to get and to set model object if the stored object is an other model (IModel).

类模型是一个基本实现,几乎只是一个“数据持有者”,因此您可以将对象存储在该模型中并获取。如果存储的对象是其他模型(IModel),则此类的附加值是转发以获取和设置模型对象。

Class PropertyModel is useful if you want to get / to set a property using property expression. See an example:

如果要使用属性表达式获取/设置属性,则类PropertyModel非常有用。看一个例子:

class Data {

    private Integer data;
    private String name;

    /* getters and setters */

}

How to get and set data using the PropertyModel:

如何使用PropertyModel获取和设置数据:

Data data = new Data();
data.setId(1);
data.setName("data entity");
IModel idModel = new PropertyModel(data, "id");
IModel nameModel = new PropertyModel(data, "name");

System.out.println(data.getId());
// prints '1'
System.out.println(idModel.getObject());
// prints '1'
System.out.println(data.getName);
// prints 'data entity'
System.out.println(nameModel.getObject());
// prints 'data entity'


data.setId(2);
nameModel.setObject("a new name");

System.out.println(data.getId());
// prints '2'
System.out.println(idModel.getObject());
// prints '2'
System.out.println(data.getName());
// prints 'a new name'
System.out.println(nameModel.getObject());
// prints 'a new name'

Class CompoundPropertyModel is useful if you want to propage properties to the components by their IDs. See an example (using the same class Data):

如果要通过其ID将组件的属性支持,则类CompoundPropertyModel非常有用。查看示例(使用相同的类Data):

Java Code (MyPanel.java):

Java代码(MyPanel.java):

class MyPanel extends Panel {

    public MyPanel(IModel<Data> model) {
        super(new CompoundPropertyModel<Data>(model));
        add(new Label("id"));
        add(new Label("data"));
    }
}

Markup (MyPanel.html):

<wicket:panel>
    <span wicket:id="id">placeholer for id</span>
    <span wicket:id="name">placeholer for id</span>
</wicket:panel>

Java Code using MyClass:

使用MyClass的Java代码:

// in a Page, Panel or an other Component
Data data = new Data();
data.setId(3);
data.setName('my name');
add(new MyPanel(Model.of(data)));

Rendered output HTML (by the panel):

渲染输出HTML(由面板):

<span>3</span>
<span>my name</span>

#2


Please check https://cwiki.apache.org/confluence/display/WICKET/Working+with+Wicket+models. It gives very good explanations.
You can also read the official docs at http://ci.apache.org/projects/wicket/guide/6.x/guide/modelsforms.html

请查看https://cwiki.apache.org/confluence/display/WICKET/Working+with+Wicket+models。它给出了非常好的解释。您还可以在http://ci.apache.org/projects/wicket/guide/6.x/guide/modelsforms.html上阅读官方文档。

#3


Very good example. Just need some correction:

很好的例子。只需要一些修正:

According to

IModel idModel = new PropertyModel(data, "id");

and

private Integer data;

I expect that In Data class supposed to be

我希望In Data类应该是

private Integer id;

instead.

And the same mistake in next place

接下来就是同样的错误

 add(new Label("data"));

expect

add(new Label("name"));

to make everything correct. Thank you for your help.

使一切正确。谢谢您的帮助。