Getting Started‎ > ‎

Listing tweets

Let's list tweeted contents as follows:

index.jsp

<%@page pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglib prefix="f" uri="http://www.slim3.org/functions"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>twitter Index</title>
<link rel="stylesheet" type="text/css" href="/css/global.css" />
</head>
<body>
<p>What are you doing?</p>
<form method="post" action="tweet">
<textarea name="content"></textarea><br />
<input type="submit" value="tweet"/>
</form>

<c:forEach var="e" items="${tweetList}">
${f:h(e.content)}
<hr />
</c:forEach>

</body>
</html>

I added the above bold part. I list tweeted contents using the tweetList variable.
f:h is a JSP function to escape HTML.
Visit http://localhost:8888/twitter/. The tweets are not shown, because I have not implemented it yet.

I need to prepare tweetList variable, so I modify "IndexControllerTest.java" as follows:

IndexControllerTest.java

package tutorial.controller.twitter;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import org.junit.Test;
import org.slim3.tester.ControllerTestCase;

public class IndexControllerTest extends ControllerTestCase {

    @Test
    public void run() throws Exception {
        tester.start("/twitter/");
        IndexController controller = tester.getController();
        assertThat(controller, is(notNullValue()));
        assertThat(tester.isRedirect(), is(false));
        assertThat(tester.getDestinationPath(), is("/twitter/index.jsp"));
        assertThat(tester.requestScope("tweetList"), is(notNullValue()));
    }
}

I added the above bold part. tester.requestScope("tweetList") is short for tester.request.getAttribute("tweetList").

Run the "IndexControllerTest.java". The result will be red(NG), because I have not modified "IndexController.java" yet. Then let's modify "IndexController.java".

IndexController.java

package tutorial.controller.twitter;

import java.util.List;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import tutorial.model.Tweet;
import tutorial.service.TwitterService;

public class IndexController extends Controller {

    private TwitterService service = new TwitterService();

    @Override
    public Navigation run() throws Exception {
        List<Tweet> tweetList = service.getTweetList();
        requestScope("tweetList", tweetList);
        return forward("index.jsp");
    }
}

tester.requestScope("tweetList", tweetList) is short for tester.request.setAttribute("tweetList", tweetList).
A compile error occurs. Compiler says "The method getTweetList is undefined for the type TwitterService". Before implementing getTweetList method, let's define the test.

TwitterServiceTest.java

package tutorial.service;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.slim3.datastore.Datastore;
import org.slim3.tester.AppEngineTestCase;

import tutorial.model.Tweet;

public class TwitterServiceTest extends AppEngineTestCase {

    private TwitterService service = new TwitterService();

    // ...

    @Test
    public void getTweetList() throws Exception {
        Tweet tweet = new Tweet();
        tweet.setContent("Hello");
        Datastore.put(tweet);
        List<Tweet> tweetList = service.getTweetList();
        assertThat(tweetList.size(), is(1));
        assertThat(tweetList.get(0).getContent(), is("Hello"));
    }
}

A compile error occurs. Compiler says "The method getTweetList is undefined for the type TwitterService", so let's define it.

TwitterService.java

package tutorial.service;

import java.util.List;
import java.util.Map;

import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import tutorial.meta.TweetMeta;
import tutorial.model.Tweet;

import com.google.appengine.api.datastore.Transaction;

public class TwitterService {

    private TweetMeta t = new TweetMeta();

    // ...

    public List<Tweet> getTweetList() {
        return Datastore.query(t).sort(t.createdDate.desc).asList();
    }
}

TweetMeta is created by Annotation Processor Tool automatically.
See Meta data of model.
You can query using a meta data of a model.
See Introducing Queries.

The compile error of "TwitterServiceTest.java" is gone, so run the "TwitterServiceTest.java". The result will be green(OK).
The compile error of "IndexControllerTest.java" is gone, so run the "IndexControllerTest.java". The result will be green(OK).

Visit http://localhost:8888/twitter/. The tweets are shown.

Congratulations!

You have completed this tutorial.

Next...

See Slim3 Datastore and try Online demo.


Comments