1. 程式人生 > >eclipse搭建maven project的spring4 spring mvc mybatis

eclipse搭建maven project的spring4 spring mvc mybatis

framework sta port cti https oca web工程 nis lang

一,先確定已經安裝好了Eclipse Java EE IDE for Web Developers我用的是如下版本

Version: Neon.3 Release (4.6.3)
Build id: 20170314-1500

然後新增一個Maven project如下圖,勾選上前2個復選框

技術分享

二,填寫groupid和artifactid等,最關鍵的是要選war類型的packaging,如下圖:

技術分享

三,然後修改如下3個地方的jdk配置為1.8並且把Project Facets中的Dynamic web module選為3.1

技術分享

技術分享

技術分享


四、修改pom.xml內容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xhl</groupId>
<artifactId>eduhomeweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>xhleduhomewebportal</name>
<description>xhl edu related home web portal</description>



<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>


<properties>
<srping.version>4.3.6.RELEASE</srping.version>
<mybatis.version>3.4.3</mybatis.version>
<mybatis-spring.version>1.3.1</mybatis-spring.version>
<mysql.version>5.1.41</mysql.version>
<log4j2.version>2.8.2</log4j2.version>
<taglibs.version>1.2.5</taglibs.version>
<dbcp2.version>2.1.1</dbcp2.version>
</properties>


<!--spring related start -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${srping.version}</version>
</dependency>
<!--spring related end -->

<!--mybatis related start -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!-- mybatis related end -->
<!-- mysql related start -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- mysql related end -->

<!-- log4j2 related start -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!-- log4j2 related end -->

<!-- taglibs related start -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>${taglibs.version}</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>${taglibs.version}</version>
</dependency>
<!-- taglibs related end -->

<!-- dbcp2 related start -->
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>${dbcp2.version}</version>
</dependency>
<!-- dbcp2 related end -->



</dependencies>

</project>

然後執行Maven選項中的Update project,執行完後再檢查下上面提到的3個jdk配置是否變化

五、然後新增一個eclipse的dynamic web project然後把WEB-INF/web.xml和META-INF目錄下的文件拷貝到Maven工程的webapp目錄下,如下圖:

技術分享

六、然後檢查一下如下圖的Deployment Assembly是不是正確

技術分享

七、然後把dto、dao、service、controller等加入項目就可以通過發布到tomcatv8.5進行頁面訪問了,但是需要註意由於沒有把tomcat的相關jar包包含進來,所以開發jsp和servlet的時候會報錯,所以需要把如下圖的tomcat相關lib加入build path

技術分享


遇到問題:

(1)搭建好maven的web工程後Publish到tomcat v8.5的時候eclipse彈框報錯: Publishing failed Could not publish to the server. java.lang.IndexOutOfBoundsException 或者報錯: Updating status for Apache Tomcat v8.5 at localhost...... java.lang.IndexOutOfBoundsException 解決辦法: 有可能是maven的repository裏有文件損壞導致的錯誤,我把C:\Users\Administrator\.m2\repository目錄下的所有文件目錄刪除了,然後重新通過maven下載一次就解決了 (2)在eclipse的problems中出現如下2個錯誤 Dynamic Web Module 3.1 requires Java 1.7 or newer One or more constraints have not been satisfied 解決方法:
概述:使用maven構建web項目時,經常會遇見這個問題,問題原因報錯講述的很清晰明了,就是web模塊需要使用java1.7及以後的版本,目前的版本不符合。因而只需要修改java版本到1.7及以上即可。 解決方法: 1. 在eclipse 構建 web中關於java版本有三處需要修改統一。 (1)在 Java Build Path的libraries中修改 (2)在Java Compiler 中修改 (3)在Project Facet中修改 2. 大部分按上上述修改就應該可以了,但總是有意外,還是報錯。因為使用了 maven構建項目,因而最好在pom.xml文件中的build標簽中加入以下代碼: <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source> //如果是1.7,修改為1.7
<target>1.8</target> //如果是1.7,修改為1.7
</configuration>
</plugin>
</plugins>
</build>
3.最後再右鍵使用maven的Update Project 即可。

eclipse搭建maven project的spring4 spring mvc mybatis