Wicket:如何重定向到另一个页面?
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如您在自己的答案中指出的那样,抛出
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 aRestartResponseException
.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 ofIAuthorizationStrategy
on determining that you're not logged in will signal a configuredIUnauthorizedComponentInstantiationListener
, typically theAuthenticatedWebApplication
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 callingcontinueToOriginalDestination()
, which is a method inComponent
and retrieves the remembered page from thePageMap
.There's some good example code for this authentication process, but the exception and intercept are hiding behind the scenes somewhat.
使用客户端重定向重定向到 wicket 页面(HTTP 302,浏览器的 URL 更改):
使用服务器重定向/转发重定向到 wicket 页面(浏览器的 URL 保持不变):
自 Wicket 1.5RC5.1 起:
在 Wicket 之前1.5RC5.1:
使用 HTTP 302 重定向到 URL(“临时移动”):
使用 HTTP 301 重定向到 URL(“永久移动”,SEO 友好):
Redirect to a wicket page, using a client-redirect (HTTP 302, the browser's URL changes):
Redirect to a wicket page, using a server redirect / forward (the browser's URL remains unchanged):
Since Wicket 1.5RC5.1:
Before Wicket 1.5RC5.1:
Redirect to an URL, using HTTP 302 ("Moved Temporarily"):
Redirect to an URL, using HTTP 301 ("Moved Permanently", SEO friendly):
快速搜索 wicket 中的所有
*Exception.java
文件就发现了这一点。必须抛出一个RestartResponseAtInterceptPageException
:A quick search for all
*Exception.java
files in wicket revealed it. One has to throw aRestartResponseAtInterceptPageException
:您可以使用
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);
我刚刚发现
它至少在 wicket 6 中工作。它在服务器端工作并重写 URL。也许它比使用异常要快一些。
I just found
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.