如何在几秒钟内自动从一个页面重定向到另一页面

发布于 2024-11-27 20:08:04 字数 813 浏览 2 评论 0原文

这个想法是显示一个可见几秒钟PageExpiredPage,并自动重定向HomePage,当网络会话过期

通过以下代码,PageExpiredPage 将显示一个指向 HomePage 的可添加书签的链接。

PageExpiredPage.html:

Your session expired, log in anew by clicking
<a wicket:id="lnk-home-page" href="#"> here</a>
...

PageExpiredPage.java:

final Application app = Session.get().getApplication();
BookmarkablePageLink<? extends Page> lnkHomePage = new BookmarkablePageLink<? extends Page>("lnk-home-page", app.getHomePage());
add(lnkHomePage);
...

如何在 Wicket 中编写代码,使 PageExpiredPage 在显示时在可配置的秒数后自动重定向到 HomePage

The idea is to display a PageExpiredPage that is visible for a few seconds and automatically redirects to the HomePage, when the web session expires.

By the following code the PageExpiredPage displays with a bookmarkable link to the HomePage on it.

PageExpiredPage.html:

Your session expired, log in anew by clicking
<a wicket:id="lnk-home-page" href="#"> here</a>
...

PageExpiredPage.java:

final Application app = Session.get().getApplication();
BookmarkablePageLink<? extends Page> lnkHomePage = new BookmarkablePageLink<? extends Page>("lnk-home-page", app.getHomePage());
add(lnkHomePage);
...

How to code in Wicket that the PageExpiredPage, when displayed, automatically redirects to HomePage after a configurable number of seconds?

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

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

发布评论

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

评论(2

八巷 2024-12-04 20:08:04

比 RedirectPage 更好的解决方案是自定义行为。 RedirectPage 的明显问题是您不能使用公共基类来进行页面布局。

public class RedirectBehavior extends Behavior {

    private final Class<? extends Page> page;
    private final int redirectInSeconds;

    public RedirectBehavior(Class<? extends Page> page, int redirectInSeconds) {
        this.page = page;
        this.redirectInSeconds = redirectInSeconds;
    }

    @Override
    public void renderHead(Component component, IHeaderResponse response) {
        response.renderString(String.format("<meta http-equiv='refresh' content='%d;URL=%s' />", redirectInSeconds,
                RequestCycle.get().urlFor(page, null)));
    }
}

这样您就可以直接传递 getHomePage() 的返回值 - 不需要 newInstance()

public class PageExpiredPage extends YourBasePage {
    public PageExpiredPage () {
        add(new RedirectBehavior(Application.get().getHomePage(), 5));
    }
}

A better solution than the RedirectPage is a custom behaviour. The obvious problem with RedirectPage is that you can't use a common base class for the layout of the page.

public class RedirectBehavior extends Behavior {

    private final Class<? extends Page> page;
    private final int redirectInSeconds;

    public RedirectBehavior(Class<? extends Page> page, int redirectInSeconds) {
        this.page = page;
        this.redirectInSeconds = redirectInSeconds;
    }

    @Override
    public void renderHead(Component component, IHeaderResponse response) {
        response.renderString(String.format("<meta http-equiv='refresh' content='%d;URL=%s' />", redirectInSeconds,
                RequestCycle.get().urlFor(page, null)));
    }
}

This way you can pass the return value from getHomePage() directly - no need for newInstance():

public class PageExpiredPage extends YourBasePage {
    public PageExpiredPage () {
        add(new RedirectBehavior(Application.get().getHomePage(), 5));
    }
}
独留℉清风醉 2024-12-04 20:08:04

我可能错过了一些东西,但在我看来 RedirectPage 可以做到这一点:

让浏览器重定向的页面。如果您想将浏览器定向到某些外部 URL(例如 Google 等),请使用此选项。或者如果您想重定向到 Wicket 页面,但有延迟。(我的重点)

构造函数:

RedirectPage(Page page, int waitBeforeRedirectInSeconds)

RedirectPage 扩展了 org.apache.wicket.markup.html.WebPage 并接受 org.apache.wicket.Page 作为第一个参数。

I may have missed something, but it seems to me that RedirectPage can do just that:

Page that let the browser redirect. Use this if you want to direct the browser to some external URL, like Google etc. or if you want to redirect to a Wicket page, but with a delay. (my emphasis)

Constructor:

RedirectPage(Page page, int waitBeforeRedirectInSeconds)

RedirectPage extends org.apache.wicket.markup.html.WebPage and accepts a org.apache.wicket.Page as first argument.

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