将 @InitBinder 初始化外部化到 WebBindingInitializer 中

发布于 2024-11-29 01:35:16 字数 1067 浏览 1 评论 0 原文

数据绑定初始化有两种主要方法,但老式方法有一个缺点,我无法弄清楚。这种注释方式很棒:

@InitBinder("order")
public void initBinder(WebDataBinder binder) {
    // Problem is that I want to set allowed and restricted fields - can be done here
    binder.setAllowedFields(allowedFields.split(","));
}

但我无法使用 ConfigurableWebBindingInitializer 来完成。首先,绑定器实例是在 AnnotationMethodHandlerAdapter 中创建的,并且初始化程序是在 HandlerMethodInvoker 中的某处传递绑定器实例的,所以我无法设置它......我不能做这样的事情:

<bean id="codesResolver" class="org.springframework.validation.DefaultMessageCodesResolver" />
<bean id="binder" class="org.springframework.web.portlet.bind.PortletRequestDataBinder" scope="prototype">
    <property name="allowedFields" value="${allowedFields}" />
    <aop:scoped-proxy />
</bean>
<bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
    <property name="messageCodesResolver" ref="codesResolver" />
</bean>

因为绑定器实例是在 handlerAdapter 中传递给它的。那么我该如何设置活页夹呢?

There are two major means of data binding initialization, but there is a drawback in the oldschool one, that I can't figure out. This annotation way is great :

@InitBinder("order")
public void initBinder(WebDataBinder binder) {
    // Problem is that I want to set allowed and restricted fields - can be done here
    binder.setAllowedFields(allowedFields.split(","));
}

but I can't be done with ConfigurableWebBindingInitializer. First off, the binder instance is created in AnnotationMethodHandlerAdapter and initializer is passed the binder instance somewhere in HandlerMethodInvoker so I can't set it up... I can't do something like this :

<bean id="codesResolver" class="org.springframework.validation.DefaultMessageCodesResolver" />
<bean id="binder" class="org.springframework.web.portlet.bind.PortletRequestDataBinder" scope="prototype">
    <property name="allowedFields" value="${allowedFields}" />
    <aop:scoped-proxy />
</bean>
<bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
    <property name="messageCodesResolver" ref="codesResolver" />
</bean>

Because binder instance is passed into it in handlerAdapter. How can I set up the binder then ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚笙 2024-12-06 01:35:16

没有办法在 xml 配置中进行设置。您必须实现自定义的 WebBindingInitializer ... ConfigurableWebBindingInitializer 显然缺少设置允许和限制字段的可能性...

或者您可以投票 SPR-8601

There is no way of setting it up in xml configuration. You must implement your custom WebBindingInitializer ... The ConfigurableWebBindingInitializer is obviously missing the possibility of setting up allowed and restricted fields...

Or you can vote up SPR-8601

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文