如何创建隐藏其参数的Wicket URL? [英] How can I create a Wicket URL that hides its parameters?

查看:115
本文介绍了如何创建隐藏其参数的Wicket URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下代码创建一组链接:

I'm currently creating a set of links with code like this:

BookmarkablePageLink<CheeseMain> havarti =
    new BookmarkablePageLink<CheeseMain>("havarti", CheeseMain.class);
havarti.setParameter("Title", "Havarti");
havarti.setParameter("Group", "cheeseName");
add(havarti);

出来的网址格式为
https:/ /mysite.com/;jsessionid=B85EE5CB0349CCA2FE37AF76AB5C30C1?wicket:bookmarkablePage=:com.mycompany.cheese.CheeseMain&Title=Havarti&group=cheeseName

我的问题是我不再希望此链接的网址是可收藏的。理想情况下,我希望它像 https://mysite.com/cheese 一样简单,但我可以使用丑陋的URL。重要的是参数不可见。

My problem is that I no longer want the URL for this link to be bookmarkable. Ideally, I would like it to be something simple like https://mysite.com/cheese, but I can live with an ugly URL. The important thing is that the parameters aren't visible.

我应该如何改变我生成链接的方式?我查看了Wicket提供的不同URL编码策略,但没有一个删除参数;它们只是以不同的方式显示它们。

How should I change the way I'm generating my links? I've looked at the different URL encoding strategies that Wicket provides, but none of them remove the parameters; they just display them differently.

推荐答案

只有当页面可收藏,或者特定链接是可收藏的时,参数才会显示在URL中。

The parameters appear in the URL only if the page is bookmarkable, or the specific link is bookmarkable.

如果您使用 setResponsePage(页面)创建导航到页面的链接 (传递一个Page实例)而不是 setResponsePage(Class< Page>,PageParameters)(传递一个Page类),创建的链接不会指向到页面的可收藏版本,但是到有状态的实例。

If you create a Link that navigates to the page using setResponsePage(Page) (passing a Page instance) instead of setResponsePage(Class<Page>, PageParameters) (passing a Page class), the link created will not point to the bookmarkable version of the page, but to a stateful instance.

为了使这个工作,你不能调用 super(PageParameters )构造函数(以便页面没有足够的信息来构建无状态URL)。

To make this work, though, you must not call the super(PageParameters) constructor (so that the Page doesn't have enough information to build the stateless URL).

在这个例子中,你可以导航到 SecretPage 通过两个不同的链接,一个是无状态的,可收起书签的,另一个是有状态的。

In this example, you can navigate to the SecretPage through two different links, one stateless, bookmarkable, and the other stateful.

SecretPage 也有两个构造函数。一个人收到 PageParameters 并调用 super 传递它。另一个直接通过construcor参数接收值,并且不将它传递给 super (如果它调用 super(new PageParameters())。添加(消息,消息),如在注释行中,它将自动重定向到可收藏的URL)。

SecretPage also has two constructors. One receives a PageParameters and calls super passing it. The other receives the value directly via construcor parameter, and doesn't pass it to super (if it'd called super(new PageParameters().add("message",message), as in the commented line, it would automatically redirect to a bookmarkable URL).

HomePage.java :

HomePage.java:

public class HomePage extends WebPage {
    public HomePage(final PageParameters parameters) {
        add(new BookmarkablePageLink<Void>("bookmarkable", SecretPage.class,
            new PageParameters().add("message", "This message will appear in the URL")));
        add(new Link<Void>("instance") {
            @Override
            public void onClick() {
                setResponsePage(new SecretPage("This message will NOT appear in the URL"));
            }
        });
    }
}

HomePage.html :

HomePage.html:

<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" >
<body>
  <p><a wicket:id="bookmarkable">Bookmarkable link (stateless)</a></p>
  <p><a wicket:id="instance">Hidden parameters link (stateful)</a></p>
</body>
</html>

SecretPage.java

SecretPage.java

public class SecretPage extends WebPage {
    public SecretPage(PageParameters parameters) {
        super(parameters);
        init(parameters.get("message").toString("No message!"));
    }
    public SecretPage(String message) {
        // super(new PageParameters().add("message", message)); // COMMENTED!
        init(message);
    }
    private void init(String message) {
        info(message);
        add(new FeedbackPanel("feedback"));
        add(new BookmarkablePageLink<Void>("back", getApplication().getHomePage()));
    }
}

SecretPage.html

SecretPage.html

<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" >
<body>
  <p wicket:id="feedback"></p>
  <p><a wicket:id="back">BACK</a></p>
</body>
</html>

并且,要有一个简单的URL,例如 http:// host / app / secret ,你必须挂载它。您可以在 WebApplication 类中执行此操作。

And, to have a simple URL, like http://host/app/secret, you must mount it. You can do it in your WebApplication class.

WicketApplication.java

WicketApplication.java

public class WicketApplication extends WebApplication {
    @Override
    protected void init() {
        super.init();
        mountPage("home", getHomePage());
        mountPage("secret", SecretPage.class);
    }
    public Class<HomePage> getHomePage() {
        return HomePage.class;
    }
}

此示例使用Wicket 1.5(仍为RC4.2) ,需要进行一些修改才能使用1.4.x(某些方法和类被重命名,或移到不同的包中),但想法是一样的。

This example uses Wicket 1.5 (still RC4.2), and need some modifications to work with 1.4.x (some methods and classes were renamed, or moved to different packages), but the idea is the same.

这篇关于如何创建隐藏其参数的Wicket URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆