简体   繁体   中英

Spring MVC partially object binding

As far I know, Spring MVC allows to bind objects like that:

@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.

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
public void initBinder(WebDataBinder binder) {
  binder.setDisallowedFields("timestamp"); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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