開発者ドキュメント

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

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

例えば、

  1. プロジェクトにあるIndex.java

    /src/main/java/com/mkyong

  2. Index.HtmlはProject

    /src/main/webapp/pages

    にあり、ルートコンテキスト

“/”/”/src/main/webapp”にあります。


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

あなたがまだリソースパスをカスタマイズすることを主張しているなら、ここで私はWicket 1.3でそれを行う2つの方法を提供します。

1. Webコンテキストでリソースを探す

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アプリケーションクラス

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. ResourceFinderでリソースを検索する

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

    1. locate(クラス>クラス、ストリングパス、ストリングスタイル、ロケールロケール、ストリング

拡張)
。 locateByResourceFinder(クラス>クラス、ストリングパス)

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アプリケーションクラス

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);
    }
}

完了しました。

ダウンロード – リンク://wp-content/uploads/2009/02/resource-loading.zip[Wicket-1.3-Resource-Loading.zip](7KB)

Wicket 1.4

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

  1. /src/main/java/com/mkyong/helloのHello.java

  2. Hello.html/src/main/webapp/pages/com/mkyong/hello



htmlが次のように

init()

メソッドでロードされるカスタムは可能です:

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");

    }
}

ダウンロード – リンク://wp-content/uploads/2009/02/Wicket1.4-resource-loading.zip[Wicket1.4-resource-loading.zip](8KB)

  • Mavenの方法** また、Mavenのリソースタグを使ってHTMLファイルをロードする場所を次のように制御できます。

ファイル: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>

Mavenの方法では、Webアプリケーション

init()

メソッドをオーバーライドする必要はありません。

モバイルバージョンを終了