Description
From the documentation here:
https://ci.apache.org/projects/wicket/guide/6.x/guide/urls.html
I'm following the section titled, "Using parameter placeholders with mounted pages".
In the documentation from here:
https://cwiki.apache.org/confluence/display/WICKET/Request+mapping
Named parameters - /page/${named1}/${named2} mountPage("/page/${named1}/${named2}", MyPage.class); Now a request to "/page/a/b" will be handled by MyPage and the parameters can be get with PageParameters.get(String) (e.g. parameters.get("named1") will return "a") Optional named parameters - /page/${named1}/#{named2\ mountPage("/page/${named1}/#{named2}", MyPage.class); This means the second parameter is optional. Requests to "/page/a/b" , "/page/a/b/" and "/page/a/" will be handled by MyPage and the parameters can be get with PageParameters.get(String) (e.g. parameters.get("named2") will return "b" for the first case and null for the second). The mapper is smart enough to handle optional named parameters in any segment, not just the last one.
it states: "The mapper is smart enough to handle optional named parameters in any segment, not just the last one."
I've found it does matter IF other segments in the url are also parameter placeholders. The resulting behaviour is that if the optional parameter placeholder isn't provided in the URL, the page's default constructor gets called instead of the PageParameter constructor as expected (since there are still additional PageParameter data that may need to be processed by the page).
Here is my example:
From child class of AdtAuthenticatedWebApplication
@Override protected void init() { super.init(); mountPage("/foo/#{optParam}/${otherParam}", MyPage.class); //mountPage("/foo/${otherParam}/#{optParam}", MyPage.class); <--- NOTE: This works as expected }
In child class of WebPage
public class MyPage extends WebPage { public MyPage(PageParameters parameters) { super(parameters); StringValue optParam = parameters.get("optParam"); StringValue otherParam = parameters.get("otherParam"); //do something... } private MyPage() { throw new IllegalAccessError("The default constructor should not be instantiated"); }
And in a different class:
public static BookmarkablePageLink getBookmarkableLink(String id, String optParam, String otherParam) { PageParameters pageParam = new PageParameters(); pageParam.add("otherParam", otherParam); if (optParam != null && !optParam.isEmpty()) { pageParam.add("optParam", optParam); } return new BookmarkablePageLink(id, MyPage.class, pageParam); }
Here are the result of clicking on the BookmarkablePageLinks created:
/foo/baz/bar -- calls MyPage(PageParameters parameters) constructor
/foo/bar -- attempts to call MyPage default constructor (exception thrown)
Exception thrown is:
Last cause: Class org.apache.wicket.session.DefaultPageFactory can not access a member of class ca.example.for.testing.MyPage with modifiers "private" WicketMessage: Can't instantiate page using constructor 'private ca.example.for.testing.MyPage()'. This constructor is private!