>带有未注释的控制器

发布于 2024-12-01 21:20:01 字数 656 浏览 0 评论 0原文

我的项目包括较旧的未注释控制器和较新的基于注释的控制器。

我正在使用最新的 Spring jar (3.0.5),并且在我的 Dispatcher-servlet.xml 中有

问题是 导致请求映射(通过 Dispatcher-servlet.xml 中控制器 bean 的 name 属性)到我未注释的控制器工作...每次我将请求定向到未注释的控制器时,我都会收到一条错误消息,例如:

org.springframework.web.servlet.DispatcherServlet noHandlerFound  
WARNING: No mapping found for HTTP request with URI [/some_path/some_page.htm] in DispatcherServlet with name 'dispatcher'

如何保持未注释的控制器不变,但告诉 spring 识别它们的(旧式)映射?

我正在寻找对我已有的控制器的 Java 代码进行最小更改的解决方案。

谢谢!

My project includes older un-annotated controllers together with newer annotation-based controllers.

I am using the latest Spring jars (3.0.5) and in my dispatcher-servlet.xml there's <mvc:annotation-driven />.

The problem is that <mvc:annotation-driven /> causes the request mapping (through the name property of the controller beans in the dispatcher-servlet.xml) to my un-annotated controllers not to work... each time I direct the request to an un-annotated controller I am getting an error message such as:

org.springframework.web.servlet.DispatcherServlet noHandlerFound  
WARNING: No mapping found for HTTP request with URI [/some_path/some_page.htm] in DispatcherServlet with name 'dispatcher'

How can I keep the un-annotated controllers as they are but tell spring to recognize their (old style) mapping?

I am looking for solutions with minimum change to the Java code of the controllers that I already have.

Thanks!

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

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

发布评论

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

评论(2

断爱 2024-12-08 21:20:01

当您将 添加到配置中时,它会替换默认的处理程序映射和处理程序适配器集,而这些默认值是处理旧式控制器的默认值。

你有两个选择。首先要尝试的是删除 。如果没有这个,您仍然可以使用带注释的控制器。它确实添加了额外的功能,例如 Jackson JSON 支持,但如果您不需要这些额外的功能,那么您就不需要它。因此,请尝试不使用 的应用程序,看看它是否仍然有效。

如果失败,您可以恢复旧控制器的映射和适配器。您没有说明控制器过去如何映射其 URL,但尝试将这些添加到您的配置中:

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.handler.ControllerClassNameHandlerMapping"/>

如果您使用 SimpleUrlHandlerMapping,那么它应该已经可以工作了。

您还需要将 HandlerAdapter 添加回来:

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

不要盲目添加这些。单独尝试它们,看看让旧控制器与新控制器一起工作的最小设置是什么。

When you add <mvc:annotation-driven /> to your config, it replaces the default set of handler mappings and handler adapters, and those defaults were the ones that handled the old-style controllers.

You have 2 options. First thing to try is to remove <mvc:annotation-driven />. You can still use annotated controllers without this. It does add extra features like Jackson JSON support, but if you don't need those extra features, then you don't need it. So try your app without <mvc:annotation-driven /> and see if it still works.

Failing that, you can reinstate the mappings and adapters for your old controllers. You didn't say how your controllers used to have their URLs mapped, but try adding these to your config:

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.handler.ControllerClassNameHandlerMapping"/>

If you used SimpleUrlHandlerMapping, then that should be working already.

You also need to add the HandlerAdapter back in:

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

Don't just add these in blindly. Try them individually, and see what the minimal set is to get your old controllers working alongside the new ones.

我不是你的备胎 2024-12-08 21:20:01

我发现通过将 mvc:annotation-driven 分解为实际的替换,更容易弄清楚。

爆炸如下:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="0" />
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="validator" ref="validator" />
        </bean>
    </property>
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
            <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
        </list>
    </property>
</bean>

<!-- Configures a validator for spring to use -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="messageInterpolator">
        <bean class="com.eps.web.spring.validation.SpringMessageSourceMessageInterpolator" />
    </property>
</bean>

<bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />

一旦魔法消失,我在尝试加载“旧”控制器时遇到此错误:
javax.servlet.ServletException:没有处理程序的适配器[org.springframework.web.servlet.mvc.ParameterizedViewController@

从那时起,我添加了

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

并且所有旧控制器都工作了。

I found that by exploding the mvc:annotation-driven into it's actual replacement, it was easier to figure out.

<mvc:annotation-driven /> explodes to this:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="0" />
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="validator" ref="validator" />
        </bean>
    </property>
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
            <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
        </list>
    </property>
</bean>

<!-- Configures a validator for spring to use -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="messageInterpolator">
        <bean class="com.eps.web.spring.validation.SpringMessageSourceMessageInterpolator" />
    </property>
</bean>

<bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />

Once the magic was gone, I got this error trying to load an "Old" controller:
javax.servlet.ServletException: No adapter for handler [org.springframework.web.servlet.mvc.ParameterizableViewController@

From that point, I added

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

And all my old controllers worked.

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