背景(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有
<!--old jstl--><!--
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>-->
<!--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步:把相關的jar檔案複制到META-INF/lib中
spring-webmvc-4.3.6.RELEASE.jar
和
taglibs-standard-impl-1.2.5.jar
這些jar檔案中的META-INF目錄包含報錯uri對應tld檔案,在目前使用者目錄(如
C:\Users\Administrator
)的
.m2\repository
中搜尋找到它們,并将其複制到
src/main/webapp/WEB-INF/lib
第2步:在pom.xml中定義屬性
<properties>
<webapp.libdir>${project.basedir}/src/main/webapp/WEB-INF/lib</webapp.libdir>
</properties>
其中
project.basedir
為maven内置屬性,無須我們再定義
第3步:在pom.xml中調整dependency
讓相關的dependency使用WEB-INF/lib中的檔案,指定
scope
為system并指定systemPath為WEB-INF/lib中相應jar檔案的路徑
<dependencies>
<!--...-->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
<scope>system</scope>
<systemPath>${webapp.libdir}/taglibs-standard-impl-1.2.5.jar</systemPath>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
<scope>system</scope>
<systemPath>${webapp.libdir}/spring-webmvc-4.3.6.RELEASE.jar</systemPath>
</dependency>
</dependencies>