1. What is the main purpose of the @RequestAttribute annotation?
Why:@RequestAttribute gives a handler method access to an attribute that is already stored in the current request.
2. Which annotation is used to read a request attribute that was set earlier on the server side?
Why:@RequestAttribute reads attributes stored in the request. The other annotations read different data, such as query parameters, headers or URL path values.
3. Where do request attributes usually come from?
Why: Request attributes are set by server-side code during the same request. Filters and interceptors are common places to set them.
4. How long does a request attribute stay available?
Why: A request attribute belongs to one HTTP request only. When the request is finished, the attribute is gone.
5. What is the main difference between a request parameter and a request attribute?
Why: Request parameters come from the client, for example from the query string. Request attributes are objects that server-side code puts into the request.
6. A client sends the URL http://example.com?id=5. Can @RequestAttribute read the value of id?
Why: Values in the query string are request parameters. Use @RequestParam to read them. @RequestAttribute only reads attributes stored in the request.
7. Which servlet method does Spring use to get the value for a parameter annotated with @RequestAttribute?
Why: Spring calls ServletRequest#getAttribute(String name) to get the value of the named request attribute.
8. What is the default value of the required element of @RequestAttribute?
Why: By default, required is true. The attribute must be present in the request.
9. What happens when the attribute is missing and required is true?
Why: A required attribute must exist. If it is missing, Spring throws an exception.
10. What does the handler method parameter get when the attribute is missing and required is set to false?
Why: With required = false, no exception is thrown. The parameter value is null when the attribute is not present. If the handler method parameter has a primitive type (such as int), it will fail during argument resolution/invocation.
11. Which line correctly marks a request attribute named user as optional?
Why: The required element controls whether the attribute must be present. Setting it to false makes the attribute optional.
12. Which elements of @RequestAttribute can be used to give the name of the attribute?
Why:value and name are aliases for each other. Both set the name of the request attribute to bind.
13. Why is @RequestAttribute often better than reading the attribute directly from HttpServletRequest?
Why: With the annotation, Spring converts the value to the parameter type and checks if the attribute is required. You do not need to write this code yourself.