简体   繁体   English

使用Wicket制作一个主要是无状态的Web应用程序是否很困难?

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

I've been working with Wicket for month or two now, making simple web applications with it and getting used to models and so forth. 我已经和Wicket合作了一两个月,用它制作简单的Web应用程序并习惯模型等等。 Now I'd like to move forward and see if I can put what I've learned so far to use and create a medium/large web application. 现在我想继续前进,看看我是否可以将迄今为止学到的东西用于创建中/大型Web应用程序。 However, I haven't spent much time thinking about how to make pages stateless. 但是,我没有花太多时间思考如何使页面无状态。

If I understand correctly, making a stateless page is achieved by making the page bookmarkable and making sure that no stateful components are added to the page. 如果我理解正确的话,通过使页面可书签并确保没有向页面添加有状态组件来实现创建无状态页面。

For the website I am making I want to avoid "Page expired" messages, let users be logged in via cookies, make a whole lot of the content available without needing to login/create a session, and I want functionality such as pagination to be stateless and bookmarkable. 对于我正在制作的网站,我想避免“页面过期”消息,让用户通过cookie登录,无需登录/创建会话即可创建大量内容,我希望分页等功能成为可能无国籍和可收藏。

This is no problem with eg PHP, but it seems to me that a whole lot of the useful Wicket components are stateful. 这对于例如PHP来说没有问题,但在我看来,很多有用的Wicket组件都是有状态的。 Am I in for a whole lot of work, such as creating my own set of components that are stateless, or is it no big deal? 我是否参与了很多工作,比如创建我自己的无状态组件,或者它没什么大不了的?

I hope someone can help me out by pointing me in the right direction. 我希望有人能指出我正确的方向来帮助我。

EDIT: Lets say I wanted to make a blog. 编辑:让我们说我想写一个博客。 Browsing posts, categories and so on should be possible without having to worry that the page will have become expired if a user decides to read an article for 2 hours and then tries to navigate further via eg pagination. 如果用户决定阅读文章2小时然后尝试通过例如分页进一步导航,则应该可以浏览帖子,类别等,而不必担心页面将过期。 I want to allow users to stay logged in for a month at a time, but I do not exactly want to store their session for a month either. 我希望允许用户一次保持登录一个月,但我并不想将他们的会话存储一个月。

I'd greatly appreciate any help on how I can accomplish what I just described, with Wicket. 我非常感谢任何有关如何使用Wicket完成我刚才描述的内容的帮助。

When building stateless pages with Wicket, you do lose most 'smart' built-in components, for example paginated tables and trees. 使用Wicket构建无状态页面时,您会丢失大多数“智能”内置组件,例如分页表和树。

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. 我认为对于网站,博客等来说这不是一个问题,它通常具有相当简单的导航模型,并且不使用这种“丰富”组件,并且使用无状态服务器友好的基于Javascript的组件/效果,比如jQuery-UI或YUI。

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 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 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 :) 虽然这比有状态的Wicket便宜得多,但我仍然发现比无状态的JSF或Struts要好得多:)

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 使用可收藏的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM