我今天用spring mvc做了一些测试,我有一个问题。对于要测试的基本应用程序,我选择本教程: http://loianegroner.com/2010/09/extjs-spring-mvc-3-and-hibernate-3-5-crud-datagrid-example/ 我已经下载了示例,一切都按预期工作。问题是当我尝试添加另一个Controller时。似乎我正在添加的文件未被扫描。
我的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:
<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" />
正如你所看到的那样有上下文:component-scan和mvc:annotation-driven标签,它们应该告诉spring扫描com.lione包中@Controller注释的所有类。但是当我尝试将自己的控制器添加到此应用程序时,它似乎没有被处理。只映射了第一个控制器。
这是我的控制器代码
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;
}
}
在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'
但缺少/helloWorld.action
提前致谢
答案 0 :(得分:3)
使用<context:component-scan base-package="package-name" />
时,有时候对您的路径更具体一点是个好主意
所以你应该做<context:component-scan base-package="com.loiane.web" />