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,

  1. Index.java located at Project /src/main/java/com/mkyong
  2. 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

  1. 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

  1. locate(Class clazz, String path, String style,Locale locale, String extension)
  2. 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.

Download It – Wicket-1.3-Resource-Loading.zip (7KB)

Wicket 1.4

For Wicket 1.4, it is more simple. See following directory structure :

  1. Hello.java in /src/main/java/com/mkyong/hello
  2. Hello.html in /src/main/webapp/pages/com/mkyong/hello
wicket change html location

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");
 
	}
}
Download It – Wicket1.4-resource-loading.zip (8KB)
Maven ways
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.

Reference

  1. Wicket – Control where HTML files are loaded from
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at Apache Wicket Tutorials