我在控制器上使用带有
和 @RequestMapping
注释的 Spring 3.0.5。这可行,并且 URL 由包扫描注册。
但是,当我在 XML 配置中定义了处理程序映射时,就会出现问题。不再选取 @RequestMapping
注释。
我已将问题隔离到一个简单的应用程序。
如果我有以下控制器:
package test.pack.age;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping(value="/test")
public String showTestPage() {
return "testPage";
}
}
和以下配置:
<context:component-scan base-package="test.pack.age" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
应用程序正常工作并且 URL /test
已注册并正常工作。
18/09/2011 20:02:55 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping INFO Mapped URL path [/test] onto handler 'testController'
18/09/2011 20:02:55 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping INFO Mapped URL path [/test] onto handler 'testController'
但是,如果我将处理程序映射添加到 XML,它就不再起作用。即使是像这样简单的东西:
<bean id="handlerMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" />
它基本上什么也不做,并且
不再注册我的 URL。
我需要为某些(第三方)控制器提供额外的处理程序映射,但我无法对其进行注释,但是在添加它时,它会破坏我的所有 @RequestMapping
。
这是正常的吗?一个错误? (我无法更改 Spring 版本)
我错过了什么吗?
I'm using Spring 3.0.5 with <context:component-scan>
and @RequestMapping
annotations on my controllers. This works, and URLs are registered by the package scan.
But there is a problem when I have a handler mapping defined in the XML config. The @RequestMapping
annotations are no longer picked up.
I've isolated the problem to a simple application.
If I have the following controller:
package test.pack.age;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping(value="/test")
public String showTestPage() {
return "testPage";
}
}
and the following configuration:
<context:component-scan base-package="test.pack.age" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
The application works correctly and the URL /test
is registered and works properly.
18/09/2011 20:02:55 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping INFO Mapped URL path [/test] onto handler 'testController'
18/09/2011 20:02:55 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping INFO Mapped URL path [/test] onto handler 'testController'
But if I add a handler mapping to the XML it no longer works. Even something simple like this:
<bean id="handlerMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" />
which basically does nothing, and the <context:component-scan>
no longer registers my URL.
I need an extra handler mapping for some (third party) controllers which I can't annotate, but when adding it it breaks all my @RequestMapping
s.
Is this normal? A bug? (I can't change the Spring version)
Am I missing something?
发布评论
评论(2)
我错过了一些东西:D。发现这个埋在Spring的JavaDocs中 DefaultAnnotationHandlerMapping:
将其添加到我的配置 XML 中,现在一切正常:
I was missing something :D. Found this buried in the Spring's JavaDocs for DefaultAnnotationHandlerMapping:
Added this to my config XML and now everything works:
我不确定为什么您的带注释的
Controller
不再被拾取,但我认为您的问题在于您提供的SimpleUrlHandlerMapping
的id
。它应该是handlerMapping
。I am not sure why your annotated
Controller
is not being picked up anymore, but I think your issue lies in theid
you provide yourSimpleUrlHandlerMapping
. It should behandlerMapping
.