How is used @RequestAttribute and @ModelAttribute annotation in this Spring MVC showcase example?

24,510

You are correct in assumption of @RequestAttribute, it need not be set in beforeInvokingHandlerMethod. Assume you have a method mapped to /data/init which forwards request to /data/custom. In this case request attribute can be set in init method also.

Why the beforeInvokingHandlerMethod() is called before the custom() method?

And why the beforeInvokingHandlerMethod() is annoted by @ModelAttribute annotation? what means in this case?

you will get the reason here http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods

An @ModelAttribute on a method indicates the purpose of that method is to add one or more model attributes. Such methods support the same argument types as @RequestMapping methods but cannot be mapped directly to requests. Instead @ModelAttribute methods in a controller are invoked before @RequestMapping methods, within the same controller.

Share:
24,510
AndreaNobili
Author by

AndreaNobili

Updated on July 09, 2022

Comments

  • AndreaNobili
    AndreaNobili almost 2 years

    I am pretty new in Spring MVC.

    In this period I am studying the Spring MVC showcase example downlodable from STS dashboard.

    I am having some problems understanding how Custom Resolvable Web Arguments are handled in this example.

    In practice I have the following situation:

    In my home.jsp view I have the following link:

    <a id="customArg" class="textLink" href="<c:url value="/data/custom" />">Custom</a> 
    

    This link generate an HTTP Request towards the URL: "/data/custom"

    The controller class that contains the method that handles this request has the following code:

    @Controller
    public class CustomArgumentController {
    
    @ModelAttribute
    void beforeInvokingHandlerMethod(HttpServletRequest request) {
        request.setAttribute("foo", "bar");
    }
    
    
    @RequestMapping(value="/data/custom", method=RequestMethod.GET)
    public @ResponseBody String custom(@RequestAttribute("foo") String foo) {
        return "Got 'foo' request attribute value '" + foo + "'";
    }
    
     }
    

    The method that handles this HTTP Request is custom()

    So when the previous link is clicked the HTTP Request is handled by the custom method...

    I am having problems understanding what the @RequestAttribute annotation.

    I think that, in this case, it binds the request attribute named foo to a new String foo variable.

    But... where is this attribute taken from? Is this variable taken by Spring?

    Ok...my idea is that this request attribute is taken from a HttpServletRequest object...

    I think this because, in this class, I have also have the beforeInvokingHandlerMethod() method that have a speacking name...so it seems that this method seta an attribute, that have name=foo and value=bar, inside an HttpServletRequest object...and then so the custom() method can use this value...

    In fact my output is:

    Got 'foo' request attribute value 'bar'

    Why is the beforeInvokingHandlerMethod() called before the custom() method?

    And why is the beforeInvokingHandlerMethod() annoted by @ModelAttribute annotation? What does this case mean?