|
1 | 1 | <!--- Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com> -->
|
| 2 | +<!-- |
2 | 3 | # HTTP routing
|
| 4 | +--> |
| 5 | +# HTTPルーティング |
3 | 6 |
|
| 7 | +<!-- |
4 | 8 | ## The built-in HTTP router
|
| 9 | +--> |
| 10 | +## 組み込み HTTP ルータ |
5 | 11 |
|
| 12 | +<!-- |
6 | 13 | The router is the component that translates each incoming HTTP request to an action call (a public method in a controller class).
|
| 14 | +--> |
| 15 | +ルータはクライアントから受け取った HTTP リクエストをアクション (コントローラクラス内の public なメソッド) の呼び出しへ変換するコンポーネントです。 |
7 | 16 |
|
| 17 | +<!-- |
8 | 18 | An HTTP request is seen as an event by the MVC framework. This event contains two major pieces of information:
|
| 19 | +--> |
| 20 | +HTTP リクエストは MVC フレームワークにとってイベントであるといえます。このイベントには大きく分けて次の二つの情報が含まれています。 |
9 | 21 |
|
| 22 | +<!-- |
10 | 23 | - the request path (such as `/clients/1542`, `/photos/list`), including the query string.
|
11 | 24 | - the HTTP method (GET, POST, ...).
|
| 25 | +--> |
| 26 | +- クエリストリングを含むリクエストパス (例えば、`/clients/1542` や `/photos/list`) |
| 27 | +- HTTP メソッド (GET、POST など) |
12 | 28 |
|
| 29 | +<!-- |
13 | 30 | Routes are defined in the `conf/routes` file, which is compiled. This means that you’ll see route errors directly in your browser:
|
| 31 | +--> |
| 32 | +ルートは `conf/routes` ファイルに定義しておくことでコンパイルされます。これは、ルート定義に関するエラーもブラウザで直接的に確認できるということです。 |
14 | 33 |
|
15 | 34 | [[images/routesError.png]]
|
16 | 35 |
|
| 36 | +<!-- |
17 | 37 | ## Dependency Injection
|
| 38 | +--> |
| 39 | +## 依存性の注入 |
18 | 40 |
|
| 41 | +<!-- |
19 | 42 | Play supports generating two types of routers, one is a dependency injected router, the other is a static router. The default is the static router, but if you created a new Play application using the Play seed Activator templates, your project will include the following configuration in `build.sbt` telling it to use the injected router:
|
| 43 | +--> |
| 44 | +Play は、2 種類のルータ生成をサポートしています。1 つは依存性が注入されたルータ、もう 1 つは静的なルータです。デフォルトは静的ルータですが、もし Play seed Activator テンプレートを使用して新しい Play アプリケーションを作成した場合、プロジェクトには、注入されたルータを使用するように指示するため `build.sbt` に以下の設定が含まれます。 |
20 | 45 |
|
21 | 46 | ```scala
|
22 | 47 | routesGenerator := InjectedRoutesGenerator
|
23 | 48 | ```
|
24 | 49 |
|
| 50 | +<!-- |
25 | 51 | The code samples in Play's documentation assumes that you are using the injected routes generator. If you are not using this, you can trivially adapt the code samples for the static routes generator, either by prefixing the controller invocation part of the route with an `@` symbol, or by declaring each of your action methods as `static`.
|
| 52 | +--> |
| 53 | +Play のドキュメントにあるコードサンプルは、注入されたルータ生成を使用することを前提としています。もしこれを使用しない場合は、コントローラの呼び出し部分の前に`@`シンボルを付けるか、それぞれのアクションメソッドを `static` として宣言することで、静的なルータ生成用のコードサンプルに作り変えることができます。 |
26 | 54 |
|
| 55 | +<!-- |
27 | 56 | ## The routes file syntax
|
| 57 | +--> |
| 58 | +## routesファイルの文法 |
28 | 59 |
|
| 60 | +<!-- |
29 | 61 | `conf/routes` is the configuration file used by the router. This file lists all of the routes needed by the application. Each route consists of an HTTP method and URI pattern associated with a call to an action method.
|
| 62 | +--> |
| 63 | +`conf/routes` はルータによって読み込まれる設定ファイルです。このファイルには、アプリケーションが必要とする全てのルートをリストアップします。それぞれのルートは、HTTP メソッドと URI パターン、そしてそれらに割り当てられたアクションメソッドの呼び出しで表します。 |
30 | 64 |
|
| 65 | +<!-- |
31 | 66 | Let’s see what a route definition looks like:
|
| 67 | +--> |
| 68 | +実際のルート定義を見てみましょう。 |
32 | 69 |
|
33 | 70 | @[clients-show](code/javaguide.http.routing.routes)
|
34 | 71 |
|
| 72 | +<!-- |
35 | 73 | > Note that in the action call, the parameter type comes after the parameter name, like in Scala.
|
| 74 | +--> |
| 75 | +> アクション呼び出しでは、Scala のように引数名の後に型を指定する事に注意して下さい。 |
36 | 76 |
|
| 77 | +<!-- |
37 | 78 | Each route starts with the HTTP method, followed by the URI pattern. The last element of a route is the call definition.
|
| 79 | +--> |
| 80 | +それぞれのルートでは、先頭に HTTP メソッド、その後に URI パターンが続きます。最後がアクション呼び出しの定義です。 |
38 | 81 |
|
| 82 | +<!-- |
39 | 83 | You can also add comments to the route file, with the `#` character:
|
| 84 | +--> |
| 85 | +`#` の文字を使って、routes ファイルにコメントを残すこともできます。 |
40 | 86 |
|
41 | 87 | @[clients-show-comment](code/javaguide.http.routing.routes)
|
42 | 88 |
|
| 89 | +<!-- |
43 | 90 | ## The HTTP method
|
| 91 | +--> |
| 92 | +## HTTPメソッド |
44 | 93 |
|
| 94 | +<!-- |
45 | 95 | The HTTP method can be any of the valid methods supported by HTTP (`GET`, `PATCH`, `POST`, `PUT`, `DELETE`, `HEAD`).
|
| 96 | +--> |
| 97 | +HTTP メソッドには、HTTP がサポートするあらゆるメソッド(`GET`、`PATCH`、`POST`、`PUT`、`DELETE`、`HEAD`)が指定できます。 |
46 | 98 |
|
| 99 | +<!-- |
47 | 100 | ## The URI pattern
|
| 101 | +--> |
| 102 | +## URIパターン |
48 | 103 |
|
| 104 | +<!-- |
49 | 105 | The URI pattern defines the route’s request path. Some parts of the request path can be dynamic.
|
| 106 | +--> |
| 107 | +URI パターンはルートのリクエストパスの定義です。リクエストパスの一部を動的にすることができます。 |
50 | 108 |
|
| 109 | +<!-- |
51 | 110 | ### Static path
|
| 111 | +--> |
| 112 | +### 静的パス |
52 | 113 |
|
| 114 | +<!-- |
53 | 115 | For example, to exactly match `GET /clients/all` incoming requests, you can define this route:
|
| 116 | +--> |
| 117 | +例えば、リクエストを `GET /clients/all` に完全一致させたいときは、次のように定義できます。 |
54 | 118 |
|
55 | 119 | @[static-path](code/javaguide.http.routing.routes)
|
56 | 120 |
|
| 121 | +<!-- |
57 | 122 | ### Dynamic parts
|
| 123 | +--> |
| 124 | +### 動的パート |
58 | 125 |
|
| 126 | +<!-- |
59 | 127 | If you want to define a route that, say, retrieves a client by id, you need to add a dynamic part:
|
| 128 | +--> |
| 129 | +しかし、URL からクライアント ID を取得するような場合には、動的パートを追加する必要があります。 |
60 | 130 |
|
61 | 131 | @[clients-show](code/javaguide.http.routing.routes)
|
62 | 132 |
|
| 133 | +<!-- |
63 | 134 | > Note that a URI pattern may have more than one dynamic part.
|
| 135 | +--> |
| 136 | +> 1 つの URI パターンには、2 つ以上の動的パートを含められます |
64 | 137 |
|
| 138 | +<!-- |
65 | 139 | The default matching strategy for a dynamic part is defined by the regular expression `[^/]+`, meaning that any dynamic part defined as `:id` will match exactly one URI path segment.
|
| 140 | +--> |
| 141 | +動的パートのデフォルトのマッチ規則は正規表現でいうと `[^/]+` です。したがって、`:id` という動的パートはちょうど一つの URI パートにマッチします。 |
66 | 142 |
|
| 143 | +<!-- |
67 | 144 | ### Dynamic parts spanning several /
|
| 145 | +--> |
| 146 | +### 複数の`/`をまたぐ動的パート |
68 | 147 |
|
| 148 | +<!-- |
69 | 149 | If you want a dynamic part to capture more than one URI path segment, separated by forward slashes, you can define a dynamic part using the `*id` syntax, which uses the `.*` regular expression:
|
| 150 | +--> |
| 151 | +動的パートを使って、URI パスの `/` で分割された複数のセグメントをまとめてキャプチャしたいときは、`.*` という正規表現に対応する `*id` という文法が使えます。 |
70 | 152 |
|
71 | 153 | @[spanning-path](code/javaguide.http.routing.routes)
|
72 | 154 |
|
| 155 | +<!-- |
73 | 156 | Here, for a request like `GET /files/images/logo.png`, the `name` dynamic part will capture the `images/logo.png` value.
|
| 157 | +--> |
| 158 | +これで、`GET /files/images/logo.png` というリクエストに対して、動的パート `name` に `images/logo.png` という値をキャプチャさせることができます。 |
74 | 159 |
|
| 160 | +<!-- |
75 | 161 | ### Dynamic parts with custom regular expressions
|
| 162 | +--> |
| 163 | +### 動的パートで独自の正規表現を使う |
76 | 164 |
|
| 165 | +<!-- |
77 | 166 | You can also define your own regular expression for a dynamic part, using the `$id<regex>` syntax:
|
78 |
| - |
| 167 | +--> |
| 168 | +動的パートに独自の正規表現を使わせたい場合は、`$id<regex>` という文法を利用します。 |
| 169 | + |
79 | 170 | @[regex-path](code/javaguide.http.routing.routes)
|
80 | 171 |
|
| 172 | +<!-- |
81 | 173 | ## Call to action generator method
|
| 174 | +--> |
| 175 | +## アクションジェネレータメソッドの呼び出し |
82 | 176 |
|
| 177 | +<!-- |
83 | 178 | The last part of a route definition is the call. This part must define a valid call to an action method.
|
| 179 | +--> |
| 180 | +ルート定義の最後のパートは、アクションの呼び出しです。このパートでは、アクションメソッド呼び出しを定義する必要があります。 |
84 | 181 |
|
| 182 | +<!-- |
85 | 183 | If the method does not define any parameters, just give the fully-qualified method name:
|
| 184 | +--> |
| 185 | +メソッドが一つも引数を取らない場合、単にメソッドの完全修飾名を指定します。 |
86 | 186 |
|
87 | 187 | @[home-page](code/javaguide.http.routing.routes)
|
88 | 188 |
|
| 189 | +<!-- |
89 | 190 | If the action method defines parameters, the corresponding parameter values will be searched for in the request URI, either extracted from the URI path itself, or from the query string.
|
| 191 | +--> |
| 192 | +アクションメソッドが引数を取る場合、対応する引数はリクエスト URI のパスまたはクエリストリングから抽出されます。 |
90 | 193 |
|
91 | 194 | @[page](code/javaguide.http.routing.routes)
|
92 | 195 |
|
| 196 | +<!-- |
93 | 197 | Or:
|
| 198 | +--> |
| 199 | +また、クエリストリングから抽出するためには、 |
94 | 200 |
|
95 | 201 | @[page](code/javaguide.http.routing.query.routes)
|
96 | 202 |
|
| 203 | +<!-- |
97 | 204 | Here is the corresponding `show` method definition in the `controllers.Application` controller:
|
| 205 | +--> |
| 206 | +このルートに対応する `controllers.Application` コントローラの `show` メソッドの定義は次のようになります。 |
98 | 207 |
|
99 | 208 | @[show-page-action](code/javaguide/http/routing/controllers/Application.java)
|
100 | 209 |
|
| 210 | +<!-- |
101 | 211 | ### Parameter types
|
| 212 | +--> |
| 213 | +### 引数の型 |
102 | 214 |
|
| 215 | +<!-- |
103 | 216 | For parameters of type `String`, the parameter type is optional. If you want Play to transform the incoming parameter into a specific Scala type, you can add an explicit type:
|
| 217 | +--> |
| 218 | +`String` 型の引数の場合、型の記述はオプションです。リクエストパラメータを特定の Scala 型に変換したいときは、明示的に型を追記することができます。 |
104 | 219 |
|
105 | 220 | @[clients-show](code/javaguide.http.routing.routes)
|
106 | 221 |
|
| 222 | +<!-- |
107 | 223 | Then use the same type for the corresponding action method parameter in the controller:
|
| 224 | +--> |
| 225 | +このルートに対応するコントローラのアクションメソッドの引数には同じ型を使用します。 |
108 | 226 |
|
109 | 227 | @[clients-show-action](code/javaguide/http/routing/controllers/Clients.java)
|
110 | 228 |
|
| 229 | +<!-- |
111 | 230 | > **Note:** The parameter types are specified using a suffix syntax. Also, the generic types are specified using the `[]` symbols instead of `<>`, as in Java. For example, `List[String]` is the same type as the Java `List<String>`.
|
| 231 | +--> |
| 232 | +> **ノート:** 引数の型は後置形式で指定されます。また、ジェネリックスの型は Java の `<>` 構文の代わりに `[]` で指定されます。例えば、 Java での `List<String>` は `List[String]` になります。 |
112 | 233 |
|
| 234 | +<!-- |
113 | 235 | ### Parameters with fixed values
|
| 236 | +--> |
| 237 | +### 引数に定数を渡す |
114 | 238 |
|
| 239 | +<!-- |
115 | 240 | Sometimes you’ll want to use a fixed value for a parameter:
|
| 241 | +--> |
| 242 | +アクションメソッドの引数に定数を渡したいときは、次のように記述します。 |
116 | 243 |
|
117 | 244 | @[page](code/javaguide.http.routing.fixed.routes)
|
118 | 245 |
|
| 246 | +<!-- |
119 | 247 | ### Parameters with default values
|
| 248 | +--> |
| 249 | +### デフォルト引数 |
120 | 250 |
|
| 251 | +<!-- |
121 | 252 | You can also provide a default value that will be used if no value is found in the incoming request:
|
| 253 | +--> |
| 254 | +受け取ったリクエストに値がないとき、代わりにデフォルト値を渡すこともできます。 |
122 | 255 |
|
123 | 256 | @[clients](code/javaguide.http.routing.defaultvalue.routes)
|
124 | 257 |
|
| 258 | +<!-- |
125 | 259 | ### Optional parameters
|
| 260 | +--> |
| 261 | +### オプション引数 |
126 | 262 |
|
| 263 | +<!-- |
127 | 264 | You can also specify an optional parameter that does not need to be present in all requests:
|
| 265 | +--> |
| 266 | +すべてのリクエストに存在している必要のないオプション引数を指定することもできます。 |
128 | 267 |
|
129 | 268 | @[optional](code/javaguide.http.routing.routes)
|
130 | 269 |
|
| 270 | +<!-- |
131 | 271 | ## Routing priority
|
| 272 | +--> |
| 273 | +## ルートの優先度 |
132 | 274 |
|
| 275 | +<!-- |
133 | 276 | Many routes can match the same request. If there is a conflict, the first route (in declaration order) is used.
|
| 277 | +--> |
| 278 | +リクエストに複数のルートがマッチしてしまうことがあります。そのような競合が発生した場合は、先に宣言された方のルートが優先されます。 |
134 | 279 |
|
| 280 | +<!-- |
135 | 281 | ## Reverse routing
|
| 282 | +--> |
| 283 | +## リバースルーティング |
136 | 284 |
|
| 285 | +<!-- |
137 | 286 | The router can be used to generate a URL from within a Java call. This makes it possible to centralize all your URI patterns in a single configuration file, so you can be more confident when refactoring your application.
|
| 287 | +--> |
| 288 | +Java コード中で URL を生成するためにルータを使うことができます。これにより、URI パターンの定義をただ一つの設定ファイルに集約することができ、アプリケーションをリファクタリングする際の間違いを減らすことができます。 |
138 | 289 |
|
| 290 | +<!-- |
139 | 291 | For each controller used in the routes file, the router will generate a ‘reverse controller’ in the `routes` package, having the same action methods, with the same signature, but returning a `play.mvc.Call` instead of a `play.mvc.Result`.
|
| 292 | +--> |
| 293 | +ルータは、routes ファイルから利用された全てのコントローラについて、`routes` パッケージ以下に `リバースコントローラ` を生成します。リバースコントローラは元になったコントローラと同じシグネチャで、`play.mvc.Result` の代わりに `play.mvc.Call` を返すようなメソッドを持っています。 |
140 | 294 |
|
| 295 | +<!-- |
141 | 296 | The `play.mvc.Call` defines an HTTP call, and provides both the HTTP method and the URI.
|
| 297 | +--> |
| 298 | +`play.mvc.Call` は HTTP 呼び出しを表していて、HTTP メソッドと URL の両方の情報を持っています。 |
142 | 299 |
|
| 300 | +<!-- |
143 | 301 | For example, if you create a controller like:
|
| 302 | +--> |
| 303 | +例えば、次のようなコントローラを作成したとします。 |
144 | 304 |
|
145 | 305 | @[controller](code/javaguide/http/routing/reverse/controllers/Application.java)
|
146 | 306 |
|
| 307 | +<!-- |
147 | 308 | And if you map it in the `conf/routes` file:
|
| 309 | +--> |
| 310 | +そして、このコントローラを `conf/routes` で次のようにマッピングしたとします。 |
148 | 311 |
|
149 | 312 | @[hello](code/javaguide.http.routing.reverse.routes)
|
150 | 313 |
|
| 314 | +<!-- |
151 | 315 | You can then reverse the URL to the `hello` action method, by using the `controllers.routes.Application` reverse controller:
|
| 316 | +--> |
| 317 | +このとき、`controllers.routes.Application` というリバースコントローラを利用することで、`hello` というアクションメソッドの URL を逆引きすることができます。 |
152 | 318 |
|
153 | 319 | @[reverse-redirect](code/javaguide/http/routing/controllers/Application.java)
|
154 | 320 |
|
| 321 | +<!-- |
155 | 322 | > **Note:** There is a `routes` subpackage for each controller package. So the action `controllers.admin.Application.hello` can be reversed via `controllers.admin.routes.Application.hello`.
|
| 323 | +--> |
| 324 | +> **注:** 各コントローラパッケージには `routes` サブパッケージが存在します。このため、`controllers.admin.Application.hello` アクションは `controllers.admin.routes.Application.hello` によってリバースすることができます。 |
0 commit comments