How to change the html file location in Wicket
Wicket requires html and java files located at the same package directory. Here we show you how to puts the java and html file into different directory.
For example,
- Index.java located at Project /src/main/java/com/mkyong
- Index.Html located at Project /src/main/webapp/pages , root context “/” at “/src/main/webapp”.
According to Wicket in Action
Wicket’s default way of locating resources enable you to quickly switch between the Java files and markup files during development because they’re right next to each other. Also, with this algorithm, your package components are immediately reusable without users having to configure where the templates are loaded from; if the components’ class can be found in the class path, so can their resources. It’s powerful default, and you may want to think twice before you implement something custom
Wicket 1.3
If you still insist to customize the resource path, here i provides 2 ways to do it in Wicket 1.3.
1. Locate the resource with web context
Create a class which extend the ResourceStreamLocator class, and override the following function
- locate(Class> clazz, String path)
package com.mkyong; import java.net.MalformedURLException; import java.net.URL; import org.apache.wicket.WicketRuntimeException; import org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.util.resource.IResourceStream; import org.apache.wicket.util.resource.UrlResourceStream; import org.apache.wicket.util.resource.locator.ResourceStreamLocator; import org.apache.wicket.util.string.Strings; public class MyOwnStreamLocator extends ResourceStreamLocator { @Override public IResourceStream locate(Class<?> clazz, String path) { String location; String extension = path.substring(path.lastIndexOf('.') + 1); String simpleFileName = Strings.lastPathComponent(clazz.getName(), '.'); location = "/pages/" + simpleFileName + "." + extension; URL url; try { // try to load the resource from the web context url = WebApplication.get().getServletContext().getResource(location); if (url != null) { return new UrlResourceStream(url); } } catch (MalformedURLException e) { throw new WicketRuntimeException(e); } // resource not found; fall back on class loading return super.locate(clazz, path); } }
Wicket Application class
package com.mkyong; import org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.util.file.WebApplicationPath; public class MyApplication extends WebApplication { public Class<Index> getHomePage() { return Index.class; } @Override protected void init() { getResourceSettings().setResourceStreamLocator(new MyOwnStreamLocator()); } }
2. Locate the resource with ResourceFinder
Create a class which extend the ResourceStreamLocator class and override the following two functions
- locate(Class> clazz, String path, String style,Locale locale, String extension)
- locateByResourceFinder(Class> clazz, String path)
package com.mkyong; import java.util.Locale; import org.apache.wicket.util.file.IResourceFinder; import org.apache.wicket.util.resource.IResourceStream; import org.apache.wicket.util.resource.locator.ResourceStreamLocator; import org.apache.wicket.util.string.Strings; public class MyOwnFinderStreamLocator extends ResourceStreamLocator { private IResourceFinder resourceFinder; public void addFinder(IResourceFinder resourceFinder) { if (resourceFinder != null) { this.resourceFinder = resourceFinder; } } @Override public IResourceStream locate(Class<?> clazz, String path, String style, Locale locale, String extension) { String simpleFileName = Strings.lastPathComponent(clazz.getName(), '.') + "." + extension; IResourceStream stream = locate(clazz, simpleFileName); if(stream!=null) return stream; else return super.locate(clazz, path,style,locale,extension); } @Override protected IResourceStream locateByResourceFinder(Class<?> clazz, String path) { IResourceStream resourceStream = null; resourceStream = resourceFinder.find(clazz, path); if (resourceStream == null) { // try by using class loader resourceStream = locateByClassLoader(clazz, path); } return resourceStream; } }
Wicket Application class
package com.mkyong; import org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.util.file.WebApplicationPath; public class MyApplication extends WebApplication { public Class<Index> getHomePage() { return Index.class; } @Override protected void init() { //resource finder MyOwnFinderStreamLocator resourceStreamLocator = new MyOwnFinderStreamLocator(); WebApplicationPath webContainerPathFinder = new WebApplicationPath(getServletContext()); webContainerPathFinder.add("/pages/"); resourceStreamLocator.addFinder(webContainerPathFinder); getResourceSettings().setResourceStreamLocator(resourceStreamLocator); } }
Done.
Wicket 1.4
For Wicket 1.4, it is more simple. See following directory structure :
- Hello.java in /src/main/java/com/mkyong/hello
- Hello.html in /src/main/webapp/pages/com/mkyong/hello

You can custom where the html is loaded in init() method like this :
package com.mkyong; import org.apache.wicket.Page; import org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.settings.IResourceSettings; import com.mkyong.hello.Hello; public class MyApplication extends WebApplication { @Override public Class<? extends Page> getHomePage() { return Hello.class; //return default page } @Override protected void init() { super.init(); IResourceSettings resourceSettings = getResourceSettings(); resourceSettings.addResourceFolder("pages"); } }
Alternatively, you can control where the HTML files is loaded via Maven resource tag like this :
File : pom.xml
<project ...> <build> <finalName>WicketExamples</finalName> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/webapp/pages</directory> </resource> </resources> </build> </project>
In Maven way, you do not need to override the WebApplication init() method.
It’s really good work done.
Nevermind, it worked perfectly after I moved all the html files. Thanks.
It does not seem to work for me. I have tried the maven way and the init way. Both ways seem to get me into these errors:
org.apache.wicket.markup.MarkupNotFoundException: Can not determine Markup. Component is not yet connected to a parent. [Page class = com.deltalloyd.SEPA.testImplementatie.SimplePage, id = 11, render count = 1] at org.apache.wicket.Component.getMarkup(Component.java:728) at org.apache.wicket.Component.internalRender(Component.java:2379) at org.apache.wicket.Component.render(Component.java:2342) at org.apache.wicket.Page.renderPage(Page.java:1060) at org.apache.wicket.request.handler.render.WebPageRenderer.renderPage(WebPageRenderer.java:105)
etc.
etc.
[...] all files “.html” and “.java” in a same package directory. Note Read this control where HTML is loaded in Wicket article to learn how to separate the “.html” and “.java” in different [...]
When using the above method with markup inheritance, seems like Wicket is unable to locate the base markup page.
Nice post,
why should we reite the code on init? can’t we just write it in load
Thanks
We need to specify the html file location which initialize the Wicket application. Not really sure which load method u refer to ?