调试Spring MVC时发现如下异常:

java.lang.ClassCastException: org.springframework.web.servlet.DispatcherServlet cannot be cast to javax.servlet.Servlet

 

问题出在servlet-api.jar上,tomcat部署springMVC项目时是不用这个包的,而用maven构建项目时需要,如果不引用该依赖就会不识别  HttpServletRequest等的类

解决方法是在maven引用servlet的依赖时,加上属性范围<scope>provided</scope>



而在引用javax.servlet.jsp的依赖时,如果也没有加上  属性范围<scope>provided</scope>,则也会抛出如下异常:



org.apache.jasper.JasperException: Unable to read TLD "META-INF/c.tld" from JAR file "file:/E:/chenzehe/mvnrepo/javax/servlet/jstl/1.2/jstl-1.2.jar": org.apache.jasper.JasperException: Failed to load or instantiate TagLibraryValidator class: org.apache.taglibs.standard.tlv.JstlCoreTLV



附:




依赖范围(scope)

主源码classpath可用

测试源码classpath可用

会被打包

compile 缺省值

TRUE

TRUE

TRUE

test

FALSE

TRUE

FALSE

runtime

FALSE

TRUE

TRUE

provided

TRUE

TRUE

FALSE