1

Spring 3.2.3 を使用して、REST-ful URL を処理する単純な CRUD コントローラーを実装しようとしています。これは、PropertyEditor に依存して、パス変数をアプリケーション サービスからロードすることによって BusinessService エンティティに変換します。コードは次のとおりです。

@Controller
public class BusinessServiceController {
    @Autowired
    private BusinessServiceService businessSvcService;

    public BusinessServiceController() {
    }

    @InitBinder
    public void initBinder(final WebDataBinder binder) {
        binder.registerCustomEditor(BusinessService.class, new BusinessServicePropertyEditor(businessSvcService));
    }

    @RequestMapping(value = "/ui/account/business-services/{businessSvc}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public ModelAndView update(@ModelAttribute("businessSvc") @Valid final BusinessService businessSvc, final BindingResult result,
            final RedirectAttributes redirectAttribs) throws UnknownBusinessServiceException {
        ModelAndView mav;

        if (result.hasErrors()) {
            mav = new ModelAndView("/business-service/edit");
        }
        else {
            businessSvcService.updateBusinessService(XSecurity.principal().getId(), businessSvc);

            mav = new ModelAndView("redirect:/ui/account/business-services");
            redirectAttribs.addFlashAttribute("message", Message.info("businessService.updated", businessSvc.getTitle()));
        }

        return mav;
    }
}

public class BusinessServicePropertyEditor extends PropertyEditorSupport {
    private final BusinessServiceService businessSvcService;

    public BusinessServicePropertyEditor(final BusinessServiceService businessSvcService) {
        this.businessSvcService = businessSvcService;
    }

    @Override
    public String getAsText() {
        final BusinessService svc = (BusinessService) getValue();
        return Long.toString(svc.getId());
    }

    @Override
    public void setAsText(final String text) {
        final BusinessService svc = businessSvcService.getBusinessService(Long.parseLong(text));
        setValue(svc);
    }
}

SPR-7608によると、Spring 3.2 以降、 @ModelAttribute メソッドの引数解決は、同じ名前のパス変数が存在するかどうかを確認し (ここでは存在します)、そのパス変数の値を登録済みを介してターゲット パラメーターの型に変換しようとします。コンバーターと PropertyEditor。これは私が経験していることではありません。ServletModelAttributeMethodProcessor の動作を調べると、要求 DataBinder の ConversionService を使用して型変換を実行することが明確であり、登録済みの PropertyEditor は考慮されないため、BusinessServicePropertyEditor#setAsText が呼び出されることはありません。

これは構成の問題ですか、それとも実際のバグですか?

ご協力いただきありがとうございます!

4

1 に答える 1

1

Spring のConversionServiceおよびConverterは、標準の Java Beans に置き換わるものですPropertyEditor

この機能が純粋に変換サービスに基づいている場合は、Converter代わりに実装する必要があります。PropertyEditor


カスタム コンバーターを登録するには、またはメソッドWebDataBinderを使用できます。ConfigurableWebBindingInitializer@InitBinder

于 2013-06-16T08:53:23.940 に答える