Skip to content

Commit a3a2ee5

Browse files
yamakz5555garbagetown
authored andcommitted
ありがとうございます! close playframework-ja#1244
1 parent 94da0fd commit a3a2ee5

File tree

1 file changed

+170
-1
lines changed

1 file changed

+170
-1
lines changed
Lines changed: 170 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,324 @@
11
<!--- Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com> -->
2+
<!--
23
# HTTP routing
4+
-->
5+
# HTTPルーティング
36

7+
<!--
48
## The built-in HTTP router
9+
-->
10+
## 組み込み HTTP ルータ
511

12+
<!--
613
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 なメソッド) の呼び出しへ変換するコンポーネントです。
716

17+
<!--
818
An HTTP request is seen as an event by the MVC framework. This event contains two major pieces of information:
19+
-->
20+
HTTP リクエストは MVC フレームワークにとってイベントであるといえます。このイベントには大きく分けて次の二つの情報が含まれています。
921

22+
<!--
1023
- the request path (such as `/clients/1542`, `/photos/list`), including the query string.
1124
- the HTTP method (GET, POST, ...).
25+
-->
26+
- クエリストリングを含むリクエストパス (例えば、`/clients/1542``/photos/list`)
27+
- HTTP メソッド (GET、POST など)
1228

29+
<!--
1330
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` ファイルに定義しておくことでコンパイルされます。これは、ルート定義に関するエラーもブラウザで直接的に確認できるということです。
1433

1534
[[images/routesError.png]]
1635

36+
<!--
1737
## Dependency Injection
38+
-->
39+
## 依存性の注入
1840

41+
<!--
1942
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` に以下の設定が含まれます。
2045

2146
```scala
2247
routesGenerator := InjectedRoutesGenerator
2348
```
2449

50+
<!--
2551
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` として宣言することで、静的なルータ生成用のコードサンプルに作り変えることができます。
2654

55+
<!--
2756
## The routes file syntax
57+
-->
58+
## routesファイルの文法
2859

60+
<!--
2961
`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 パターン、そしてそれらに割り当てられたアクションメソッドの呼び出しで表します。
3064

65+
<!--
3166
Let’s see what a route definition looks like:
67+
-->
68+
実際のルート定義を見てみましょう。
3269

3370
@[clients-show](code/javaguide.http.routing.routes)
3471

72+
<!--
3573
> Note that in the action call, the parameter type comes after the parameter name, like in Scala.
74+
-->
75+
> アクション呼び出しでは、Scala のように引数名の後に型を指定する事に注意して下さい。
3676
77+
<!--
3778
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 パターンが続きます。最後がアクション呼び出しの定義です。
3881

82+
<!--
3983
You can also add comments to the route file, with the `#` character:
84+
-->
85+
`#` の文字を使って、routes ファイルにコメントを残すこともできます。
4086

4187
@[clients-show-comment](code/javaguide.http.routing.routes)
4288

89+
<!--
4390
## The HTTP method
91+
-->
92+
## HTTPメソッド
4493

94+
<!--
4595
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`)が指定できます。
4698

99+
<!--
47100
## The URI pattern
101+
-->
102+
## URIパターン
48103

104+
<!--
49105
The URI pattern defines the route’s request path. Some parts of the request path can be dynamic.
106+
-->
107+
URI パターンはルートのリクエストパスの定義です。リクエストパスの一部を動的にすることができます。
50108

109+
<!--
51110
### Static path
111+
-->
112+
### 静的パス
52113

114+
<!--
53115
For example, to exactly match `GET /clients/all` incoming requests, you can define this route:
116+
-->
117+
例えば、リクエストを `GET /clients/all` に完全一致させたいときは、次のように定義できます。
54118

55119
@[static-path](code/javaguide.http.routing.routes)
56120

121+
<!--
57122
### Dynamic parts
123+
-->
124+
### 動的パート
58125

126+
<!--
59127
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 を取得するような場合には、動的パートを追加する必要があります。
60130

61131
@[clients-show](code/javaguide.http.routing.routes)
62132

133+
<!--
63134
> Note that a URI pattern may have more than one dynamic part.
135+
-->
136+
> 1 つの URI パターンには、2 つ以上の動的パートを含められます
64137
138+
<!--
65139
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 パートにマッチします。
66142

143+
<!--
67144
### Dynamic parts spanning several /
145+
-->
146+
### 複数の`/`をまたぐ動的パート
68147

148+
<!--
69149
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` という文法が使えます。
70152

71153
@[spanning-path](code/javaguide.http.routing.routes)
72154

155+
<!--
73156
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` という値をキャプチャさせることができます。
74159

160+
<!--
75161
### Dynamic parts with custom regular expressions
162+
-->
163+
### 動的パートで独自の正規表現を使う
76164

165+
<!--
77166
You can also define your own regular expression for a dynamic part, using the `$id<regex>` syntax:
78-
167+
-->
168+
動的パートに独自の正規表現を使わせたい場合は、`$id<regex>` という文法を利用します。
169+
79170
@[regex-path](code/javaguide.http.routing.routes)
80171

172+
<!--
81173
## Call to action generator method
174+
-->
175+
## アクションジェネレータメソッドの呼び出し
82176

177+
<!--
83178
The last part of a route definition is the call. This part must define a valid call to an action method.
179+
-->
180+
ルート定義の最後のパートは、アクションの呼び出しです。このパートでは、アクションメソッド呼び出しを定義する必要があります。
84181

182+
<!--
85183
If the method does not define any parameters, just give the fully-qualified method name:
184+
-->
185+
メソッドが一つも引数を取らない場合、単にメソッドの完全修飾名を指定します。
86186

87187
@[home-page](code/javaguide.http.routing.routes)
88188

189+
<!--
89190
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 のパスまたはクエリストリングから抽出されます。
90193

91194
@[page](code/javaguide.http.routing.routes)
92195

196+
<!--
93197
Or:
198+
-->
199+
また、クエリストリングから抽出するためには、
94200

95201
@[page](code/javaguide.http.routing.query.routes)
96202

203+
<!--
97204
Here is the corresponding `show` method definition in the `controllers.Application` controller:
205+
-->
206+
このルートに対応する `controllers.Application` コントローラの `show` メソッドの定義は次のようになります。
98207

99208
@[show-page-action](code/javaguide/http/routing/controllers/Application.java)
100209

210+
<!--
101211
### Parameter types
212+
-->
213+
### 引数の型
102214

215+
<!--
103216
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 型に変換したいときは、明示的に型を追記することができます。
104219

105220
@[clients-show](code/javaguide.http.routing.routes)
106221

222+
<!--
107223
Then use the same type for the corresponding action method parameter in the controller:
224+
-->
225+
このルートに対応するコントローラのアクションメソッドの引数には同じ型を使用します。
108226

109227
@[clients-show-action](code/javaguide/http/routing/controllers/Clients.java)
110228

229+
<!--
111230
> **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]` になります。
112233
234+
<!--
113235
### Parameters with fixed values
236+
-->
237+
### 引数に定数を渡す
114238

239+
<!--
115240
Sometimes you’ll want to use a fixed value for a parameter:
241+
-->
242+
アクションメソッドの引数に定数を渡したいときは、次のように記述します。
116243

117244
@[page](code/javaguide.http.routing.fixed.routes)
118245

246+
<!--
119247
### Parameters with default values
248+
-->
249+
### デフォルト引数
120250

251+
<!--
121252
You can also provide a default value that will be used if no value is found in the incoming request:
253+
-->
254+
受け取ったリクエストに値がないとき、代わりにデフォルト値を渡すこともできます。
122255

123256
@[clients](code/javaguide.http.routing.defaultvalue.routes)
124257

258+
<!--
125259
### Optional parameters
260+
-->
261+
### オプション引数
126262

263+
<!--
127264
You can also specify an optional parameter that does not need to be present in all requests:
265+
-->
266+
すべてのリクエストに存在している必要のないオプション引数を指定することもできます。
128267

129268
@[optional](code/javaguide.http.routing.routes)
130269

270+
<!--
131271
## Routing priority
272+
-->
273+
## ルートの優先度
132274

275+
<!--
133276
Many routes can match the same request. If there is a conflict, the first route (in declaration order) is used.
277+
-->
278+
リクエストに複数のルートがマッチしてしまうことがあります。そのような競合が発生した場合は、先に宣言された方のルートが優先されます。
134279

280+
<!--
135281
## Reverse routing
282+
-->
283+
## リバースルーティング
136284

285+
<!--
137286
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 パターンの定義をただ一つの設定ファイルに集約することができ、アプリケーションをリファクタリングする際の間違いを減らすことができます。
138289

290+
<!--
139291
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` を返すようなメソッドを持っています。
140294

295+
<!--
141296
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 の両方の情報を持っています。
142299

300+
<!--
143301
For example, if you create a controller like:
302+
-->
303+
例えば、次のようなコントローラを作成したとします。
144304

145305
@[controller](code/javaguide/http/routing/reverse/controllers/Application.java)
146306

307+
<!--
147308
And if you map it in the `conf/routes` file:
309+
-->
310+
そして、このコントローラを `conf/routes` で次のようにマッピングしたとします。
148311

149312
@[hello](code/javaguide.http.routing.reverse.routes)
150313

314+
<!--
151315
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 を逆引きすることができます。
152318

153319
@[reverse-redirect](code/javaguide/http/routing/controllers/Application.java)
154320

321+
<!--
155322
> **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

Comments
 (0)