如何创建隐藏其参数的 Wicket URL?

发布于 2024-11-16 08:58:59 字数 672 浏览 4 评论 0原文

我目前正在创建一组带有如下代码的链接:

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

出现的 URL 具有以下格式 https://mysite.com/;jsessionid=B85EE5CB0349CCA2FE37AF76AB5C30C1?wicket:bookmarkablePage=:com.mycompany.cheese.CheeseMain&Title=Havarti&group=cheeseName

我的问题是我不再希望该链接的 URL 可添加书签。理想情况下,我希望它是像 https://mysite.com/cheese 这样简单的东西,但我可以接受丑陋的 URL。重要的是参数是不可见的。

我应该如何更改生成链接的方式?我研究了 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);

The URL that comes out has the format
https://mysite.com/;jsessionid=B85EE5CB0349CCA2FE37AF76AB5C30C1?wicket:bookmarkablePage=:com.mycompany.cheese.CheeseMain&Title=Havarti&group=cheeseName.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟若柳尘 2024-11-23 08:58:59

仅当页面可添加书签或特定链接可添加书签时,参数才会出现在 URL 中。

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

不过,要实现此功能,您不得调用 super(PageParameters) 构造函数(这样 Page 就没有足够的信息来构建无状态 URL)。

在此示例中,您可以通过两个不同的链接导航到 SecretPage,一个是无状态的、可添加书签的,另一个是有状态的。

SecretPage 也有两个构造函数。接收一个 PageParameters 并调用 super 传递它。另一个直接通过构造函数参数接收值,并且不将其传递给 super (如果它调用 super(new PageParameters().add("message",message)< 注释行中所示,它会自动重定向到可添加书签的

URL

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"));
            }
        });
    }
}

/

<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>

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()));
    }
}

code>,如

<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>

http://host/app/secret,您必须安装它。您可以在您的 WebApplication 类中执行此操作。

此示例

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 一起使用(某些方法和类被重命名,或移动到不同的包),但想法是相同的。

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

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.

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).

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

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:

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:

<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

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

<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>

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

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;
    }
}

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.

慵挽 2024-11-23 08:58:59

您仍然可以使用您选择的 URL 编码策略并将参数封装到继承的类中,例如:

public class Havarti extends CheeseMain {
    public Havarti() {
        super(new PageParameters("Title=Havarti,Group=cheeseName"));
    }
}

然后您可以使用:new BookmarkablePageLink("havarti", Havarti.class)

You could still use your URL encoding strategy of choice and encapsulate your parameters into an inherited class such as:

public class Havarti extends CheeseMain {
    public Havarti() {
        super(new PageParameters("Title=Havarti,Group=cheeseName"));
    }
}

Then you can use: new BookmarkablePageLink<Void>("havarti", Havarti.class).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文