1
votes

My final goal is to generate a go back button in my wicket site forms.

Right now I'm able to get the referrer with:

HttpServletRequest req = (HttpServletRequest)getRequest().getContainerRequest();
l.info("referer: {}", req.getHeader("referer"));

This works and I get the whole URL (as a String) but I'm unable to generate a Link object from this.

I'm not sure about the internals although I've been seeing the code for Application.addMount, IRequestHandler and more, I'm not able to find exactly where a URL is converted to what I need to generate a BookmarkablePageLink: the Class and the PageParameters.

P.S. I know this can be done with JavaScript, but I want to serve users without JS active.

Thanks

Possible solution I'm currently using:

public static WebMarkupContainer getBackButton(org.apache.wicket.request.Request request, String id) {
    WebMarkupContainer l = new WebMarkupContainer(id);
    HttpServletRequest req = (HttpServletRequest)request.getContainerRequest();
    l.add(AttributeModifier.append("href", req.getHeader("referer")));
    return l;
}

In my markup I have:

<a wicket:id="backButton">Back</a>

And then, in my Page object:

add(WicketUtils.getBackButton(getRequest(), "backButton");

If anyone has any better idea, I'm leaving this open for a while.

1

1 Answers

1
votes

You should be able to use an ExternalLink for this.

Something resembling

public Component getBackButton(org.apache.wicket.request.Request request, String id) {
    HttpServletRequest req = (HttpServletRequest)request.getContainerRequest();
    String url = req.getHeader("referer");
    return new ExternalLink(id, url, "Back");
}

with html

<a href="#" wicket:id="backButton">this body will be replaced</a>

And your Page object code unchanged.