在Spring中,我可以使用一個Principal
對象作爲Web控制器方法的參數,並將其填充。例如:在Spring中,如何自動填充web @Controller方法的自定義參數?
public String listTransactionsForAccount(Model model, Principal principal) {
String username = principal.getName();
// do something with the username
}
我的問題是:我怎麼能傳遞一個自定義對象(Account
),並把它填充,類似Spring如何填充Principal
?
在我的應用程序模型中,我有一個設置,其中存在0..n User
s/Account
。我可以得到基於Principal
的用戶,但我真正的是Account
。我發現,我必須這樣做在很多地方:
String username = principal.getName();
User user = userService.findByUsername(username);
Long accountId = user.getAccount().getId();
這將是理想的,如果我可能只是這樣做:
public String listTransactionsForAccount(Model model, Account account) {
// do stuff directly with the account
}
我怎麼會去這樣做呢?
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/method/support/HandlerMethodArgumentResolver.html最有可能 –