背景(Background)
- 使用Eclipse + Tomcat + Maven Project组合
- 在Eclipse中启用了Serve modules without publishing模式来部署Maven Project到Tomcat
- 在Eclipse中,Tomcat以调试模式运行
问题(Problem)
用浏览器访问Maven Project中一个页面(该页面用到jstl),服务器响应500错误,消息是:
The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
用浏览器访问Maven Project中一个页面(该页面用到spring-tag),Tomcat响应500错误,消息是:
The absolute uri: http://www.springframework.org/tags/form cannot be resolved in either web.xml or the jar files deployed with this application
解决(Solution)
方法1
不要在Eclipse中启用Serve modules without publishing模式来部署项目到Tomcat。
如果喜欢折腾,可以尝试方法2
方法2
设Maven Project的pom.xml有
<!--jstl-->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
第1步:把内含相关tld的jar文件复制到META-INF/lib中
spring-webmvc-4.3.6.RELEASE.jar
和taglibs-standard-impl-1.2.5.jar
这些jar文件中的META-INF目录包含报错uri对应tld文件,在当前用户目录中.m2\repository
中搜索找到它们,并将其复制到项目src/main/webapp/WEB-INF/lib
第2步:在pom.xml中调整dependency
让相关的dependency使用WEB-INF/lib中的文件,指定scope
为system并指定systemPath为WEB-INF/lib中相应jar文件的路径
<!--jstl-->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/taglibs-standard-impl-1.2.5.jar</systemPath>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/spring-webmvc-4.3.6.RELEASE.jar</systemPath>
</dependency>