簡體   English   中英

Spring MVC部分對象綁定

[英]Spring MVC partially object binding

據我所知,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
   ...
}

有任何想法嗎?

嘗試進行數據綁定程序初始化。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM