Java Wicket:如何重定向到另一个页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3334827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wicket: how to redirect to another page?
提问by Mot
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.
如何使用 Wicket 重定向到另一个页面?IIRC,必须在构造函数中抛出一些异常,但我不记得是哪一个。提前致谢。
采纳答案by Don Roby
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
.
RestartResponseAtInterceptPageException
正如您在自己的回答中指出的那样,抛出 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...).
我所知道的主要用途RestartResponseAtInterceptPageException
是在“重定向到登录页面”过程中。如果您正在使用基于角色的身份验证,则IAuthorizationStrategy
在确定您未登录时的实现将发出一个已配置的信号IUnauthorizedComponentInstantiationListener
,AuthenticatedWebApplication
如果您未登录,通常会抛出此异常,并重定向到已配置的登录页面。(如果您已登录但未经授权,则会发生其他事情......)。
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
.
实际的重定向是由 完成的PageMap
,在这种情况下,它也会记住您试图转到的页面。成功登录后,登录页面可以通过调用 请求将您发送到您最初尝试访问的页面continueToOriginalDestination()
,这是 中的一个方法Component
并从PageMap
.
There's some good example codefor this authentication process, but the exception and intercept are hiding behind the scenes somewhat.
这个身份验证过程有一些很好的示例代码,但异常和拦截有点隐藏在幕后。
回答by Mot
A quick search for all *Exception.java
files in wicket revealed it. One has to throw a RestartResponseAtInterceptPageException
:
快速搜索*Exception.java
wicket 中的所有文件就会发现它。必须抛出一个RestartResponseAtInterceptPageException
:
public MyPage() {
...
if (redirect) {
throw new RestartResponseAtInterceptPageException(targetPage);
}
...
}
回答by sother
Redirect to a wicket page, using a client-redirect (HTTP 302, the browser's URL changes):
使用客户端重定向(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"));
Redirect to a wicket page, using a server redirect / forward (the browser's URL remains unchanged):
使用服务器重定向/转发重定向到检票口页面(浏览器的 URL 保持不变):
Since Wicket 1.5RC5.1:
从 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:
在 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"):
使用 HTTP 302(“临时移动”)重定向到 URL:
import org.apache.wicket.request.flow.RedirectToUrlException;
...
throw new RedirectToUrlException("http://targetURL");
Redirect to an URL, using HTTP 301 ("Moved Permanently", SEO friendly):
重定向到 URL,使用 HTTP 301(“永久移动”,SEO 友好):
import org.apache.wicket.request.flow.RedirectToUrlException;
import javax.servlet.http.HttpServletResponse;
...
throw new RedirectToUrlException("http://targetURL",
HttpServletResponse.SC_MOVED_PERMANENTLY);
回答by user2081279
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.
至少在 wicket 6 中工作。它在服务器端工作并重写 URL。也许它比使用异常要快一些。
回答by Shubham Pandey
you can use
您可以使用
setResponsePage(new RedirectPage("/"));
setResponsePage(new RedirectPage("/"));
or
或者
setResponsePage(HomePage.class);
setResponsePage(HomePage.class);
or
或者
throw new RestartResponseException(HomePage.class);
throw new RestartResponseException(HomePage.class);