Spring MVCのカスタムエディターを使用して、文字列値をドメインオブジェクトにマップしています。単純なケース:ユーザーオブジェクトは会社を参照します(User.company-> Company)。ユーザーフォームでデータバインダーを登録します。
protected void initBinder(WebDataBinder binder) throws Exception {
binder.registerCustomEditor(Company.class, new CompanyEditor(appService));
}
エディタは次のように定義されます。
class CompanyEditor extends PropertyEditorSupport {
private AppService appService;
public CompanyEditor(AppService appService) {
this.appService = appService;
}
public void setAsText(String text) {
Company company = appService.getCompany(text);
setValue(company);
}
public String getAsText() {
Company company = (Company) this.getValue();
if (company != null)
return company.getId();
return null;
}
}
フォームでドロップダウンを使用する場合
<form:select path="company">
<form:options items="${companies}" itemLabel="name" itemValue="id"/>
</form:select>
(会社が選択されているかどうかを確認するために)オプションごとにsetAsTextとgetAsTextを起動し、会社ごとにSQLクエリを実行するため、パフォーマンスに深刻な問題が発生します。
フォームをコミットして、会社のIDをCompany(永続化された)オブジェクトに変換する方法をアプリケーションに知らせるときに、setAsTextが使用されると思いました。ドロップダウンで起動する必要があるのはなぜですか。それを修正する方法はありますか?