Wicket implement flash redirection - wicket

I would like to perform something like a flash redirection (not sure if it is really called flash redirection).
After a certain action like delete device, I am redirecting to the device list page.
Now, for the redirect URL... I am appending &sdr=true and it works.
The problem is... that parameter (&sdr=true) stays there even after just refreshing the page.
If I remember it correctly, when I do flash refresh... the parameter stays there but will disappear on refresh... or just good for one refresh only.
Below is my method for redirection:
public static void redirect(String redirectUrl) {
throw new RedirectToUrlException(redirectUrl);
}
now, how do I implement the flash redirection in wicket? I am using wicket 6 version.
Or... I would like the parameter &sdr=true be good only for 1 request. When the page is refreshed or reloaded... it should be gone.
Thanks :)
Sorry if this question is very easy, I am really new to wicket

May be there is another solution for your problem.
Wicket could be stateful, i.e. it can keep state between the pages navigation. So you can do:
setResponsePage(new SomePage(someState));
this way there is no need to pass anything in the url and SomePage's constructor can decide what to do with the passed state.
If you prefer to add request parameter in the url then you may use
PageParameters params = new PageParameters();
params.put("sdr", "true");
setResponsePage(SomePage.class, params);
In SomePage's constructor you will need to remove the sdr parameter from the passed PageParameters so that it is not rendered in all urls inside the page, e.g. in links, form action, etc.
But if you want the parameter to disappear in a page refresh/reload then you will need to make another redirect:
public SomePage(PageParameters params) {
super(params);
StringValue srdValue = params.get("srd");
if (!srdValue.isNull()) {
params.remove("srd");
// do something custom
throw new RestartResponseException(this);
// or throw new RestartResponseException(getPageClass(), params);
}
}

Related

In wicket, how to make the page re-init when back button is hit

I'm using Wicket 6, and we have a situation where a user is hitting back and it's loading the page without initializing it from the page history. I want the page init to run so that data is read fresh and things are in the proper state. How can I make wicket do this?
I thought I was already doing this with a custom MountedMapper that someone had suggested long ago, but I have a breakpoint in the page constructor (the one that accepts PageParameters) and it's not running.
The custom MountedMapper:
if (requestHandler instanceof ListenerInterfaceRequestHandler || requestHandler instanceof BookmarkableListenerInterfaceRequestHandler) {
return null;
} else {
return super.mapHandler(requestHandler);
}
You could make your page stateless, so it is recreated on each access.
Or improved your page, so that it loads fresh data on each render:
either use appropriate models that automatically deliver up-to-date data or override #onConfigure() and update,

How can I reload my page to redirect to an URI Fragment in VAADIN?

In my Vaadin webapp I have a tipical architecture with login. In some cases, the user can access directly to some resources using Vaadin URI Fragments (http://example.com/#fragment).
When a user tryes to access some resource, If the user has logged in, I take from the URL the #FRAGMENT and I bring him to it.
But if the user has no logged in, when he logs in I used to bring him to the main page using
getPage().open("/", "_self");
but since if I add an URI Fragment, the getPage().open(...) does not work.
Is there any way to redirect the user to a correct URL (URL with UriFragment in my case) from code?
Note that there is a fundamental difference in how navigation is handled in traditional web applications versus single-page applications as implemented with Vaadin. In traditional web applications you navigate through the app by making full HTTP GET-Requests on some path (such as www.example.com/myapp/home). On each such request, a full page reload is performed. You can't do that with Vaadin, as a full page reload means reloading the Vaadin widget set and rebuilding the page layout from the ground up. Therefore, single-page applications typically use the URI fragment for navigation purposes. Changes to this fragment are solely handled by the client-side JavaScript code. There will be no GET-Request induced by the browser when the URI fragment is changed.
That's why the approach you described doesn't work for you. Using Page.open(...) will open a web page through a HTTP GET-Request resulting in a complete reload of your Vaadin application.
The solution for your problem is to solely handle all navigation (including state-dependent redirects) through the Page object's URI fragment handling methods (or through the [Navigator][1] component). Redirecting in Vaadin can be achieved by programmatically setting the URI fragment with Page#setUriFragment() or Navigator#navigateTo() and having your URI handling code (or Navigator) take care of the rest. Only then it is assured that your users stay on the same page even when they are redirected to a login form or to some other place after logging in.
I would like to add to Roland's answer and share how I solved this.
My UI:
#Override
protected void init(VaadinRequest request) {
setSizeFull();
setContent(masterView);
getPage().addUriFragmentChangedListener(event -> present(event.getUriFragment()));
present(getPage().getUriFragment());
}
The masterView is just a CustomComponent that has a content section. When the menu is clicked, I simply setContent to the masterView's content section. Swapping out the middle, basically.
present method:
private void present(String fragment) {
masterView.setContent(getComponentFromFragment(fragment));
}
Finally:
private Component getComponentFromFragment(String fragment) {
if (fragment.equals(someOtherView.NAME))
return someOtherView;
return null; // null clears it out as in the welcome page
}
The important part is the present in the init. When the UI renders for the first time and fires the init, it goes ahead and grabs whatever the URI fragment is in the browser and presents that as well.
Works great.
Maybe this can work:
UI.getCurrent().getPage().executeJavaScript("window.location.href = 'http://google.com'");

How to server-side forward a page request with Wicket 6

In some circumstances, I have to pass a request to a Wicket page to another Wicket page on the server side, i.e. forward maintaining the URL in the browser address bar, but passing the page parameters to the second page.
Before Wicket 1.5, I could do
public MyPage(PageParameters params) {
// some logic here to decide whether and where to forward
setRedirect(false);
setResponsePage(MyOtherPage.class, params);
}
As setRedirect(boolean) no longer exists, is there a way to achieve a server-side forward in later Wicket versions?
A colleague just found the solution here:
http://mail-archives.apache.org/mod_mbox/wicket-users/201203.mbox/%3CCAMomwMr2fkO38E3d9RTk5TEmuf0Vx66F46F8eYs84Bb3bVtPgA#mail.gmail.com%3E
Now it is:
RequestCycle.get().scheduleRequestHandlerAfterCurrent(new RenderPageRequestHandler(new PageProvider(MyOtherPage.class, params), RenderPageRequestHandler.RedirectPolicy.NEVER_REDIRECT));
Scary piece of code... does not look elegant at all, but works.
On Wicket 6 you can redirect to another page in a simpler way, throwing RestartResponseAtInterceptPageException at any point in your page code:
throw new RestartResponseAtInterceptPageException(WicketPage.class)
It worked fine for me...
You should be able to simply do:
throw new RestartResponseException(MyOtherPage.class, params);

How To Redirect From a Control Using MVC2

I have been tasked with creating a user control to live in our master page that allows users to switch between accounts. This way, we can allow users to change their account without having to go back to the accounts page. This seemed like a legitimate and perfectly straightforward task.
I've built the control and added it to the master page using Html.RenderAction. The last step is for me to redirect the user to the home page for that account. In order to do this, I build a route to the home page and attempt return RedirectToRoute(route).
When I attempt this, I get this error:
Child actions are not allowed to perform redirect actions
Anyone have any ideas on how to resolve this or have I coded myself into a box
Thanks in advance
You can cheat with an ugly hack:
[ChildActionOnly]
public ActionResult SomeUserControlAction()
{
// ... some processing
var url = Url.RouteUrl("routeName", new
{
action = "foo",
controller = "bar"
});
Response.Redirect(url);
return null;
}
It's so ugly that I feel ashamed for even mentioning it, but it works.
Another possibility would be to pass the url as part of the view model to the view and perform the redirect in javascript by setting window.location.href to the new url.

GWT. Set url without submit

Can i change url(set parameter) without submit?
I found this method
http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/Window.Location.html#replace%28java.lang.String%29
but it submit page. All GWT state will be lost.
If you want to change something that is not in the hash, for example you want to change a parameter in the URL, you can do it like this!
private void someMethod() {
String newURL = Window.Location.createUrlBuilder().setParameter("someParam", "someValue").buildString();
updateURLWithoutReloading(newURL);
}
private static native void updateURLWithoutReloading(String newUrl) /*-{
$wnd.history.pushState(newUrl, "", newUrl);
}-*/;
Then you could register a function that handles the user using the back and forward browser buttons as demonstrated here.
Why are you trying to do this? Generally speaking, GWT apps don't change pages - thus they are normally SPAs (single page applications)
When you load a new page from a server, you will lose the state on that page. You can change the hash part of the URL as that won't return to the server, like this:
String newURL = Window.Location.createUrlBuilder().setHash("newhash").buildString();
Window.Location.replace(newURL);
However, if you're going to do this, I would recommend taking a look at GWT's MVP framework, which has built in support for managing locations using hash tokens.
http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html
$wnd.history.pushState(newUrl, "", newUrl);
Works nicely in HTML5-browsers. Not in IE8 or IE9!