Locked History Actions

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

Differences between revisions 1 and 2
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Installing a Servlet Module == サーブレットモジュールのインストール ==
Line 3: Line 3:
Once you have GuiceFilter up and running, Guice Servlet is set up. However, you will need to install an instance of ServletModule in order to get real use out of Guice Servlet: GuiceFilterを設定して起動すればGuiceサーブレットがセットアップされるが、
しかしGuiceサーブレットを実際に使うためにはServletModuleインスタンスのインストールが必要である。
{{{
   Guice.createInjector(new ServletModule());
}}}
このモジュールはリクエスト及びセッションスコープをセットアップし、フィルターとサーブレットをコンフィギュレーションするための方法を提供する。
インジェクタを作成する場所はいかなるところでも構わないのだが、論理的な場所としてServletContextListenerがある。
Line 5: Line 11:
   Guice.createInjector(new ServletModule()); ServletContextListenerはウェブアプリケーションが配備された直後、リクエストが到着する以前にトリガーされるJavaサーブレットである。
Guiceサーブレットはサブクラス化するための便利なユーティリティを提供しているの、あなた自身のServletContextListenerを作成することができる。
Line 7: Line 14:
This module sets up the request and session scopes, and provides a place to configure your filters and servlets from. While you are free to create the injector from any place of your choice, a logical place to do it in is a ServletContextListener.

A ServletContextListener is a Java servlet component that is triggered as soon as a web application is deployed, and before any requests begin to arrive. Guice Servlet provides a convenience utility that you can subclass in order to register your own ServletContextListeners:
{{{
Line 18: Line 22:
}}}
Line 19: Line 24:
Next, add the following to web.xml so the servlet container triggers this class when the app is deployed:
次に、以下のようなweb.xmlを追加し、サーブレットコンテナがアプリケーション配備時にこのクラスをトリガーできるようにする。
{{{
Line 24: Line 29:
}}}
これで、必要に応じてGuiceサーブレットを使えるようになった。
ただし、必ずしもGuiceサーブっトを使うために、ServletContextListenerは必要ではない、
インジェクタ生成時にServletModuleのインストールを忘れさえしなければ。
Line 25: Line 34:
You can now use Guice Servlet as per your needs. Note that it is not necessary to use a ServletContextListener to use Guice Servlet, as long as you remember to install ServletModule when creating your injector.
The Binding Language
== バインディング言語 ==
Line 28: Line 36:
Think of the ServletModule as an in-code replacement for the web.xml deployment descriptor. Filters and servlets are configured here using normal Java method calls. Here is a typical example of registering a servlet when creating your Guice injector:
ServletModuleをweb.xml配備デスクリプタのコードによる置き換えと考えてみよう。
フィルタとサーブレットは通常のJavaメソッド呼び出しによってコンフィギュレーションされる。
以下はGuiceインジェクタを作成する際にサーブレットを登録する典型的な例である。
{{{
Line 37: Line 47:
}}}
これは.htmlで終了するすべてのウェブリクエストをサービスするためのMyServletという名のサーブレット(HttpServletのサブクラス)を登録するものである。
{{{
       serve("/my/*").with(MyServlet.class)
}}}
Line 38: Line 53:
This registers a servlet (subclass of HttpServlet) called MyServlet to serve any web requests ending in .html. You can also use a path-style syntax to register servlets as you would in web.xml: == フィルタマッピング ==
Line 40: Line 55:
       serve("/my/*").with(MyServlet.class) サーブレットフィルタを同様のシンタックスでマップすることができる。
{{{
      filter("/*").through(MyFilter.class);
}}}
Line 42: Line 60:
Filter Mapping これはMyFilterを通して到着するすべてのリクエストをルーティングし、
他のマッチするフィルタを通し、最終的に処理サーブレットに配布する。
Line 44: Line 63:
You may also map Servlet Filters using a very similar syntax: 注意:すべてのサーブレット(あるいはフィルタ)は@Singletonでなければならない。
もしこれらのクラスに直鉄的にアノテーションできない場合は、bind(..).in(Singleton.class)でバインドすること、
separate to the filter() or servlet() rules.
他のスコープへのマッピングはエラーとなる。
これはサーブレット仕様の一貫性保持のためである。
Guiceサーブレットは古いSingleThreadModelはサポートしていない。
Line 46: Line 70:
      filter("/*").through(MyFilter.class);
Line 48: Line 71:
This will route every incoming request through MyFilter, and then continue to any other matching filters before finally being dispatched to a servlet for processing. == 可能な注入 ==
Line 50: Line 73:
Note: Every servlet (or filter) is required to be a @Singleton. If you cannot annotate the class directly, you must bind it using bind(..).in(Singleton.class), separate to the filter() or servlet() rules. Mapping under any other scope is an error. This is to maintain consistency with the Servlet specification. Guice Servlet does not support the deprecated SingleThreadModel.
Available Injections
サーブレットモジュールをインストールすることにより、サーブレットフレームワークから様々なクラスにアクセスが可能となる。
ServletModuleをインストールすると、サーブレットプログラミングモデルで便利なものが得られるし、デフォルトでGuiceが注入するオブジェクトが得られる。
Line 53: Line 76:
Installing the servlet module automatically gives you access to several classes from the servlet framework. These are useful for working with the servlet programming model and are injectable in any Guice injected object by default, when you install the
ServletModule
:
{{{
Line 66: Line 86:
}}}
Line 68: Line 89:
{{{
Line 70: Line 91:
}}}
Line 75: Line 96:
{{{
Line 89: Line 110:
}}}
Line 91: Line 112:
{{{
Line 93: Line 114:
}}}
Line 95: Line 116:
{{{
Line 97: Line 118:
}}}
Line 102: Line 123:
{{{
Line 104: Line 125:
}}}

サーブレットモジュールのインストール

GuiceFilterを設定して起動すればGuiceサーブレットがセットアップされるが、 しかしGuiceサーブレットを実際に使うためにはServletModuleインスタンスのインストールが必要である。

   Guice.createInjector(new ServletModule());

このモジュールはリクエスト及びセッションスコープをセットアップし、フィルターとサーブレットをコンフィギュレーションするための方法を提供する。 インジェクタを作成する場所はいかなるところでも構わないのだが、論理的な場所としてServletContextListenerがある。

ServletContextListenerはウェブアプリケーションが配備された直後、リクエストが到着する以前にトリガーされるJavaサーブレットである。 Guiceサーブレットはサブクラス化するための便利なユーティリティを提供しているの、あなた自身のServletContextListenerを作成することができる。

public class MyGuiceServletConfig extends GuiceServletContextListener {

  @Override
  protected Injector getInjector() {
    return Guice.createInjector(new ServletModule());
  }
}

次に、以下のようなweb.xmlを追加し、サーブレットコンテナがアプリケーション配備時にこのクラスをトリガーできるようにする。

<listener>
  <listener-class>com.example.MyGuiceServletConfig</listener-class>
</listener>

これで、必要に応じてGuiceサーブレットを使えるようになった。 ただし、必ずしもGuiceサーブっトを使うために、ServletContextListenerは必要ではない、 インジェクタ生成時にServletModuleのインストールを忘れさえしなければ。

バインディング言語

ServletModuleをweb.xml配備デスクリプタのコードによる置き換えと考えてみよう。 フィルタとサーブレットは通常のJavaメソッド呼び出しによってコンフィギュレーションされる。 以下はGuiceインジェクタを作成する際にサーブレットを登録する典型的な例である。

   Guice.createInjector(..., new ServletModule() {

     @Override
     protected void configureServlets() {
       serve("*.html").with(MyServlet.class)
     }
   }

これは.htmlで終了するすべてのウェブリクエストをサービスするためのMyServletという名のサーブレット(HttpServletのサブクラス)を登録するものである。

       serve("/my/*").with(MyServlet.class)

フィルタマッピング

サーブレットフィルタを同様のシンタックスでマップすることができる。

      filter("/*").through(MyFilter.class);

これはMyFilterを通して到着するすべてのリクエストをルーティングし、 他のマッチするフィルタを通し、最終的に処理サーブレットに配布する。

注意:すべてのサーブレット(あるいはフィルタ)は@Singletonでなければならない。 もしこれらのクラスに直鉄的にアノテーションできない場合は、bind(..).in(Singleton.class)でバインドすること、 separate to the filter() or servlet() rules. 他のスコープへのマッピングはエラーとなる。 これはサーブレット仕様の一貫性保持のためである。 Guiceサーブレットは古いSingleThreadModelはサポートしていない。

可能な注入

サーブレットモジュールをインストールすることにより、サーブレットフレームワークから様々なクラスにアクセスが可能となる。 ServletModuleをインストールすると、サーブレットプログラミングモデルで便利なものが得られるし、デフォルトでGuiceが注入するオブジェクトが得られる。

{{{ @RequestScoped class SomeNonServletPojo {

} }}}

The request and response are scoped to the current http request. Similarly the http session object is scoped to the current user session. In addition to this you may also inject the current ServletContext and a map of the request parameters using the binding annotation @RequestParameters as follows:

@Inject @RequestParameters Map<String, String[]> params;

This must be a map of strings to arrays of strings because http allows multiple values to be bound to the same request parameter, though typically there is only one. Note that if you want access to any of the request or session scoped classes from singletons or other wider-scoped instances, you should inject a Provider<T> instead. Dispatch Order

You are free to register as many servlets and filters as you like this way. They will be compared and dispatched in the order in which the rules appear in your ServletModule:

   Guice.createInjector(..., new ServletModule() {

     @Override
     protected void configureServlets() {
       filter("/*").through(MyFilter.class);
       filter("*.css").through(MyCssFilter.class);
       // etc..

       serve("*.html").with(MyServlet.class);
       serve("/my/*").with(MyServlet.class);
       // etc..
      }
    }

This will traverse down the list of rules in lexical order. For example, a url /my/file.js will first be compared against the servlet mapping:

       serve("*.html").with(MyServlet.class);

And failing that, it will descend to the next servlet mapping:

       serve("/my/*").with(MyServlet.class);

Since this rule matches, Guice Servlet will dispatch the request to MyServlet and stop the matching process. The same process is used when deciding the dispatch order of filters (that is, matched to their order of appearance in the binding list). Varargs Mapping

The two mapping rules above can also be written in more compact form using varargs syntax:

       serve("*.html", "/my/*").with(MyServlet.class);

This way you can map several URI patterns to the same servlet. A similar syntax is also available for filter mappings.