clipse創建Maven結構的web項目的時候選擇Artifact Id為maven-artchetype-webapp,點擊finish之後,一般會遇到如下問題 1. The superclass "javax.servlet.http.HttpServlet" was not found on ...
clipse創建Maven結構的web項目的時候選擇Artifact Id為maven-artchetype-webapp,點擊finish之後,一般會遇到如下問題
1. The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 錯誤,
這是eclipse環境里沒有SeverRuntime導致的,在BuildPath裡加入即可,如下圖:
添加前:
選擇 add library
選擇 Apache Tomcat V7.0 並點擊 next
點擊finish之後,完成添加如下圖
2. 如何Maven創建動態Web項目後產生的版本不匹配的問題
我們用Eclipse創建Maven結構的web項目的時候選擇了Artifact Id為maven-artchetype-webapp,由於這個catalog比較老,用的servlet還是2.3的、java版本是1.5的,而一般現在至少都是3.0/1.7,因此我們需要逐個修改!
(1) 修改JRE 版本
Go to project Build Path and change the Java Library version to 1.7
刪除原先的低版本JRE,並添加workplace預設的版本(JDK1.7),完成後如下:
(2) Eclipse Preferences -> Java -> Compilre -> Change compliance level to 1.7
(3) 修改 Project Facets 版本(註意順序)
在項目上單機右鍵 -> Properties -> Project Facets -->取消選中 Dynamic Web Module 狀態,點擊 Apply -->將 Java facet 版本變為 1.7 ,點擊 Apply如下圖
-->將 Dyanmic Web Module 版本更改為3.0, 點擊 Apply.
此時會有較大幾率提示: web.xml is missing and <failOnMissingWebXml> is set to true,如下圖
解決辦法,在項目上單擊右鍵-->java EE Tools ---> Generate Deployment Descriptro Stub
OK 問題解決
3. 解決發佈之後404錯誤
預設情況下因為預設的deployment assembly中 webapp路徑的問題,如下圖可以看到預設的webapp下的頁面都被髮布到WEB-INF/class下了,
導致出現瞭如下圖的目錄結構,所以所有的jsp都無法訪問以及通過web.xml載入的ssm環境無法初始化!
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source'
出現SetPropertiesRule警告的原因是因為Tomcat在server.xml的Context節點中不支持source屬性:<Context docBase="…" path="/…" source="org.eclipse.jst.j2ee.server:…"/>
解決方法是在Servers視圖裡雙擊創建的server,然後在其server的配置界面中選中"Publish module contexts to separate XML files"選項。
具體“Publish module contexts to separate XML files”是什麼意思,
請看Tomcat Publishing Options介紹:
Two new options which affect publishing are now available in the Server Options section of the Tomcat server editor. The Tomcat server must be 5.0.x or later for these options to be enabled. The Serve modules without publishing option does what it says. Web content will be served directly from the "WebContent" folder of the Dynamic Web Project. A customized context is used to make the project's dependencies available in the Web application's classloader. The Publish module contexts to separate XML files option will publish contexts using the preferred method of separate XML files under the "conf/Catalina/localhost" directory, rather than keeping them in the "server.xml" file. A couple of improvements for this option are noted in Bugs 180931 and 180936.
關於解決方法,再詳細說明一下:
Servers視圖的打開方法:Window--Show View-other..--Servers
雙擊Server:就是雙擊伺服器名,我的伺服器名為:Tomcat v6.0 Server at localhost 即雙擊它,進入
server的配置界面: 選中"Publish module contexts to separate XML files"選項
web.xml is missing and <failOnMissingWebXml> is set to true解決方法
這種錯誤是因為maven預設簡單構建項目是sevlet3.0版本,web.xml不是必須的,這時候需要手動創建webapp/WEB-INF/web.xml,web.xml可以從其他項目複製一個過來改改,
或者pom.xml添加如下配置
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
修改完了別忘了右鍵項目Mavan/update project..