Java – how to create a wicket URL that hides its parameters?

I am currently creating a set of links with the following code:

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

The URL is formatted https://mysite.com/ ; jsessionid=B85EE5CB0349CCA2FE37AF76AB5C30C1? wicket:bookmarkablePage=:com. mycompany. cheese. CheeseMain\u0026amp; Title=Havarti\u0026amp; group=cheeseName.

My problem is that I no longer want the URL of this link to be collectable Ideally, I want it to be like https://mysite.com/cheese As simple, but I can use ugly URLs It is important that the parameters are not visible

How can I change the way I generate links? I checked the different URL encoding strategies provided by wicket, but there is no deletion parameter; They just show it in different ways

Solution

Parameters are displayed in the URL only if the page is bookmarkable or a specific link is favorite

If you create a link that navigates to a page using setresponsepage (passing page instance) instead of setresponsepage (class < Page >, pageparameters) (passing page class), the created link will not point to a collectable version page, but a stateful instance

However, to make it work, you cannot call the super (page parameters) constructor (so that the page does not have enough information to build a stateless URL)

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

Secretpage also has two constructors A person receives a pageparameters and calls super to pass it The other receives the value directly through the constructor parameter and does not pass it to super (if it is called super (New pageparameters()) Add ("message", message), e.g. in the comment line, it will automatically redirect to the favorite 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>

Also, to have a simple URL, such as http: / / host / APP / secret, you must install it You can do this in the 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 needs some modifications to use 1.4 X (some methods and classes are renamed or moved to different packages), but the idea is the same

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>