定时刷新页面或者页面中的单独控件AjaxSelfUpdatingTimerBehavior这个类将很方便的帮助我们解决这个问题。下面来看看这个怎么用。
比如我们需要让一个 Table里的数据定时刷新,以 DefaultDataTable为例,
1
2
3
| DefaultDataTable<TaskBean> dataTable =
newDefaultDataTable<TaskBean>
("taskHost", colsArr, newSortableTaskDataProvide(taskDao), 20);
|
然后我们 new 一个 AjaxSelfUpdatingTimerBehavior
1
2
| AjaxSelfUpdatingTimerBehavior asutb = newAjaxSelfUpdatingTimerBehavior
(Duration.seconds(AUTO_REFRESH_TIME));
|
之后将这个 Behaviour 添加到 DataTable 里就可以了,
这里需要注意的是对于 Component 绑定的数据对象要能实时的拿到新的数据,比如这个例子里的 SortableTaskDataProvide 的实现需要注意。
完整代码如清单 11 所示:
清单 11. SortableTaskDataProvide 的实现代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| class SortableTaskDataProvide extendsSortableDataProvider<TaskBean> {
private TaskDao taskDao;
public SortableTaskDataProvide(TaskDao taskDao) {
this.taskDao = taskDao;
}
private static final long serialVersionUID= 1L;
public Iterator<TaskBean> iterator(final intarg0, final intarg1) {
List<TaskBean> taskList = null;
try
{
taskList = taskDao.getAllTask();
} catch(SQLException e)
{
e.printStackTrace();
}
return taskList.subList(arg0, arg0+arg1).iterator();
}
public IModel<TaskBean> model(finalTaskBean arg0) {
return new DetachableTasksModel(arg0);
} public
intsize() {
try
{
return taskDao.countAllTask();
} catch(SQLException e)
{
e.printStackTrace();
return0;
}
}
}
class DetachableTasksModel extendsLoadableDetachableModel<TaskBean> {
private static final long serialVersionUID= 1L;
private TaskBean task;
public DetachableTasksModel(TaskBean task) {
this.task = task;
}
@Override
protected TaskBean load() {
returntask;
}
}
|
修改 Wicket 并未提供的对应 HTML 元素的属性(如 DIV、SPAN)修改 Wicket 并未提供的对应 HTML 元素的属性(如 DIV、SPAN)
在开发中我遇到了一个问题,就是想要控制 DIV 或者 SPAN 这样的 HTML 元素,但 Wicket 并没有提供的对应的标签,我们要修改它们的一些属性,或者给它的里面添加文字,这个时候不知道怎么办了,最后在一个论坛里发现的一个问题给了我启示,我们可以通过 WebMarkupContainer 这个类来绑定到 DIV 这些 HTML标签上去,然后对其添加相应的 Behavior 来修改其属性,这里我们使用 AttributeModifier 这个来修改 DIV 的属性:代码如清单 12 所示。
清单 12.如何修改控件属性1
2
3
| WebMarkupContainer wmc = newWebMarkupContainer("progress");
// 这我们修改 DIV 的 style 属性
wmc.add(AttributeModifier.append("style", "width:"+percentage+"%"));
|
如果我们要给 DIV 里添加文字,该如何做呢?其实通过重载 WebMarkupContainer#onComponentTagBody 的这个方法就能够实现。
代码如清单 13 所示。
清单 13.重载 onComponentTagBody 方法的代码1
2
3
4
5
6
7
8
9
10
11
12
| WebMarkupContainer wmc = newWebMarkupContainer("progress"){
@Override
public void onComponentTagBody(MarkupStream markupStream,
ComponentTag openTag)
{
// 这里,我们可以通过 replaceComponentTagBody 这个方法来,修改 DIV 内的文字。
replaceComponentTagBody(markupStream, openTag, percentage + "%");
super.onComponentTagBody(markupStream, openTag);
}
};
|
关于 properties 文件使用时候的搜索顺序对于 message 放置的 properties 文件,为了方便搜索查找可以遵循下面的规则直接写到 yourapp.properties 文件里面。
关于 properties 文件的查找顺序:
1
2
3
4
5
6
7
8
9
| Wicket will search up the hierarchy
YourPanel.properties
YourCoolPage.properties (Panel所在的Page)
YourBaseWebPage.properties (你所用于所有Page的基类 (用于共享公共的布局或者功能))
YourApp.properties
|
|