您的位置:首页 > 编程语言 > Java开发

使用SpringMVC 配置<mvc:default-servlet-handler/> 报错

2015-08-05 17:50 501 查看
最近在捣鼓一下SpringMVC,我使用的是Spring 3.0. 当我在Spring配置文件中配置<mvc:default-servlet-handler/>的时候报错了,说是找不到default-servlet-handler的声明。先给出我的Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
<span style="color:#ff0000;">xmlns:mvc="http://www.springframework.org/schema/mvc"
</span>	xsi:schemaLocation="http://www.springframework.org/schema/beans <a target=_blank href="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</a> http://www.springframework.org/schema/context <a target=_blank href="http://www.springframework.org/schema/context/spring-context-3.0.xsd">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>
<span style="color:#ff0000;">http://www.springframework.org/schema/mvc </span><a target=_blank href="http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><span style="color:#ff0000;">http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd</span></a><span style="color:#ff0000;">">
</span>

<context:component-scan base-package="com.cyx.crud"></context:component-scan>

<mvc:default-servlet-handler/>
<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

下面给出报错信息:

<span style="color:#ff0000;">org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 48 in XML document from ServletContext resource [/WEB-INF/springDispatcherServlet-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 48; columnNumber: 32; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'mvc:default-servlet-handler' 的声明。
Caused by: org.xml.sax.SAXParseException; lineNumber: 48; columnNumber: 32; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'mvc:default-servlet-handler' 的声明。</span>

根据提示信息是找不到mvc标签中的default-servlet-handler元素。我上网查了一下,有人出现这种错误是因为没有把相关的文件引进来,如上面配置文件中红色标出的部分,但是我的配置文件没问题,mvc是可以使用的,因为配置<mvc:annotation-driven/>没有问题,但是配置<mvc:default-servlet-handler/>或者<mvc:resources location="/js/" mapping="/js/**"/>就会出现这种错误。我点击http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd该链接进去看里面的源码时是找得到对于上面两个元素的声明的。捣鼓了好久都不知道是什么原因,后来想想会不会是jar包问题,如果使用其他版本的Spring会不会出错呢??果断试试,我找了Spring4.0的jar包引进来,然后以运行,嘿,不会出错了。

至此,问题解决了,具体为什么会出现这个问题不得而知,可能真的是jar包的问题,在Spring3.0中是没有Springmvc这个jar包的,但是有个SpringServlet的包,对比一下,内容差不多的,应该是后来的版本将此包改名交springmvc了。所以总结一下:如果有人遇到这种问题的话第一找下对应的标签内容是不是引进来了,如果引进来还出错的话,就有可能是jar包的问题了,这时可以试试换一下Spring版本。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc spring