简体   繁体   English

Spring MVC部分对象绑定

[英]Spring MVC partially object binding

As far I know, Spring MVC allows to bind objects like that: 据我所知,Spring MVC允许绑定这样的对象:

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

But does there exists an elegant solution, which allows to bind the only specific fields? 但是,是否存在一个优雅的解决方案,该解决方案仅允许绑定特定的字段?

For example, class User may possible contain private information such as registration timestamp which could be easily replaced by 3d-party side in case of using common object binding. 例如,类User可能包含私有信息,例如注册时间戳,在使用公共对象绑定的情况下,很容易被3d方替换。

So needed is smth like: 因此需要的是:

class User {

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

Any ideas? 有任何想法吗?

Try with data binder initialization. 尝试进行数据绑定程序初始化。

For example using @InitBinder annotation in the controller: 例如,在控制器中使用@InitBinder批注:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM