2023年2月28日火曜日

Cozy Web/エンティティ表示

前々回に作成したHelloResourceでは、Cozy Webでモデル定義をすることでWeb上のリソースとしてアクセスすることができることを説明しました。

前回はデモモードを使って、デモ時のみメモリDBにデータ投入する方法を紹介しました。

今回はHelloResourceを使って、モデルで定義したリソースをデザイン化した画面で表示する方法を説明します。

HelloResource

前々回作成したHelloResourceにエンティティの情報を表示するビューを追加します。

HelloResourceではビューは定義されていないので、エンティティのデフォルトの表示画面で表示されます。これをデザイン化した表示画面にすることが目的です。

準備

cozyを起動するディレクトリのwebappsディレクトリに、アプリケーションのホームディレクトリとなるHelloResourceが作成されています。ここにビューを追加していきます。

モデル

WEB-INF/models/model.orgにアプリケーションで使用するモデルが定義されています。

  1. * entity    
  2. ** product  
  3. *** features    
  4. table=product  
  5. *** attributes    
  6. | Name  | Type   | Multiplicity |  
  7. |-------+--------+--------------|  
  8. | id    | token  |            1 |  
  9. | name  | string |            1 |  
  10. | price | int    |            1 |  

エンティティproductを定義しています。

これがWebのリソースproductとなります。

ビュー

リソースproductの表示ビューとして以下のHTMLをプロジェクトルートにproduct.htmlとして配置します。

  1. <html>  
  2.     <head>  
  3.     <title>Product List</title>  
  4.     </head>  
  5.     <body>  
  6.     <c:table />  
  7.     </body>  
  8. </html>  

タグ c:table の場所にエンティティ一覧のモデルの内容が表示されます。それ以外の部分は通常のHTMLとして装飾できます。

また c:table の表示に関してもカスタマイズができますが、詳細は今後取り上げていく予定です。

product.htmlの代わりにproduct.jadeとして以下のJadeファイルを配置しても同じ振る舞いになります。

  1. html  
  2.   head  
  3.     title Product List  
  4.   body  
  5.     c:table  

実行

それではHelloResourceを実行してみましょう。

モデルとして定義したリソースのproductの一覧取得は以下になります。

WebアプリケーションのURL http://localhost:8080/web/HelloResource の直下にリソース名productを指定しています。

  1. $ curl http://localhost:8080/web/HelloResource/product  

この結果、以下のHTML文書が返されます。(読みやすいようにxmllintで整形しています。)

  1. <?xml version="1.0"?>  
  2. <!DOCTYPE html>  
  3. <html>  
  4.   <head>  
  5.     <title>Product List</title>  
  6.   </head>  
  7.   <body>  
  8.     <table class="">  
  9.       <caption class="">product</caption>  
  10.       <thead class="">  
  11.         <tr class="">  
  12.           <th scope="col" class="">Id</th>  
  13.           <th scope="col" class="">Name</th>  
  14.           <th scope="col" class="">Price</th>  
  15.         </tr>  
  16.       </thead>  
  17.       <tbody class="">  
  18.         <tr data-href="product/1.html">  
  19.           <td class="">1</td>  
  20.           <td class="">Apple</td>  
  21.           <td class="">300</td>  
  22.         </tr>  
  23.         <tr data-href="product/2.html">  
  24.           <td class="">2</td>  
  25.           <td class="">Orange</td>  
  26.           <td class="">350</td>  
  27.         </tr>  
  28.         <tr data-href="product/3.html">  
  29.           <td class="">3</td>  
  30.           <td class="">Peach</td>  
  31.           <td class="">400</td>  
  32.         </tr>  
  33.       </tbody>  
  34.     </table>  
  35.   </body>  
  36. </html>  

今回データとして投入した4つのエンティティに対応する情報が表示されています。

詳細表示

次はエンティティの詳細表示ビューです。

ビュー

リソースproductの詳細表示を行うビューとして以下のHTMLを用意します。

  1. <html>  
  2.     <head>  
  3.     <title>Product Detail</title>  
  4.     </head>  
  5.     <body>  
  6.     <c:detail />  
  7.     </body>  
  8. </html>  

タグ c:detail がエンティティの詳細表示を行うタグです。この場所にエンティティの詳細情報が表示されます。それ以外の部分は通常のHTMLとして装飾できます。

また c:detail の表示に関してもカスタマイズができますが、詳細は今後取り上げていく予定です。

このHTMLファイルをプロジェクトルート配下のproduct.entityディレクトリ配下に配置します。

product.entityディレクトリはリソースproductに対してエンティティ操作のシナリオに対するビューを格納するためのディレクトリです。サフィックスのentityが「エンティティ操作のシナリオに対するビュー」を示しています。

この配置を行うことにより「product/3」といったリソースproductのID 3のエンティティに対する表示のためのビューとして利用されます。

実行

それではHelloResourceを実行してみましょう。

  1. $ curl http://localhost:8080/web/HelloResource/product/4  

この結果、以下のHTML文書が返されます。(読みやすいようにxmllintで整形しています。)

  1. <?xml version="1.0"?>  
  2. <!DOCTYPE html>  
  3. <html>  
  4.   <head>  
  5.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
  6.     <meta name="keywords" content="TBD"/>  
  7.     <meta name="description" content="TBD"/>  
  8.     <meta name="robots" content="noindex,nofollow"/>  
  9.     <meta name="author" content="TBD"/>  
  10.     <title>Product Detail</title>  
  11.   </head>  
  12.   <body>  
  13.     <table class="">  
  14.       <tbody>  
  15.         <tr class="">  
  16.           <th scope="row" class="">Id</th>  
  17.           <td class="">4</td>  
  18.         </tr>  
  19.         <tr class="">  
  20.           <th scope="row" class="">Name</th>  
  21.           <td class="">Berry</td>  
  22.         </tr>  
  23.         <tr class="">  
  24.           <th scope="row" class="">Price</th>  
  25.           <td class="">450</td>  
  26.         </tr>  
  27.       </tbody>  
  28.     </table>  
  29.   </body>  
  30. </html>  

まとめ

今回はモデルとして定義されたエンティティをリソースとして操作する際に、参照ビューを定義する方法について説明しました。

次回はエンティティ・リソースの更新についてみていきます。

諸元

Cozy
0.0.12