使用 Wicket 构建无状态页面时,您确实会丢失大多数“智能”内置组件,例如分页表和树。
我认为这对网站,博客等来说不是一个问题,它们通常具有相当简单的导航模型,并且不使用这种“丰富”组件,而是使用无状态服务器友好,基于Javascript的组件/效果,如jQuery-UI或YUI。
有些操作会有所不同,例如分页。例如,您必须使用页面参数和无状态链接来创建自己的机制,而不是使用内置的分页组件:
首页.html
<html xmlns:wicket="http://wicket.apache.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div class="container">
<div wicket:id="posts">
<h2 wicket:id="title"></h2>
<p wicket:id="content"></p>
Posted on <span wicket:id="date"></span>
</div>
<div>
<a wicket:id="recentPosts"><< Recent posts</a>
<a wicket:id="previousPosts">Previous posts >></a>
</div>
</div>
</body>
</html>
首页.java
package wishminimal.ui.home;
import java.util.Iterator;
import org.apache.wicket.PageParameters;
import org.apache.wicket.devutils.stateless.StatelessComponent;
import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import wishminimal.dao.PostDAO;
import wishminimal.entity.Post;
@StatelessComponent
public class HomePage extends WebPage {
@SpringBean
PostDAO postDAO;
ISortableDataProvider<Post> dataProvider = new SortableDataProvider<Post>() {
public Iterator<? extends Post> iterator(int first, int count) {
return postDAO.findAll(first, count).iterator();
}
public int size() {
return postDAO.countAll();
}
public IModel<Post> model(Post object) {
return new CompoundPropertyModel<Post>(object);
}
};
public HomePage(PageParameters params) {
final int currentPage = params.getAsInteger("p", 0);
final DataView<Post> dataView = new DataView<Post>("posts", dataProvider, 10) {
@Override
protected void populateItem(Item<Post> item) {
item.add(new Label("title"));
item.add(new Label("content"));
item.add(new Label("date"));
}
};
dataView.setCurrentPage(currentPage);
add(dataView);
add(new BookmarkablePageLink<Void>("recentPosts", getClass(), new PageParameters("p=" + (currentPage - 1))) {
@Override
public boolean isVisible() {
return currentPage > 0;
}
});
add(new BookmarkablePageLink<Void>("previousPosts", getClass(), new PageParameters("p=" + (currentPage + 1))) {
@Override
public boolean isVisible() {
return currentPage < dataView.getPageCount();
}
});
}
}
虽然这比有状态的Wicket方便得多,但我仍然发现比无状态的JSF或Struts:)