我使用Apache Tomcat 7和它的ON - Spring 3以及类路径上的所有库(编译和运行时)。
这是我的web.xml文件/WEB-INF/web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcherServlet-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
这是我在WEB-INF / dispatcherServlet-context.xml中的上下文文件:
<!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="controllers" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
将http://localhost:8080/projectname/
放入浏览器时出现的Tomcat错误是:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml];
嵌套异常是java.io.FileNotFoundException:无法打开ServletContext资源[/WEB-INF/dispatcherServlet-servlet.xml] ecc ecc ecc
请注意,错误涉及文件 dispatcherServlet-servlet.xml 而不是 dispatcherServlet-context.xml (我使用,创建和映射的那个)
谢谢大家!
答案 0 :(得分:1)
只需删除
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcherServlet-context.xml</param-value>
</context-param>
摘录并将dispatcherServlet-context.xml
重命名为dispatcherServlet-servlet.xml
。
原因:Spring和Spring MVC有两个不同的配置文件。您曾尝试使用Spring Framework的Spring MVC配置。
So for Spring : pass how many you want config files via `contextConfigLocation`
For Spring MVC : /WEB-INF/<dispatcher_servlet_name>-servlet.xml.
您可以更改Spring MVC案例的默认名称(请参阅@Stefan Lindenberg答案)
答案 1 :(得分:1)
在Spring MVC中,调度程序servlet配置的缺省位置是:/ WEB-INF/[SERVLET-NAME]-servlet.xml。您可以通过向servlet声明添加init参数来重新配置它:
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
[PATH-TO-YOUR-FILE]/[CONFIG-FILE-NAME].xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>