Started learning Wicket after ASP.NET MVC and feel a little bit confused about managing its URLs. Here’s the code:
Application:
package com.test.wicketapp1;
import org.apache.wicket.protocol.http.WebApplication;
public class WicketApplication extends WebApplication {
public WicketApplication() {
mountPage("/page1", HomePage.class);
mountPage("/page2", Page2.class);
}
@Override public Class<HomePage> getHomePage() {
return HomePage.class;
}
}
HomePage:
package com.test.wicketapp1;
import java.io.IOException;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.WebPage;
public class HomePage extends WebPage {
private static final long serialVersionUID = 1L;
public HomePage(final PageParameters parameters) throws IOException {
BookmarkablePageLink<Page2> bookmarkablePageLink = new BookmarkablePageLink<Page2>("gopage2link", Page2.class);
add(bookmarkablePageLink);
}
}
HomePage markup:
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<title>Apache Wicket Quickstart</title>
</head>
<body>
<a href="#" wicket:id="gopage2link">go page 2</a>
</body>
</html>
What I wanted to have is pretty simple. I expected that there would be 2 urls: “/page1” for HomePage.class and “/page2” for Page2.class, then my HomePage has a link that navigates to Page2 and when HomePage is rendered, that link should have an URL of “/page2”.
When I run the application and go to home page, it is rendered like this:
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<title>Apache Wicket Quickstart</title>
</head>
<body>
<a href="./wicket/bookmarkable/com.test.wicketapp1.Page2" wicket:id="gopage2link">go page 2</a>
</body>
</html>
I expected to have something like:
<a href="/page2" wicket:id="gopage2link">go page 2</a>
instead. What did I miss?
The problem is – I should use app’s
initmethod instead of ctor to define the mappings.