Locked History Actions

Diff for "guice/Manual/Integration/WebandServlets/ServletRegexKeyMapping"

Differences between revisions 1 and 2
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Regular Expressions == 正規表現 ==
Line 3: Line 3:
You can also map servlets (or filters) to URIs using regular expressions: 正規表現を使って、サーブレット(あるいはフィルター)をURLにマップすることもできる。
{{{
    serveRegex("(.)*ajax(.)*").with(MyAjaxServlet.class)
}}}
これを使うと"ajax"というテキストを含むいかなるURIもMyAjaxServletにマップできる、次のような
 * http://www.google.com/ajax.html
 * http://www.google.com/content/ajax/index
 * http://www.google.com/it/is-totally-ajaxy
Line 5: Line 12:
    serveRegex("(.)*ajax(.)*").with(MyAjaxServlet.class) == 初期化パラメータ ==
Line 7: Line 14:
This will map any URI containing the text "ajax" in it to MyAjaxServlet. Such as:

    * http://www.google.com/ajax.html
    * http://www.google.com/content/ajax/index
    * http://www.google.com/it/is-totally-ajaxy

Initialization Parameters

Servlets (and filters) allow you to pass in string values using the <init-param> tag in web.xml. Guice Servlet supports this using a Map<String, String> of name/value pairs. For example, to initialize MyServlet with two parameters coffee="Espresso" and site="google.com" you could write:
web.xml上で<init-param>タグを使うことにより、サーブレット(とフィルタ)に文字列値を渡すことができる。
GuiceサーブレットはこれをMap<String, String>という名前/値ペアでサポートしている。
例えば、MyServletを二つのパラメータcoffee="Espresso"とsite="google.com"で初期化するならこう書く。
{{{
Line 23: Line 24:
}}}
MyServletに渡されるServletConfigオブジェクトは適切な入力パラメータが含まれ、getInitParams()で取得することができる。同様のシンタックスがフィルタにもある。
Line 24: Line 27:
And the ServletConfig object passed to MyServlet will contain the appropriate input parameters when using getInitParams(). The same syntax is available with filters.
Binding Keys
== バインディングキー ==
Line 27: Line 29:
You can also bind keys rather than classes. This lets you hide implementations with package-local visbility and expose them using only a Guice module and an annotation: クラスではなく、キーをバインドすることもできる。
これによりパッケージローカルな可視性の実装を隠し、Guiceモジュールとアノテーションによってだけ使えるように公開することができる。
{{{
      filter("/*").through(Key.get(Filter.class, Fave.class));
}}}
Filter.classとは、サーブレットAPIであるjavax.servlet.Filterであり、Fave.classとはカスタムバインディングアノテーションである。他のどこかで(あなた自身のモジュールの一つの中で)このフィルタを実装にバインドすることができる。
{{{
   bind(Filter.class).annotatedWith(Fave.class).to(MyFilterImpl.class);
}}}
Line 29: Line 39:
      filter("/*").through(Key.get(Filter.class, Fave.class)); バインディングアノテーションについてはユーザガイドを参照のこと
Line 31: Line 41:
Where Filter.class refers to the Servlet API interface javax.servlet.Filter and Fave.class is a custom binding annotation. Elsewhere (in one of your own modules) you can bind this filter's implementation: == インジェクタの注入 ==
Line 33: Line 43:
   bind(Filter.class).annotatedWith(Fave.class).to(MyFilterImpl.class);

See the
User's Guide
for more information on binding and annotations.
Injecting the injector

Once you have a servlet or filter injected for you using ServletModule, you can access the injector at any time by simply injecting it directly into any of your classes:
ServletModuleを使うことにより、インジェクトされたサーブレットあるいはフィルタを取得したら、
あなた自身のクラスに直接それを注入することにより、いつでもインジェクタにアクセスできる。
{{{
Line 50: Line 54:

This is often useful for integrating third party frameworks that need to use the injector themselves.
}}}
これは、インジェクタそれ自体を使う必要のあるサードパーティ製フレームワークに統合するのに便利である。

正規表現

正規表現を使って、サーブレット(あるいはフィルター)をURLにマップすることもできる。

    serveRegex("(.)*ajax(.)*").with(MyAjaxServlet.class)

これを使うと"ajax"というテキストを含むいかなるURIもMyAjaxServletにマップできる、次のような

初期化パラメータ

web.xml上で<init-param>タグを使うことにより、サーブレット(とフィルタ)に文字列値を渡すことができる。 GuiceサーブレットはこれをMap<String, String>という名前/値ペアでサポートしている。 例えば、MyServletを二つのパラメータcoffee="Espresso"とsite="google.com"で初期化するならこう書く。

  Map<String, String> params = new HashMap<String, String>();
  params.put("coffee", "Espresso");
  params.put("site", "google.com");

  ...
      serve("/*").with(MyServlet.class, params)

MyServletに渡されるServletConfigオブジェクトは適切な入力パラメータが含まれ、getInitParams()で取得することができる。同様のシンタックスがフィルタにもある。

バインディングキー

クラスではなく、キーをバインドすることもできる。 これによりパッケージローカルな可視性の実装を隠し、Guiceモジュールとアノテーションによってだけ使えるように公開することができる。

      filter("/*").through(Key.get(Filter.class, Fave.class));

Filter.classとは、サーブレットAPIであるjavax.servlet.Filterであり、Fave.classとはカスタムバインディングアノテーションである。他のどこかで(あなた自身のモジュールの一つの中で)このフィルタを実装にバインドすることができる。

   bind(Filter.class).annotatedWith(Fave.class).to(MyFilterImpl.class);

バインディングアノテーションについてはユーザガイドを参照のこと

インジェクタの注入

ServletModuleを使うことにより、インジェクトされたサーブレットあるいはフィルタを取得したら、 あなた自身のクラスに直接それを注入することにより、いつでもインジェクタにアクセスできる。

@Singleton
public class MyServlet extends HttpServlet {
  @Inject private Injector injector;
  ...
}

// elsewhere in ServletModule
serve("/myurl").with(MyServlet.class);

これは、インジェクタそれ自体を使う必要のあるサードパーティ製フレームワークに統合するのに便利である。