【问题标题】:Spring MVC partially object bindingSpring MVC 部分对象绑定
【发布时间】:2015-08-22 10:21:09
【问题描述】:

据我所知,Spring MVC 允许这样绑定对象:

@RequestMapping(...)
public void doSmth(MyObject obj) {
// All MyObject's fields are filled now
}

但是是否存在一个优雅的解决方案,它允许绑定唯一的特定字段?

例如,User 类可能包含诸如注册时间戳之类的私有信息,在使用公共对象绑定的情况下,这些信息可以很容易地被 3d 方替换。

所以需要的是这样的:

class User {

   public String nick; <-- wanna bind this
   public String pass; <-- and this
   public Calendar timestamp; <-- but not this
   ...
}

有什么想法吗?

【问题讨论】:

    标签: spring spring-mvc data-binding


    【解决方案1】:

    尝试使用数据绑定器初始化。

    例如在控制器中使用@InitBinder注解:

    @InitBinder
    public void initBinder(WebDataBinder binder) {
      binder.setDisallowedFields("timestamp"); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-17
      • 2014-05-08
      • 2013-02-14
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      相关资源
      最近更新 更多