@PathVariable和@ModelAttribute的值重叠 [英] Values of @PathVariable and @ModelAttribute overlapping

查看:193
本文介绍了@PathVariable和@ModelAttribute的值重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与@SessionAttributes的会话中存储了一个User对象.还有一个用@ModelAttribute装饰的简单方法,以便在会话的值为null时对其进行初始化.

I have an User object stored in the session with @SessionAttributes. And a straight-forward method decorated with @ModelAttribute in order to initialize it whenever the session's value is null.

用户类别:

@Entity
@Table( name="USER")
public class User implements java.io.Serializable {

    private Long id;
    private String username;
    private String password;
    ....

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name ="ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    ...

控制器:

@RequestMapping("/item")
@Controller
@SessionAttributes({"user"})
public class MyController {

@ModelAttribute方法:

@ModelAttribute method:

@ModelAttribute("user")
    public User createUser(Principal principal) {
        return userService.findByUsername(principal.getName());
    }

除此特定方法外,一切似乎都能按预期工作:

It all seems to work as expected except in this particular method:

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String showItem(@PathVariable("id") Long id, @ModelAttribute("user") User user,
            Model uiModel) {
   ...    
}

问题是使用@PathVariable("id") 设置了User.id.我相信我也遇到了@RequestParam这个问题.我假设这是因为两者具有相同的名称和类型.阅读 Spring的文档(见下文),我假设这是预期的行为:

The problem is that User.id is being set with @PathVariable("id"). I believe I ran into this with @RequestParam too. I'm assuming that's because both have the same name and type. After reading Spring's documentation (see below) I'm assuming this is expected behavior:

下一步是数据绑定. WebDataBinder类将请求参数名称(包括查询字符串参数和表单字段)匹配,以按名称对属性字段进行建模.在必要时应用了类型转换(从String到目标字段类型)后,将填充匹配字段.

The next step is data binding. The WebDataBinder class matches request parameter names — including query string parameters and form fields — to model attribute fields by name. Matching fields are populated after type conversion (from String to the target field type) has been applied where necessary.

但是,我认为这种情况相当普遍,其他人如何处理呢?如果我的发现是正确的,并且这是预期的行为(或错误),则似乎很容易出错.

However, I would think this scenario is fairly common, how are other people handling this? If my findings are correct and this is expected behavior (or bug), this seems to be very error prone.

可能的解决方案:

  1. @PathVariable("id")更改为@PathVariable("somethingElse").可以,但是@RequestParam并不是那么简单(例如,我不知道如何将jqgrid的请求参数ID更改为其他名称,但这是另一个问题).
  2. @PathVariable("id")类型从Long更改为Int.这将使User.idid类型不同,但是对Long的强制转换看起来很丑陋:)
  3. 在此处不要使用@ModelAttribute,并再次在数据库中查询User.与其他方法不一致,并且涉及冗余的数据库调用.
  1. Change @PathVariable("id") to @PathVariable("somethingElse"). Works but it's not as straightforward with @RequestParam (e.g. I don't know how to change jqgrid's request parameter id to something else but this is another issue).
  2. Change @PathVariable("id") type from Long to Int. This will make User.id and id types differ but the cast to Long looks ugly :)
  3. Don't use @ModelAttribute here and query the DB for User again. Not consistent with other methods and involves redundant DB calls.

有什么建议吗?

推荐答案

这种方法怎么样-

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String showItem(@PathVariable("id") Long id,
            Model uiModel) {
       User user = (User)uiModel.asMap().get("user");
   ...    
}

这篇关于@PathVariable和@ModelAttribute的值重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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