java Spring MVC 控制器映射未注册
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4246831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC Controllers mapping not registered
提问by Ivan
I'm making some tests with spring mvc this days and I have one problem. For a basic application to test with I choose this tutorial: http://loianegroner.com/2010/09/extjs-spring-mvc-3-and-hibernate-3-5-crud-datagrid-example/I've downloaded the example and everything works as expected. The problem is when I try to add another Controller. It seems that file I'm adding is not scanned.
这几天我正在用 spring mvc 做一些测试,但我遇到了一个问题。对于要测试的基本应用程序,我选择本教程:http: //loianegroner.com/2010/09/extjs-spring-mvc-3-and-hibernate-3-5-crud-datagrid-example/我已经下载这个例子和一切都按预期工作。问题是当我尝试添加另一个控制器时。我添加的文件似乎没有被扫描。
My web.xml file:
我的 web.xml 文件:
<servlet>
<servlet-name>extjs-crud-grid-spring-hibernate</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/app-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>extjs-crud-grid-spring-hibernate</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
/WEB-INF/spring/app-config.xml:
/WEB-INF/spring/app-config.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.loiane" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- misc -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- Configures Hibernate - Database Config -->
<import resource="db-config.xml" />
as you can see there are context:component-scan and mvc:annotation-driven tags which should tell spring to scan all the classes in com.lione package for @Controller annotation. But when I try to add my own controller to this application it seems it's not processed. Only first controller is mapped.
正如你所看到的,有 context:component-scan 和 mvc:annotation-driven 标签应该告诉 spring 扫描 com.lione 包中的所有类以获得 @Controller 注释。但是当我尝试将我自己的控制器添加到这个应用程序时,它似乎没有被处理。只有第一个控制器被映射。
Here is my controller code
这是我的控制器代码
package com.loiane.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/helloWorld.action")
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView();
mav.setViewName("helloWorld");
mav.addObject("message", "Hello World!");
return mav;
}
}
After running this in eclipse console log I've got
在 eclipse 控制台日志中运行这个后,我得到了
INFO: Mapped URL path [/contact/delete.action] onto handler 'contactController'
INFO: Mapped URL path [/contact/create.action] onto handler 'contactController'
INFO: Mapped URL path [/contact/update.action] onto handler 'contactController'
INFO: Mapped URL path [/contact/view.action] onto handler 'contactController'
but /helloWorld.action is missing
但 /helloWorld.action 不见了
Thanks in advance
提前致谢
回答by gozie
Sometimes it is a good idea to be more specific with your path when using <context:component-scan base-package="package-name" />
So you should be doing <context:component-scan base-package="com.loiane.web" />
有时在使用时更具体地说明你的路径是个好主意<context:component-scan base-package="package-name" />
所以你应该这样做<context:component-scan base-package="com.loiane.web" />