spring-mvcannotationsspring-portlet-mvc

Difference between @ActionMapping,@RequestMapping,@Rendermapping


The differences between @RequestMapping, @ActionMapping and @RenderMapping are not very clear to me. It seems that all of the above ultimately map a request url to an actual method in the controller. So what is the difference? What can be done by @Rendermapping that cannot be done by @Requestmapping?


Solution

  • @RequestMapping allows you to specify the mode (EDIT, VIEW etc) and many other options (although not the window state). @RenderMapping allows you to specify only the request parameters and the window state (MAXIMISED, NORMAL etc), and is a convenient way of indicating a Render request mapping. You can use both annotations on the same class. For example you might annotate at class level with RequestMapping to indicate it serves requests for VIEW mode, and then at method level annotate the handlers for specific requests with @RenderMapping (or @ActionMapping or @ResourceMapping).

    It is probably helpful to say something about the different types of request now.

    Render requests are used to render HTML, so typically you would display a JSP or some other type of view.

    Action requests are used to perform an action such as modifying data, uploading a file etc. It's important to note that each action request is followed by a render request. So if you have two portlets on your page, and you make an action request to portlet one, that will receive an action request followed by a render request. Portlet two will just receive a render request.

    There's another type of request which you don't mention which is also of interest

    Resource requests can be used to render other content types. AJAX is a common use case for resource requests as they are suitable for returning JSON or XML for example.

    Event requests also exist and can be used for inter-portlet communication.

    I can recommend the Spring portlet docs. Although they do not cover the annotations well, they do explain the different request types and you should be able to use that information to work out the annotations.

    NB. You can use @RequestMapping to define render, action and resource requests if you like, but @RenderMapping, @ActionMapping or @ResourceMapping offer more convenient ways to do so.