Wicket:在简单 CRUD 中使用多个浏览器窗口时 ListView 未更新

发布于 2024-09-27 05:25:15 字数 2154 浏览 0 评论 0原文

好的,这是我使用 Wicket 1.5M2 的控制器:

public class Users extends WebPage {

private static final List<User> users = Collections.synchronizedList(new ArrayList<User>());

public Users() {

    final UserForm userForm = new UserForm("userForm");
    add(userForm);

    add(new ListView<User>("users", users) {

        @Override
        protected void populateItem(ListItem<User> item) {
            final User user = item.getModelObject();

            item.add(new Label("name", user.getName()));
            item.add(DateLabel.forDatePattern("dateOfBirth", new Model(user.getDateOfBirth()), "dd.MM.yyyy"));
            item.add(new Label("email", user.getEmail()));

            item.add(new Link("editLink") {

                @Override
                public void onClick() {
                    userForm.setModelObject(user);
                }

            });

            item.add(new Link("removeLink") {

                @Override
                public void onClick() {
                    users.remove(user);
                }

            });
        }

    });

    setVersioned(false);
}

private class UserForm extends Form<User> {

    public UserForm(String id) {
        super(id, new CompoundPropertyModel<User>(new User()));

        add(new TextField<String>("name"));
        add(DateTextField.forDatePattern("dateOfBirth", "dd.MM.yyyy"));
        add(new TextField<String>("email"));
    }

    @Override
    protected void onSubmit() {
        User user = getModelObject();
        users.remove(user);
        users.add(user);
        setModelObject(new User());
    }
}
}

当我一次仅使用一个浏览器窗口进行 CRUD 操作时,它可以工作。如果我使用两个窗口,用户列表不会更新,除非我再次在地址字段中输入基地址以重新加载页面。

澄清:在同一台计算机上同时使用不同的浏览器效果很好。仅当在同一浏览器中使用多个窗口时,它才不起作用。

  • 幕后发生了什么?

  • 我为整个页面设置了setVersioned(false),为什么我的地址栏显示这样的地址: http://localhost:8080/wicket/bookmarkable/package.name.Users?2 那个数字2不是页面的版本吗?

  • 最重要的是,我需要做什么才能使其表现得像桌面 GUI 应用程序,因此一个浏览器窗口 == 应用程序的一个实例。

Ok, here is my controller using Wicket 1.5M2:

public class Users extends WebPage {

private static final List<User> users = Collections.synchronizedList(new ArrayList<User>());

public Users() {

    final UserForm userForm = new UserForm("userForm");
    add(userForm);

    add(new ListView<User>("users", users) {

        @Override
        protected void populateItem(ListItem<User> item) {
            final User user = item.getModelObject();

            item.add(new Label("name", user.getName()));
            item.add(DateLabel.forDatePattern("dateOfBirth", new Model(user.getDateOfBirth()), "dd.MM.yyyy"));
            item.add(new Label("email", user.getEmail()));

            item.add(new Link("editLink") {

                @Override
                public void onClick() {
                    userForm.setModelObject(user);
                }

            });

            item.add(new Link("removeLink") {

                @Override
                public void onClick() {
                    users.remove(user);
                }

            });
        }

    });

    setVersioned(false);
}

private class UserForm extends Form<User> {

    public UserForm(String id) {
        super(id, new CompoundPropertyModel<User>(new User()));

        add(new TextField<String>("name"));
        add(DateTextField.forDatePattern("dateOfBirth", "dd.MM.yyyy"));
        add(new TextField<String>("email"));
    }

    @Override
    protected void onSubmit() {
        User user = getModelObject();
        users.remove(user);
        users.add(user);
        setModelObject(new User());
    }
}
}

It works when I use only one browser window at a time for CRUD operations. If I use two windows, the list of users does not update, unless I input the base address again to the address field to reload the page.

CLARIFICATION: Using different browsers simultaneously in same computer works good. Only when using multiple windows in same browser it does not work.

  • What is happening behind the scenes?

  • I set setVersioned(false) for the whole page, why is it that my address bar shows addresses like: http://localhost:8080/wicket/bookmarkable/package.name.Users?2
    Isn't that number 2 the version of the page?

  • Most importantly what do I need to do to make this behave like a desktop GUI app, so one browser window == one instance of the app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霊感 2024-10-04 05:25:15

我不确定您的主要问题,但 setVersioned(false) 的作用完全符合您的想法,并且似乎正在工作。无论 2 是什么,它都不是 Wicket 页面版本号;而是 Wicket 页面版本号。如果是这样,那么 2 就会在句号之后出现,而不是问号。

查看此页面有关版本控制的信息,特别是不显示版本号:Wicket 极端一致的 URL

I'm not sure about your main issue, but setVersioned(false) does exactly what you think it does and appears to be working. Whatever the 2 is, it's not the Wicket page version number; if it were, the 2 would have come after a period, not a question mark.

Check out this page about versioning, specifically not displaying version numbers: Wicket extreme consistent URLs

新人笑 2024-10-04 05:25:15

1)在您的实现中有一个有状态页面,是的,“?2”是该页面的一个版本。您可以通过打开新选项卡或提交表单来检查 - 该数字将会增加。

当您在同一浏览器中打开新选项卡时,实际上您正在使用相同的会话。 WebApp 认为您是上一个选项卡上的同一用户,并创建该页面的另一个版本。

现在,如果您在新选项卡中提交一些信息,那就没问题,但如果您返回上一个选项卡并提交一些信息 - Wicket 检测到,该版本的页面已被弃用并开始搜索实际版本。由于某种原因,您的 POST 信息此时会丢失。

2)要摆脱页面版本,您需要将页面设置为无状态。为此,您需要使链接和表单无状态:只需将它们替换为 StatelessLinkStatelessForm 对象即可。此外,设置您的页面 setStatelessHint ( true ); ,现在您的选项卡将正常工作且无需版本。开发人员示例

3) 这不是正常行为,您需要管理浏览器的cookie。有关此的更多信息 这里

1) In your implementation there is a statefull page, and yes, "?2" is a version of the page. You can check it by opening new tabs, or submiting form - this number will increase.

When you opens new tab in the same browser, actually you are using the same session. WebApp thinks that you are the same user on the previous tab and creates another version of the page.

Now, if you submit some information in new tab, than it will be ok, but if you return on previous tab and submit some info - Wicket detects, that this version of the page is deprecated and starts to search actual version. For some reason your POST info looses at this time.

2) To get rid of the page version you need to set your page stateless. To do that you need to make your links and form stateless : just replace them by StatelessLink and StatelessForm objects. Additionally, set your page setStatelessHint ( true ); and now your tabs will work correctlly and without versions. Developer example.

3) This is not normal behavior, and you need to manage browser's cookies. More about this here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文