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,

  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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
9 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
7 Comment authors
krzst@o2.plnausheenfatmaSujit KirveMarcelWicket Hello World Example Recent comment authors
newest oldest most voted
krzst@o2.pl
Guest
krzst@o2.pl

Any idea how can we do it in Wicket 6.4 ?

krzst@o2.pl
Guest
krzst@o2.pl

I found quick solution:

IResourceSettings resourceSettings = getResourceSettings();
IResourceFinder pagesResources = new WebApplicationPath(getServletContext(), "/pages/");
resourceSettings.getResourceFinders().add(pagesResources);

Now pages file can be located in: “src/main/webapp/pages” directory.

Cheers

nausheenfatma
Guest
nausheenfatma

Thanks alot sir,Its working perfectly !

nausheenfatma
Guest
nausheenfatma

Thanks a lot sir,its working perfectly !!

Sujit Kirve
Guest
Sujit Kirve

It’s really good work done.

Marcel
Guest
Marcel

Nevermind, it worked perfectly after I moved all the html files. Thanks.

Marcel
Guest
Marcel

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.

trackback
Wicket Hello World Example

[…] 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 […]

who
Guest
who

When using the above method with markup inheritance, seems like Wicket is unable to locate the base markup page.

software development company
Guest
software development company

Nice post,

why should we reite the code on init? can’t we just write it in load

Thanks