concept render method in category java

appears as: render method, render method, render methods, The render method, render methods
Portlets in Action

This is an excerpt from Manning's book Portlets in Action.

At , the @RenderMode annotation for the sayHello method informs the Java runtime that the sayHello method is the portlet’s render method in VIEW mode (we’ll come back to this in chapter 2).

Figure 2.8. Portlet lifecycle methods defined in the Portlet interface. An action request invokes the processAction method, followed by the render method. A render request invokes the render method. The timing of the init and destroy invocations depends on the portlet container implementation.

If the @RequestMapping annotated handler methods provide flexibility with method arguments and return types, they also add a little bit of confusion to the process of distinguishing a handler class’s render methods from its action methods. The simplest way to distinguish your render and action methods from each other is by passing RenderRequest or RenderResponse arguments to your render method, and ActionRequest or ActionResponse arguments to your action method, and keeping the return type of the action method as void.

Your action method must be defined to return void. Any other return type will result in an error. A common misconception is that an @RequestMapping annotated method that returns void is an action method, but that’s incorrect because you may have a render method that returns void and yet accepts a RenderResponse object as an argument to write directly to the output stream. For example, the following method is a render method, not an action method:

@RequestMapping(params="action=doSomeWork")
public void doSomething(RenderResponse response) {

     PrintWriter writer = response.getWriter();
     writer.println("Hello World");
}

In the preceding code, the RenderResponse object is passed to the doSomething method, which writes directly to the output stream and so acts as a render method.

In listing 8.14, the showAddBookForm method is the render method that’s invoked when the user clicks the Add Book button on the home page of the Book Catalog portlet. The getCommandObject method is annotated with @ModelAttribute, so it will be invoked before the handler invokes an action or render method. The getCommandObject creates a new Book object that’s stored in the Model object with an attribute named book, which means a new command object is created before the render request is received by the AddBookController class.

8.3.6. Identifying render methods with @RenderMapping

You earlier saw that the @RequestMapping annotation makes it a bit difficult to distinguish render methods from action methods in annotated controllers. Spring 3.0 introduced the @RenderMapping annotation, which is used to specify a handler method as a render method. @RenderMapping is a method-level annotation that maps render requests to a handler class’s render methods. You can still use the @RequestMapping annotation to annotate your render methods, but using the @RenderMapping annotation is recommended.

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest