How to redirect to other wicket page from onEvent(String event) method?
All that which I’ve tried is commented out, so far nothing seems to work, tried mounting pages using mountPage method, yet no change. Is it event possible to redirect to other page from ajaxBehavior?
import org.apache.wicket.ajax.AjaxEventBehavior; import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.request.cycle.RequestCycle; public class SignInPage extends WebPage { private String username; private String password; public SignInPage() { TextField<String> username = new TextField<>("username"); TextField<String> password = new TextField<>("password"); Button button = new Button("button"); AjaxEventBehavior onClick = new AjaxEventBehavior("click") { @Override protected void onEvent(AjaxRequestTarget ajaxRequestTarget) { System.out.println("inside on event"); RequestCycle res = RequestCycle.get(); res.setResponsePage(HomePage.class); RequestCycle.get().getRequestHandlerScheduledAfterCurrent().respond(res); //return new RenderPageRequestHandler(new PageProvider(HomePage.class), RenderPageRequestHandler.RedirectPolicy.ALWAYS_REDIRECT); //getRequestCycle().setResponsePage(HomePage.class); //getRequestCycle().getRequestHandlerScheduledAfterCurrent().respond(getRequestCycle()); //setRedirect// not available //getRequestCycle().setRdirect()// not available //getRequestCycle().setResponsePage(HomePage.class);//setResponsePage(HomePage.class); /* redirectToInterceptPage(new HomePage()); ajaxRequestTarget.respond(getRequestCycle());*/ } }; button.add(onClick); add(username); add(password); add(button); } }
Advertisement
Answer
After some time, I realized that this was Authorization issue and that redirect works as per this code (I’ve used a different role than was defined on HomePage.java.
i.e
@AuthorizeInstantiation(“USER”), but the role after signing was @AuthorizeInstantiation(“ADMIN”))
import org.apache.wicket.ajax.AjaxEventBehavior; import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.request.cycle.RequestCycle; public class SignInPage extends WebPage { private String username; private String password; public SignInPage() { TextField<String> username = new TextField<>("username"); TextField<String> password = new TextField<>("password"); Button button = new Button("button"); AjaxEventBehavior onClick = new AjaxEventBehavior("click") { @Override protected void onEvent(AjaxRequestTarget ajaxRequestTarget) { System.out.println("inside on event"); setResponsePage(HomePage.class); } }; button.add(onClick); add(username); add(password); add(button); } }