Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Now, let's build a HTML view for blog posts (you don't want your readers to get your posts by JSON, right?). Again, we will use convention to make it work easily. Open up BlogEntryController and add the following method:

Code Block
  @Override
  protected boolean isAutomaticHtmlRoutingEnabled() {
    return true;
  }

...

So now, the automatic HTML routing will send the request for ra/blogEntries.html to the BlogEntryIndexPage component. But we don't have any content in this component, so let's make a method to fetch all blog entries per creation date in descending order. So in BlogEntryIndexPage.java, add the following method:

Code Block
    public NSArray<BlogEntry> entries() {
      EOEditingContext ec = ERXEC.newEditingContext();
      return BlogEntry.fetchAllBlogEntries(ec, BlogEntry.CREATION_DATE.descs());
    }

We need to use that method in a WORepetition, and for that loop, we need a BlogEntry variable to iterate in the list, so add the following code to BlogEntryIndexPage.java:

Code Block
    private BlogEntry entryItem;

    public BlogEntry entryItem() {
      return entryItem;
    }
    
    public void setEntryItem(BlogEntry entryItem) {
      this.entryItem = entryItem;
    }

The Java part is done, so let's add the loop inside the component. Open BlogEntryIndexPage.wo (it's located in the Component folder) and right after the <body> tag, add:

Code Block
    <wo:loop list="$entries" item="$entryItem">
      <p><wo:str value="$entryItem.title" /></p>
      <p><wo:str value="$entryItem.author.fullName" /></p>
    </wo:loop>

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.

If you go to _ http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.html_Image Modified, you will see the list of blog entries!

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.

Open BlogEntryShowPage.java and make sure the class extends from er.rest.routes.IERXRouteComponent.

Code Block

import er.rest.routes.IERXRouteComponent;

public class BlogEntryShowPage extends WOComponent implements IERXRouteComponent {

We need to add other methods to receive the BlogEntry object from the controller. In BlogEntryShowPage.java, add:

Code Block

    private BlogEntry blogEntry;
    
    @ERXRouteParameter
    public void setBlogEntry(BlogEntry blogEntryFromController) {
      this.blogEntry = blogEntryFromController;
    }
    
    public BlogEntry blogEntry() {
      return this.blogEntry;
    }

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.

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:

Code Block

    <h1><wo:str value="$blogEntry.title" /></h1>
    <p><wo:str value="$blogEntry.content" /></p>
    <p>Created on: <wo:str value="$blogEntry.creationDate" dateformat="%Y/%m/%d" /></p>
    <p>Added by: <wo:str value="$blogEntry.author.fullName" /></p>

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:

Code Block

<p><wo:str value="$entryItem.title" /></p>

with:

Code Block

<p><wo:ERXRouteLink entityName="BlogEntry" record="$entryItem" action="show"><wo:str value="$entryItem.title" /></wo:ERXRouteLink></p>

Save the component and run the app. Go to http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.htmlImage Added 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!

The REST part of this tutorial is now complete, you can now switch to the next part of the tutorial.