MVC Render Command

MVCRenderCommands are classes that respond to MVCPortlet render URLs. If your render logic is simple and you want to implement all of your render logic in your portlet class, see Writing MVC Portlet Controller Code. If your render logic is complex or you want clean separation between render paths, use MVCRenderCommands. Each render URL in your portlet’s JSPs invokes an appropriate render command class.

Here are the steps:

  1. Configure your JSPs to generate render URLs via <portlet:renderURL> tags.

    For example, this render-command-portlet sample render URL invokes an MVC render command named /blade/render.

    <portlet:renderURL var="bladeRender">
    	<portlet:param name="mvcRenderCommandName" value="/blade/render" />
    </portlet:renderURL>
    
  2. Name the render URL via its <portlet:param> named mvcRenderCommandName. The render URL and *MVCRenderCommand class (demonstrated later) map to the mvcRenderCommandName value.

  3. Assign the <portlet:renderURL>’s var attribute a variable name to pass to a UI component.

  4. Assign the render URL variable (var) to a UI component. When the user triggers the UI component, the *MVCRenderCommand class that matches the render URL handles the render request.

    For example, the render URL with the variable bladeRender triggers on users clicking this button.

    <aui:button href="<%= bladeRender %>" value="goto page render" />
    
  5. Create a class that implements the MVCRenderCommand interface.

  6. Annotate the class with an @Component annotation, like this one:

    @Component(
    	property = {
    		"javax.portlet.name=com_liferay_blade_samples_portlet_rendercommand_BladeRenderPortlet",
    		"mvc.command.name=/blade/render"
    	},
    	service = MVCRenderCommand.class
    )
    
  7. Set a javax.portlet.name property to your portlet’s internal ID.

  8. Set a mvc.command.name property to your <portlet:renderURL> tag mvcRenderCommandName portlet parameter value. This maps your class to the render URL.

  9. Register your class as an MVCRenderCommand service by setting the service attribute to MVCRenderCommand.class.

    Note, you can apply MVC Command classes to multiple portlets by setting a javax.portlet.name property for each portlet and apply MVC Command classes to multiple command names by setting an mvc.command.name property for each command name. For example, this component’s javax.portlet.name properties and mvc.command.name properties apply it to two specific portlets and two specific command names.

    @Component(
        immediate = true,
        property = {
           "javax.portlet.name=" + HelloWorldPortletKeys.HELLO_MY_WORLD,
           "javax.portlet.name=" + HelloWorldPortletKeys.HELLO_WORLD,
           "mvc.command.name=/hello/edit_super_entry",
           "mvc.command.name=/hello/edit_entry"
        },
        service = MVCRenderCommand.class
    )
    
  10. Implement your render logic in a method that overrides MVCRenderCommand’s render method. Some *MVCRenderCommands, such as the one below, always render the same JSP.

    public class BlogsViewMVCRenderCommand implements MVCRenderCommand {
    
        @Override
        public String render(
            RenderRequest renderRequest, RenderResponse renderResponse) {
    
            return "/blogs/view.jsp";
        }
    
    }
    

As you can see, MVC render commands are easy to implement and can respond to multiple command names for multiple portlets.

Creating an MVC Portlet

Configuring the View Layer

MVC Resource Command

MVC Action Command

MVC Command Overrides

« MVC Action CommandMVC Resource Command »
この記事は役に立ちましたか?
0人中0人がこの記事が役に立ったと言っています