7

I am pretty new in Spring MVC. Currently I am studying the Spring MVC Showcase, that demonstrates the features of the Spring MVC web framework.

I have some problem to understand 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 contain the method that handles this request have 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 have some problem to understand what exactly does the @RequestAttribute annotation do. I think that, in this case, it binds the request attribute named foo to a new String foo variable. But where this attribute is taken from? Is this variable taken by Spring?

OK, my idea is that this request attribute is taken from a HttpServletRequest object. I think so because, in this class, I also have the beforeInvokingHandlerMethod() method that have a speaking name, so it seems that this method sets 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 the beforeInvokingHandlerMethod() is called before the custom() method?

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

2

The RequestAttribute is nothing but the parameters which you have passed in the form submission. Lets understand with a sample example

Suppose I have the below form

<form action="...">
<input type=hidden name=param1 id=param1 value=test/>
</form>

Now, if I have the below controller which is mapped with the request url which is mapped with the form submission as below.

@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("param1") String param1 ) {
    // Here, I will have value of param1 as test in String object which will be mapped my Spring itself
}
Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
  • mmm let me understand well this thing: so when I annote my beforeInvokingHandlerMethod() method by @ModelAttribute annotation is the same thing if I have correctly passed a form with the "foo", "bar" values? and inside this method I do the request.setAttribute("foo", "bar"); ? – AndreaNobili Dec 15 '12 at 20:11
  • Excatly, you are currect some what, for better understanding the ModelAttribute you can visit the official documentation of Spring MVC http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/bind/annotation/ModelAttribute.html and also from http://blog.trifork.nl/2009/05/14/simple-forms-with-spring-mvc-2-5/ – Bhavik Ambani Dec 15 '12 at 20:14
  • I had read this documentation but I had some problem to understand well it :-( In practice you are sayng me that when I have a method that is annoted by the ModelAttribute annotation I am binding a method parameter to a named model attribute and then I can use it in some other method by using RequestAttribute annotation? But in my case what is the parameter that I am binding? is it the parameter named "foo" that have "bar" as value? Thank you very much you are very kind :-) – AndreaNobili Dec 15 '12 at 20:22