我最近在进行一些关于Spring MVC的测试,但是我遇到了一个问题。我选择了这个教程来做一个基本的测试应用:
http://loianegroner.com/2010/09/extjs-spring-mvc-3-and-hibernate-3-5-crud-datagrid-example/。
我下载了这个例子,一切都按照预期正常工作。但是当我尝试添加另一个控制器时,似乎添加的文件没有被扫描到。
我的web.xml文件如下:
在Eclipse控制台日志中运行后,我得到了以下结果:
我的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" />
如您所见,这里有context: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不存在
提前致谢