Changes for page Your First Rest Project
Last modified by Steve Peery on 2013/09/06 11:02
From version 31.1
edited by Pascal Robert
on 2011/12/27 23:07
on 2011/12/27 23:07
Change comment:
There is no comment for this version
To version 32.1
edited by Pascal Robert
on 2011/12/29 08:31
on 2011/12/29 08:31
Change comment:
There is no comment for this version
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -310,7 +310,7 @@ 310 310 311 311 {{code}} 312 312 313 - 313 +@Override 314 314 protected boolean isAutomaticHtmlRoutingEnabled() { 315 315 return true; 316 316 } ... ... @@ -317,7 +317,7 @@ 317 317 318 318 {{/code}} 319 319 320 -Switching the return value of this method says that we will follow a certain convention for HTML components. The convention for automatic HTML routing is that the component should be named <EntityName><Action>Page.wo. So in our case, the component will be **BlogEntryIndexPage**. Right-click on the project name in Eclipse and select **New** -> **WOComponent**. Change the name to **BlogEntryIndexPage** and check the **Create HTML contents** button. Click **Finish**.320 +Switching the return value of this method says that we will follow a certain convention for HTML components. The convention for automatic HTML routing is that the component should be named <EntityName><Action>Page.wo. So in our case, the component will be **BlogEntryIndexPage**. Right-click on the project name in Eclipse and select **New** > **WOComponent**. Change the name to **BlogEntryIndexPage** and check the **Create HTML contents** button. Click **Finish**. 321 321 322 322 The next step to get it to work is to make **BlogEntryIndexPage** to implements the **er.rest.routes.IERXRouteComponent** interface. 323 323 ... ... @@ -333,7 +333,7 @@ 333 333 334 334 {{code}} 335 335 336 - 336 +public NSArray<BlogEntry> entries() { 337 337 EOEditingContext ec = ERXEC.newEditingContext(); 338 338 return BlogEntry.fetchAllBlogEntries(ec, BlogEntry.CREATION_DATE.descs()); 339 339 } ... ... @@ -344,12 +344,12 @@ 344 344 345 345 {{code}} 346 346 347 - 347 +private BlogEntry entryItem; 348 348 349 349 public BlogEntry entryItem() { 350 350 return entryItem; 351 351 } 352 - 352 + 353 353 public void setEntryItem(BlogEntry entryItem) { 354 354 this.entryItem = entryItem; 355 355 } ... ... @@ -360,7 +360,7 @@ 360 360 361 361 {{code}} 362 362 363 - 363 +<wo:loop list="$entries" item="$entryItem"> 364 364 <p><wo:str value="$entryItem.title" /></p> 365 365 <p><wo:str value="$entryItem.author.fullName" /></p> 366 366 </wo:loop> ... ... @@ -369,4 +369,66 @@ 369 369 370 370 That component code will loop over the blog entries and display the title of the entry + the name of the author. Save everything and run the application. 371 371 372 -If you go to //http:~/~/192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.html_, you will see the list of blog entries!// 372 +If you go to http:~/~/192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.html, you will see the list of blog entries 373 + 374 +Now that we have a list of blog entries, let's make a page to show the content of a blog entry. Create a new component named **BlogEntryShowPage**. 375 + 376 +Open **BlogEntryShowPage.java** and make sure the class extends from **er.rest.routes.IERXRouteComponent**. 377 + 378 +{{code}} 379 + 380 +import er.rest.routes.IERXRouteComponent; 381 + 382 +public class BlogEntryShowPage extends WOComponent implements IERXRouteComponent { 383 + 384 +{{/code}} 385 + 386 +We need to add other methods to receive the BlogEntry object from the controller. In **BlogEntryShowPage.java**, add: 387 + 388 +{{code}} 389 + 390 + private BlogEntry blogEntry; 391 + 392 + @ERXRouteParameter 393 + public void setBlogEntry(BlogEntry blogEntryFromController) { 394 + this.blogEntry = blogEntryFromController; 395 + } 396 + 397 + public BlogEntry blogEntry() { 398 + return this.blogEntry; 399 + } 400 + 401 +{{/code}} 402 + 403 +The **@ERXRouteParameter** annotation tells the REST framework that it can automatically receive an object from the controller. And again, it's convention at work. You have to use the annotation and the setter name should be //set<EntityName>//, so for a BlogEntry, it's //setBlogEntry//, for a Author, it will be //setAuthor//. 404 + 405 +The Java part of the work is done, so save the Java class. It's time to work on the component part. Open **BlogEntryShowPage.wo** and between the <body></body> part, add: 406 + 407 +{{code}} 408 + 409 + <h1><wo:str value="$blogEntry.title" /></h1> 410 + <p><wo:str value="$blogEntry.content" /></p> 411 + <p>Created on: <wo:str value="$blogEntry.creationDate" dateformat="%Y/%m/%d" /></p> 412 + <p>Added by: <wo:str value="$blogEntry.author.fullName" /></p> 413 + 414 +{{/code}} 415 + 416 +Our view component is done, the only thing remaining is a link for the blog entry list (BlogEntryIndexPage) to the view page (BlogEntryShowPage). Save **BlogEntryShowPage.wo** and open **BlogEntryIndexPage.wo**. We are going to add a link on the title, you will replace to replace this: 417 + 418 +{{code}} 419 + 420 +<p><wo:str value="$entryItem.title" /></p> 421 + 422 +{{/code}} 423 + 424 +with: 425 + 426 +{{code}} 427 + 428 +<p><wo:ERXRouteLink entityName="BlogEntry" record="$entryItem" action="show"><wo:str value="$entryItem.title" /></wo:ERXRouteLink></p> 429 + 430 +{{/code}} 431 + 432 +Save the component and run the app. Go to http:~/~/192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.html to get the list of posts, and you should see a link on the title. Click on it, and now you get the full details of the blog entry! 433 + 434 +The REST part of this tutorial is now complete, you can now switch to the next part of the tutorial.