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,必须在构造函数中抛出一些异常,但我不记得是哪一个。提前致谢。
5 个解决方案
#1
28
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将会这样做,正如您在自己的回答中所指出的那样,但这实际上是允许在当前页面(通常是授权过程的一部分)进行重定向的系统的一部分。如果那不是你的情况,但你仍然需要做一些中断处理的事情,那么抛出一个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 code for this authentication process, but the exception and intercept are hiding behind the scenes somewhat.
这个身份验证过程有一些很好的示例代码,但异常和拦截在某种程度上隐藏在幕后。
#2
23
Redirect to a wicket page, using a client-redirect (HTTP 302, the browser's URL changes):
使用客户端重定向(HTTP 302,浏览器的URL更改)重定向到wicket页面:
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):
使用服务器重定向/转发重定向到wicket页面(浏览器的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):
使用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);
#3
5
A quick search for all *Exception.java
files in wicket revealed it. One has to throw a RestartResponseAtInterceptPageException
:
快速搜索wicket中的所有* Exception.java文件显示它。必须抛出RestartResponseAtInterceptPageException:
public MyPage() {
...
if (redirect) {
throw new RestartResponseAtInterceptPageException(targetPage);
}
...
}
#4
1
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。也许它比使用异常快一点。
#5
0
you can use
您可以使用
setResponsePage(new RedirectPage("/"));
setResponsePage(new RedirectPage(“/”));
or
要么
setResponsePage(HomePage.class);
setResponsePage(HomePage.class);
or
要么
throw new RestartResponseException(HomePage.class);
抛出新的RestartResponseException(HomePage.class);
#1
28
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将会这样做,正如您在自己的回答中所指出的那样,但这实际上是允许在当前页面(通常是授权过程的一部分)进行重定向的系统的一部分。如果那不是你的情况,但你仍然需要做一些中断处理的事情,那么抛出一个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 code for this authentication process, but the exception and intercept are hiding behind the scenes somewhat.
这个身份验证过程有一些很好的示例代码,但异常和拦截在某种程度上隐藏在幕后。
#2
23
Redirect to a wicket page, using a client-redirect (HTTP 302, the browser's URL changes):
使用客户端重定向(HTTP 302,浏览器的URL更改)重定向到wicket页面:
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):
使用服务器重定向/转发重定向到wicket页面(浏览器的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):
使用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);
#3
5
A quick search for all *Exception.java
files in wicket revealed it. One has to throw a RestartResponseAtInterceptPageException
:
快速搜索wicket中的所有* Exception.java文件显示它。必须抛出RestartResponseAtInterceptPageException:
public MyPage() {
...
if (redirect) {
throw new RestartResponseAtInterceptPageException(targetPage);
}
...
}
#4
1
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。也许它比使用异常快一点。
#5
0
you can use
您可以使用
setResponsePage(new RedirectPage("/"));
setResponsePage(new RedirectPage(“/”));
or
要么
setResponsePage(HomePage.class);
setResponsePage(HomePage.class);
or
要么
throw new RestartResponseException(HomePage.class);
抛出新的RestartResponseException(HomePage.class);