Wicket:如何重定向到另一个页面?

发布于 2024-09-11 02:35:36 字数 64 浏览 4 评论 0原文

如何使用 Wicket 重定向到另一个页面? IIRC,构造函数中必须抛出一些异常,但我不记得是哪一个。提前致谢。

How do I redirect to another page using Wicket? IIRC, some exception has to be thrown in the constructor, but I don't remember which one. Thanks in advance.

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

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

发布评论

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

评论(5

晒暮凉 2024-09-18 02:35:36

正如您在自己的答案中指出的那样,抛出 RestartResponseAtInterceptPageException 就可以做到这一点,但这实际上是允许重定向并最终在当前页面继续的系统的一部分(通常是授权过程的一部分)。如果这不是您的情况,但您仍然需要执行一些中断处理的操作,那么抛出 RestartResponseException 可能会更好。

据我所知,RestartResponseAtInterceptPageException 的主要用法是在“重定向到登录页面”过程中。如果您使用基于角色的身份验证,则确定您未登录时 IAuthorizationStrategy 的实现将向已配置的 IUnauthorizedComponentInstantiationListener 发出信号,通常是 AuthenticatedWebApplication< /code> 如果您未登录,则会抛出此异常,并重定向到配置的登录页面。 (如果您已登录但未经授权,则会发生其他情况......)。

实际的重定向是由 PageMap 完成的,在本例中它也会记住您尝试访问的页面。成功登录后,登录页面可以通过调用 ContinueToOriginalDestination() 要求将您转到最初尝试访问的页面,这是 Component 中的一个方法,并检索PageMap 中记住的页面。

对于此身份验证过程,有一些很好的示例代码,但异常和拦截在某种程度上隐藏在幕后。

Throwing a RestartResponseAtInterceptPageException will do it, as you noted in your own answer, but that's really part of a system for allowing a redirect with an eventual continuation at the current page (frequently part of an authorization process). If that's not your situation, but you still have to do something that interrupts processing, it might be better to throw a RestartResponseException.

The principal usage that I know of for RestartResponseAtInterceptPageException is in the "redirect to login page" process. If you're using role-based authentication, an implementation of IAuthorizationStrategy on determining that you're not logged in will signal a configured IUnauthorizedComponentInstantiationListener, typically the AuthenticatedWebApplication which throws this exception if you're not logged in, with a redirect to a configured login page. (If you're logged in but unauthorized, something else happens...).

The actual redirect is done by the PageMap, which also in this case remembers the page you were trying to go to. After a successful login, the login page can ask to send you to the page you were trying for originally by calling continueToOriginalDestination(), which is a method in Component and retrieves the remembered page from the PageMap.

There's some good example code for this authentication process, but the exception and intercept are hiding behind the scenes somewhat.

南七夏 2024-09-18 02:35:36

使用客户端重定向重定向到 wicket 页面(HTTP 302,浏览器的 URL 更改):

import org.apache.wicket.RestartResponseException;
import org.apache.wicket.request.mapper.parameter.PageParameters;
...
throw new RestartResponseException(
    TargetWicketPage.class, 
    new PageParameters().set("param1", "value1")); 

使用服务器重定向/转发重定向到 wicket 页面(浏览器的 URL 保持不变):

自 Wicket 1.5RC5.1 起:

import org.apache.wicket.RestartResponseException;
import org.apache.wicket.request.handler.PageProvider;
import org.apache.wicket.request.handler.RenderPageRequestHandler.RedirectPolicy;
import org.apache.wicket.request.mapper.parameter.PageParameters;
...
throw new RestartResponseException(
    new PageProvider(
        TargetWicketPage.class, 
        new PageParameters().set("param1", "value1")), 
    RedirectPolicy.NEVER_REDIRECT));

在 Wicket 之前1.5RC5.1:

import org.apache.wicket.request.RequestHandlerStack.ReplaceHandlerException;
import org.apache.wicket.request.handler.PageProvider;
import org.apache.wicket.request.handler.RenderPageRequestHandler;
import org.apache.wicket.request.handler.RenderPageRequestHandler.RedirectPolicy;
import org.apache.wicket.request.mapper.parameter.PageParameters;
...
throw new ReplaceHandlerException(
    new RenderPageRequestHandler(
        new PageProvider(
            TargetWicketPage.class, 
            new PageParameters().set("param1", "value1")), 
        RedirectPolicy.NEVER_REDIRECT), 
    true);

使用 HTTP 302 重定向到 URL(“临时移动”):

import org.apache.wicket.request.flow.RedirectToUrlException;
...
throw new RedirectToUrlException("http://targetURL");

使用 HTTP 301 重定向到 URL(“永久移动”,SEO 友好):

import org.apache.wicket.request.flow.RedirectToUrlException;
import javax.servlet.http.HttpServletResponse;
...
throw new RedirectToUrlException("http://targetURL", 
    HttpServletResponse.SC_MOVED_PERMANENTLY);

Redirect to a wicket page, using a client-redirect (HTTP 302, the browser's URL changes):

import org.apache.wicket.RestartResponseException;
import org.apache.wicket.request.mapper.parameter.PageParameters;
...
throw new RestartResponseException(
    TargetWicketPage.class, 
    new PageParameters().set("param1", "value1")); 

Redirect to a wicket page, using a server redirect / forward (the browser's URL remains unchanged):

Since Wicket 1.5RC5.1:

import org.apache.wicket.RestartResponseException;
import org.apache.wicket.request.handler.PageProvider;
import org.apache.wicket.request.handler.RenderPageRequestHandler.RedirectPolicy;
import org.apache.wicket.request.mapper.parameter.PageParameters;
...
throw new RestartResponseException(
    new PageProvider(
        TargetWicketPage.class, 
        new PageParameters().set("param1", "value1")), 
    RedirectPolicy.NEVER_REDIRECT));

Before Wicket 1.5RC5.1:

import org.apache.wicket.request.RequestHandlerStack.ReplaceHandlerException;
import org.apache.wicket.request.handler.PageProvider;
import org.apache.wicket.request.handler.RenderPageRequestHandler;
import org.apache.wicket.request.handler.RenderPageRequestHandler.RedirectPolicy;
import org.apache.wicket.request.mapper.parameter.PageParameters;
...
throw new ReplaceHandlerException(
    new RenderPageRequestHandler(
        new PageProvider(
            TargetWicketPage.class, 
            new PageParameters().set("param1", "value1")), 
        RedirectPolicy.NEVER_REDIRECT), 
    true);

Redirect to an URL, using HTTP 302 ("Moved Temporarily"):

import org.apache.wicket.request.flow.RedirectToUrlException;
...
throw new RedirectToUrlException("http://targetURL");

Redirect to an URL, using HTTP 301 ("Moved Permanently", SEO friendly):

import org.apache.wicket.request.flow.RedirectToUrlException;
import javax.servlet.http.HttpServletResponse;
...
throw new RedirectToUrlException("http://targetURL", 
    HttpServletResponse.SC_MOVED_PERMANENTLY);
孤独患者 2024-09-18 02:35:36

快速搜索 wicket 中的所有 *Exception.java 文件就发现了这一点。必须抛出一个 RestartResponseAtInterceptPageException

public MyPage() {
   ...
   if (redirect) {
       throw new RestartResponseAtInterceptPageException(targetPage);
   }
   ...
}

A quick search for all *Exception.java files in wicket revealed it. One has to throw a RestartResponseAtInterceptPageException:

public MyPage() {
   ...
   if (redirect) {
       throw new RestartResponseAtInterceptPageException(targetPage);
   }
   ...
}
醉生梦死 2024-09-18 02:35:36

您可以使用

setResponsePage(new RedirectPage("/"));

setResponsePage(HomePage.class);

抛出 new RestartResponseException(HomePage.class);

you can use

setResponsePage(new RedirectPage("/"));

or

setResponsePage(HomePage.class);

or

throw new RestartResponseException(HomePage.class);

木格 2024-09-18 02:35:36

我刚刚发现

getRequestCycle().setResponsePage(MyOtherPage.class);

它至少在 wicket 6 中工作。它在服务器端工作并重写 URL。也许它比使用异常要快一些。

I just found

getRequestCycle().setResponsePage(MyOtherPage.class);

which is working at least in wicket 6. It works server-side and rewrites the URL too. Maybe it is a bit faster than using an exception.

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