java Wicket 重定向:如何传递参数并保持 URL“漂亮”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4155591/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:05:26  来源:igfitidea点击:

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

javaredirectwicketfriendly-url

提问by Jonik

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

考虑一个重定向到另一个页面的 Wicket 网页(基于此处省略的一些逻辑):

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:

我需要将 PageParameters 传递到另一个页面,这似乎是这样做的方法:

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:

但是,当像这样创建一个新的 Page 对象时,我最终会得到一个 URL,/?wicket:interface=:1::::而不是 clean /another。另一个页面定义为:

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

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

(其中 MountPath 来自 org.wicketstuff.annotation.mount 包。)

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?
  • 传递参数的其他方式?
  • 一些保持URL漂亮的方法?以上是 Wicket Stuff 的特定限制而不是核心 Wicket 吗?

Update

更新

Heh, turns out anyof the suggested approaches work, and alsowhat 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 realisedI 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)!

呵呵,结果证明任何建议的方法都有效,我最初尝试的方法也是如此setResponsePage(new AnotherPage(parameters))——只要我删除setRedirect(true). 在这种情况下,URL 确实保持不变(SomePage 的路径),我刚刚意识到我真的应该从一开始就提到,如果这样做也没关系(只要它“漂亮”并且传递了参数)!

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. :-/

页面(“SomePage”)根据查询参数将请求分派到几个可能的结果页面,这些页面看起来不同但通过相同的 url 访问。我试图将问题表述为尽可能通用和极简的问题,但由于我遗漏了相关信息,这出了差错。:-/

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

抱歉,如果这对其他人来说很奇怪、不清楚或无用。如果您有关于重命名的建议,请随时发表评论。

采纳答案by mfunk

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

也许 Component 中的另一个 setResponsePage 方法更接近你想要的:

/**
 * 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);
}

回答by Pops

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

seanizer 有正确的想法,但只是为了完整性,还有比BookmarkablePageRequestTargetUrlCodingStrategyand更多的选项HybridUrlCodingStrategy

回答by Sean Patrick Floyd

I think the key phrase on this reference pageis 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.

下一步是确定使用哪种 URL 编码策略。除非明确指定,否则使用的默认值是BookmarkablePageRequestTargetUrlCodingStrategy。在上面的示例中,条款页面使用默认值。

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

我认为您需要安装另一个 url 编码策略,可能类似于HybridUrlCodingStrategy.

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

编辑:您可能只需要添加此注释即可:

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

回答by Tim

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

我已经成功地从 onClick 处理程序中使用了它,但还不确定它是否也可以从构造函数中工作:

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