Getting Started‎ > ‎

Creating a controller and a test

Visit the mini twitter(http://localhost:8888/twitter/) application. You will encounter 404 NOT FOUND HTTP ERROR, because you haven't create it yet. So let's create it.

Double-click build.xml under the project.

In the Outline view, right-click gen-controller task and select Run as > Ant Build(first item). If you encounter an exception, confirm that you use Java 32 bit and Eclipse 32 bit.

In the ant input dialog, input "/twitter/", and click OK button. The important point is that "/twitter/" ends with "/".
"/twitter/" is short for "/twitter/index".
"tutorial.controller.twitter.IndexController", "tutorial.controller.twitter.IndexControllerTest" and "war/twitter/index.jsp" are created by "/twitter/index" path.

If your project is not refreshed automatically, in the Window menu, select Preferences > General > Workspace. Click "Refresh automatically" checkbox. Or refresh your project manually.

"src/tutorial/controller/twitter/", "test/tutorial/controller/twitter/" and "war/twitter/index.jsp" will appear.

Let's take a look at "src/tutorial.controller.twitter/".

package tutorial.controller.twitter;

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

public class IndexController extends Controller {

    public Navigation run() throws Exception {
        return forward("index.jsp");

When you visit "/twitter/", tutorial.controller.twitter.IndexController#run() is performed. The return value of run() is a destination path.
"index.jsp" is equivalent to "war/twitter/index.jsp".

Let's take a look at "test/tutorial.controller.twitter/".

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 {

    public void run() throws Exception {
        IndexController controller = tester.getController();
        assertThat(controller, is(notNullValue()));
        assertThat(tester.getDestinationPath(), is("/twitter/index.jsp"));

start("/twitter/") emulates visiting "/twitter/" URL in the browser. After calling this method, you can access the execution result.
By calling getController method, you can get the controller instance.
By calling getDestinationPath method, you can get the destination path.

Let's take a look at "war/twitter/index.jsp".


<%@page pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib prefix="c" uri=""%>
<%@taglib prefix="fmt" uri=""%>
<%@taglib prefix="f" uri=""%>

<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" />
<p>Hello twitter Index !!!</p>

Run the "". The result will be green(OK).
Then, visit http://localhost:8888/twitter/ again. "Hello twitter Index !!!" must be shown.
Slim3 can find a controller from the path automatically, so you don't need the manual configuration.
Due to HOT reloading, when you change the source code, you can see the result on your browser without restarting web application.


Continue to Creating a form.