Spring MVC:将请求属性绑定到控制器方法参数 [英] Spring MVC: bind request attribute to controller method parameter

查看:350
本文介绍了Spring MVC:将请求属性绑定到控制器方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring MVC中,很容易将请求参数绑定到处理请求的方法参数。我只使用 @RequestParameter(name)。但是我可以对请求属性执行相同的操作吗?目前,当我想访问请求属性时,我必须执行以下操作:

In Spring MVC, it is easy to bind request parameter to method paramaters handling the request. I just use @RequestParameter("name"). But can I do the same with request attribute? Currently, when I want to access request attribute, I have to do following:

MyClass obj = (MyClass) request.getAttribute("attr_name");

但我真的想用这样的东西:

But I really would like to use something like this instead:

@RequestAttribute("attr_name") MyClass obj

不幸的是,这种方式不起作用。我可以以某种方式扩展Spring功能并添加我自己的绑定器吗?

Unfortunately, it doesn't work this way. Can I somehow extend Spring functionality and add my own "binders"?

编辑 (我正在努力实现的目标):我将当前登录的用户存储在请求属性中。因此,每当我想访问当前登录的用户(这几乎都在每个方法中)时,我必须写这个额外的行 user =(User)request.getAttribute(user); 。我想尽量缩短它,最好将它作为方法参数注入。或者如果你知道如何通过拦截器和控制器传递某些东西,我会很高兴听到它。

EDIT (what I'm trying to achieve): I store currently logged user inside request attribute. So whenever I want to access currently logged user (which is pretty much inside every method), I have to write this extra line user = (User) request.getAttribute("user");. I would like to make it as short as possible, preferably inject it as a method parameter. Or if you know another way how to pass something across interceptors and controllers, I would be happy to hear it.

推荐答案

嗯,我终于理解了模型的工作原理以及 @ModelAttribute 的用途。这是我的解决方案。

Well, I finally understood a little bit how models work and what is @ModelAttribute for. Here is my solution.

@Controller 
class MyController
{
    @ModelAttribute("user")
    public User getUser(HttpServletRequest request) 
    {
        return (User) request.getAttribute("user");
    }

    @RequestMapping(value = "someurl", method = RequestMethod.GET)
    public String HandleSomeUrl(@ModelAttribute("user") User user)  
    {
        // ... do some stuff
    }
}

标有 @ModelAttribute 注释的 getUser()方法将自动填充所有用户用户标有 @ModelAttribute 的参数。因此,当调用HandleSomeUrl方法时,调用看起来像 MyController.HandleSomeUrl(MyController.getUser(request))。至少这是我的想象。很酷的是,用户也可以从JSP视图中访问而无需任何进一步的努力。

The getUser() method marked with @ModelAttribute annotation will automatically populate all User user parameters marked with @ModelAttribute. So when the HandleSomeUrl method is called, the call looks something like MyController.HandleSomeUrl(MyController.getUser(request)). At least this is how I imagine it. Cool thing is that user is also accessible from the JSP view without any further effort.

这解决了我的问题,但我还有其他问题。是否有一个常见的地方我可以放置那些 @ModelAttribute 方法,以便它们对我的所有控制器都是通用的?我可以以某种方式从拦截器的 preHandle()方法内部添加模型属性吗?

This solves exactly my problem however I do have further questions. Is there a common place where I can put those @ModelAttribute methods so they were common for all my controllers? Can I somehow add model attribute from the inside of the preHandle() method of an Interceptor?

这篇关于Spring MVC:将请求属性绑定到控制器方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆