Is it difficult to make a mainly stateless web application with Wicket?

落花浮王杯 提交于 2019-11-30 00:33:33

When building stateless pages with Wicket, you do lose most 'smart' built-in components, for example paginated tables and trees.

I think this is less an issue for sites, blogs and the like, which usually have a fairly simpler navigation model and don't use this kind of 'rich' component, and use stateless-server-friendly, Javascript-based components/effects, like jQuery-UI or YUI instead.

Some things you'll do differently, like pagination. For example, instead of using built-in pagination components, you'll have to make your own mechanism, using page parameters and stateless links:

HomePage.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">&lt;&lt; Recent posts</a>
      <a wicket:id="previousPosts">Previous posts &gt;&gt;</a>
    </div>

  </div>
</body>
</html>

HomePage.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();
            }
        });
    }
}

While this is much less convenient than stateful Wicket, I still find much better than say, stateless JSF or Struts :)

joshua

To make pages stateless, I do the following:

  1. Use bookmarkable pages
  2. Use stateless forms in all cases
  3. To pass data across pages, I use page parameters as the only constructor arguments in the pages
  4. Use bookmarkable links
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!