The Missing Annotation

Have you ever wondered why Spring MVC supports @RequestParam but not @RequestAttribute? There are probably plenty of philosophical reasons for this – good or bad. But if your hands are tied and you’re stuck with, let’s say a CMS, that insists on passing context as request attributes what are you going to do?

As usual, Google provides some answers. It turns out I am not the only one who’s been asking for this feature and as of Spring 3 it is available albeit in a slightly different form. Instead of @RequestAttribute you would use @Value and SpEL.

@Controller
public class YourController {

@RequestMapping("/xyz")
public ModelAndView handle(
@Value("#{request.getAttribute('key')}") SomeClass obj) {

...
return new ModelAndView(...);
}

}