Render a view

ActionControl class

All methods in ActionControl class are static so that they can be applied to any controller without the controller inheriting from it.

Just do a static import of the ActionControl class in a controller class, then all static methods from ActionControl are available.

import static com.scooterframework.web.controller.ActionControl.*;

Prepare view data

ActionControl.setViewData(String key, Object data) method is used to prepare data for views. This method binds data to the key. In the view file, the token represented by the key will be replaced by content of data.

Multiple ways of rendering

Scooter's controller can render views in multiple ways.

1. Use return null in action method
public class PostsController {
    public String index() {
        setViewData("posts", Post.findAll());
        return null;
    }
}

//This would render the view:
//{controller}/{method}.{view.extension} => views/posts/index.jsp

"return null" from an action method would let Scooter to automatically render a view with the same name as the method name.

view.extension is a property defined in environment.properties file with default value jsp.

If your views are not based on jsp, you may either change the value of view.extension or use a render method defined below.

2. Use renderView methods to render a view template
public class PostsController {
    public String show() {
        ActiveRecord post = Post.findById(p("id"));
        setViewData("post", post);

        //Use one of the return type below to render a view for the post:

        //render view file views/posts/show.jsp
        return renderView("show");

        //render view file .../WEB-INF/views/posts/show.jsp
        return renderView("posts/show");

        //render view file /home/foo/templates/show.st with StringTemplate engine
        return renderView("/home/foo/templates/show.st");

        //render view file file .../WEB-INF/views/posts/show.st and return as xml
        return renderView("show.st", "xml");

        //return a view coded in String Template as html
        return renderView("paged_list.st", "html", map);

        //return a view coded in FreeMarker Template as text
        return renderView("paged_list.ftl", "text", map);
    }
}

As you can see, renderView methods are flexible. You can render a view not written in jsp. You can also return rendered results in text or xml. The default is html format.

3. Use render methods to render an object
public class PostsController {
    public String show() {
        ActiveRecord post = Post.findById(p("id"));

        //Use one of the return type below to render the post object:

        //render object post, and let requester to define return format.
        //  /posts/10      => return post content as html
        //  /posts/10.json => return post content as json
        //  /posts/10.text => return post content as text
        //  /posts/10.txt  => return post content as text
        //  /posts/10.xml  => return post content as xml
        return render(post);

        //render object post and always return result in html format
        //Here the request extension has no impact on response format
        return render(post, "html");

        //render object post and always return result in text format
        //Here the request extension has no impact on response format
        return render(post, "text");

        //render object post and always return result in xml format
        //Here the request extension has no impact on response format
        return render(post, "xml");
    }
}
4. Use built-in helper methods to render a string
public class HelloController {
    public String sayit() {
        //Use one of the return type below to render a string:

        //render a string, and let requester to define return format.
        //  /hello/sayit      => return content as html
        //  /hello/sayit.json => return content as json
        //  /hello/sayit.text => return content as text
        //  /hello/sayit.txt  => return content as text
        //  /hello/sayit.xml  => return content as xml
        return "Hello World";

        //render a string and always return result in html format
        //Here the request extension has no impact on response format
        return html("<h1>Hello World</h1>");

        //render a string and always return result in text format
        //Here the request extension has no impact on response format
        return text("Hello World");

        //render a string and always return result in xml format
        //Here the request extension has no impact on response format
        return xml("Programming Java");
    }
}