Wicketのhtmlファイルの場所を変更する方法

Wicketでhtmlファイルの場所を変更する方法

Wicketには、同じパッケージディレクトリにあるHTMLファイルとJavaファイルが必要です。 ここでは、javaファイルとhtmlファイルを別のディレクトリに配置する方法を示します。

例えば、

  1. Index.java located at Project /src/main/java/com/example

  2. Index.Html located at Project /src/main/webapp/pages , root context “/” at “/src/main/webapp”.

Wicket in Actionによると

Wicketのデフォルトのリソース検索方法により、開発中にJavaファイルとマークアップファイルがすぐ隣にあるため、それらをすばやく切り替えることができます。 また、このアルゴリズムを使用すると、ユーザーがテンプレートのロード元を構成する必要なく、パッケージコンポーネントをすぐに再利用できます。コンポーネントのクラスがクラスパスで見つかった場合、そのリソースも見つかります。 これは強力なデフォルトであり、カスタムを実装する前によく考えてください。

Wicket 1.3

それでもリソースパスをカスタマイズする場合は、ここでWicket 1.3でそれを行う2つの方法を提供します。

1. Webコンテキストでリソースを見つける

ResourceStreamLocatorクラスを拡張するクラスを作成し、次の関数をオーバーライドします

  1. Locate(クラスクラッツ、ストリングパス)

package com.example;

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クラス

package com.example;

import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.util.file.WebApplicationPath;

public class MyApplication extends WebApplication
{
    public Class getHomePage()
    {
        return Index.class;
    }

    @Override
    protected void init() {

        getResourceSettings().setResourceStreamLocator(new MyOwnStreamLocator());

    }

}

2. ResourceFinderでリソースを見つけます

ResourceStreamLocatorクラスを拡張するクラスを作成し、次の2つの関数をオーバーライドします

  1. Locate(クラスクラズ、文字列パス、文字列スタイル、ロケールロケール、文字列拡張子)

  2. LocateByResourceFinder(クラスクラス、文字列パス)

package com.example;

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クラス

package com.example;

import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.util.file.WebApplicationPath;

public class MyApplication extends WebApplication
{
    public Class 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);
    }
}

完了しました。

ダウンロード–Wicket-1.3-Resource-Loading.zip(7KB)

改札1.4

Wicket 1.4では、より簡単です。 次のディレクトリ構造を参照してください。

  1. Hello.java in /src/main/java/com/example/hello

  2. Hello.html in /src/main/webapp/pages/com/example/hello

wicket change html location

次のように、htmlがinit()メソッドでロードされる場所をカスタマイズできます。

package com.example;

import org.apache.wicket.Page;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.settings.IResourceSettings;
import com.example.hello.Hello;

public class MyApplication extends WebApplication {

    @Override
    public Class getHomePage() {
        return Hello.class; //return default page
    }

    @Override
    protected void init() {
        super.init();

        IResourceSettings resourceSettings = getResourceSettings();
                resourceSettings.addResourceFolder("pages");

    }
}

ダウンロード–Wicket1.4-resource-loading.zip(8KB)

Maven ways
または、次のようにMavenリソースタグを介してHTMLファイルをロードする場所を制御できます。

ファイル:pom.xml


    
        WicketExamples

        
            
                src/main/resources
            
            
                src/main/webapp/pages
            
        

    

Mavenの方法では、WebApplicationinit()メソッドをオーバーライドする必要はありません。