As we all know Wicket require html and java file located at the same directory. Is there a way to separate the java and html file into different directory? Can i control where html file located? Can i put the html pages in a diferent location of the .class?

For example
Index.java located at Project/src/main/java/com/mkyong
Index.Html located at Project/WEB-INF/pages

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

If you still insist to customize the resource path, here i provide 2 mechanism to locate the index.html from different class directory like Project/WEB-INF/pages.

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 = "/WEB-INF/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 function

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("/WEB-INF/pages/");
		resourceStreamLocator.addFinder(webContainerPathFinder);
 
		getResourceSettings().setResourceStreamLocator(resourceStreamLocator);
	}
}

Done.

Download Source Code Here

This article was posted in Wicket category.