Wicket redirect: how to pass on parameters and keeps URLs “pretty”?

故事扮演 提交于 2019-12-21 17:04:24

问题


Consider a Wicket WebPage that redirects to another page (based on some logic omitted from here):

public class SomePage extends WebPage {
    public SomePage(PageParameters parameters) {
        setResponsePage(AnotherPage.class);
        setRedirect(true);
    }
}

I need to pass on the PageParameters to that other page, and this seems to be the way to do that:

setResponsePage(new AnotherPage(parameters));

However, when creating a new Page object like this, I end up at an URL such as /?wicket:interface=:1:::: instead of the clean /another. AnotherPage is defined as:

@MountPath(path = "another")
public class AnotherPage extends WebPage {
    // ...
}

(Where MountPath is from org.wicketstuff.annotation.mount package.)

So, my questions are:

  • Some other way to pass on the params?
  • Some way to keep the URL pretty? Is the above a Wicket Stuff specific limitation instead of core Wicket?

Update

Heh, turns out any of the suggested approaches work, and also what I originally tried — setResponsePage(new AnotherPage(parameters)) — as long as I remove setRedirect(true). The URL does stay the same (path to SomePage) in that case, and I just realised I really should have mentioned right from the start that it's okay if it does (as long as it's "pretty" and the parameters are passed)!

The page ("SomePage") dispatches requests, based on query params, to a couple of possible result pages that look different but that are accessed through the same url. I tried to formulate the question as generic and minimalist as possible, but that went awry as I left out relevant info. :-/

Sorry if this ended up weird, unclear or useless for others. If you have a suggestion about renaming it, feel free to comment.


回答1:


Maybe the other setResponsePage method in Component is closer to what you want:

/**
 * Sets the page class and its parameters that will respond to this request
 * 
 * @param <C>
 * 
 * @param cls
 *            The response page class
 * @param parameters
 *            The parameters for this bookmarkable page.
 * @see RequestCycle#setResponsePage(Class, PageParameters)
 */
public final <C extends Page> void setResponsePage(final Class<C> cls, PageParameters parameters)
{
    getRequestCycle().setResponsePage(cls, parameters);
}



回答2:


seanizer has the right idea, but just for completeness, there are more options than BookmarkablePageRequestTargetUrlCodingStrategy and HybridUrlCodingStrategy:

  • IndexedHybridUrlCodingStrategy
  • IndexedParamUrlCodingStrategy
  • MixedParamUrlCodingStrategy
  • QueryStringUrlEncodingStrategy



回答3:


I think the key phrase on this reference page is this:

The next step is to determine which URL encoding strategy to use. Unless one is explicity specified, the default used is BookmarkablePageRequestTargetUrlCodingStrategy. In the example above, the Terms page uses the default.

I think you need to mount another url coding strategy, probably something like HybridUrlCodingStrategy.

Edit: you probably just need to add this annotation to do that:

@MountMixedParam(parameterNames={"param1", "param2"})



回答4:


I've successfully been using this from an onClick handler, but not yet sure if it'll work from an constructor as well:

getRequestCycle().setRequestTarget(new BookmarkablePageRequestTarget(ReportPage.class, params));


来源:https://stackoverflow.com/questions/4155591/wicket-redirect-how-to-pass-on-parameters-and-keeps-urls-pretty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!