在本教程中,咱們將演示如何使用 Maven 建立一個 Java Web 項目(Spring MVC)。html
用到的技術/工具:java
您能夠經過使用Maven的maven-archetype-webapp模板來建立一個快速啓動Java Web應用程序的項目。在終端(* UNIX或Mac)或命令提示符(Windows)中,導航至您想要建立項目的文件夾。web
鍵入如下命令:spring
$ mvn archetype:generate -DgroupId=com.yiibai -DartifactId=CounterWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
具體示例 :apache
C:\worksp>mvn archetype:generate -DgroupId=com.tomato -DartifactId=CounterWebAp p -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom -- - [INFO] Generating project in Batch mode Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/ma ven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav en-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar (4 KB at 0.1 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/ma ven-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav en-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom (533 B at 0.1 KB/sec) [INFO] ------------------------------------------------------------------------- --- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0 [INFO] ------------------------------------------------------------------------- --- [INFO] Parameter: basedir, Value: C:\worksp [INFO] Parameter: package, Value: com.tomato [INFO] Parameter: groupId, Value: com.tomato [INFO] Parameter: artifactId, Value: CounterWebApp [INFO] Parameter: packageName, Value: com.tomato [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: C:\worksp\CounterWebApp [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10:30 min [INFO] Finished at: 2015-10-28T20:31:03+08:00 [INFO] Final Memory: 16M/174M [INFO] ------------------------------------------------------------------------
新的Web項目命名爲 「CounterWebApp」,以及一些標準的 web 目錄結構也會自動建立。瀏覽器
查看生成的項目結構佈局:tomcat
.|____CounterWebApp ||____pom.xml ||____src |||____main ||||____resources ||||____webapp |||||____index.jsp |||||____WEB-INF ||||||____web.xml
Maven 產生了一些文件夾,一個部署描述符 web.xml,pom.xml 和 index.jsp。服務器
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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tomato</groupId> <artifactId>CounterWebApp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>CounterWebApp Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>CounterWebApp</finalName> </build> </project>
web.xml – Servlet 2.3 已經比較舊, 建議升級到2.5mvc
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Archetype Created Web Application</display-name></web-app>
index.jsp – 一個簡單的 hello world html 頁面文件
<html> <body> <div><div class="ads-in-post hide_if_width_less_800"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- 728x90 - After2ndH4 --> <ins class="adsbygoogle hide_if_width_less_800" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-2836379775501347" data-ad-slot="3642936086" data-ad-region="mkyongregion"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div></div><h2>Hello World!</h2> </body> </html>
要導入這個項目到Eclipse中,須要生成一些 Eclipse 項目的配置文件:
3.一、在終端,進入到 「CounterWebApp」 文件夾中,鍵入如下命令:
C:\worksp>cd CounterWebApp C:\worksp\CounterWebApp>mvn eclipse:eclipse -Dwtpversion=2.0 [INFO] Scanning for projects... Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven -war-plugin/2.2/maven-war-plugin-2.2.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven- war-plugin/2.2/maven-war-plugin-2.2.pom (7 KB at 2.5 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven -war-plugin/2.2/maven-war-plugin-2.2.jar Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven- war-plugin/2.2/maven-war-plugin-2.2.jar (77 KB at 26.2 KB/sec) [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building CounterWebApp Maven Webapp 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-eclipse-plugin:2.10:eclipse (default-cli) > generate-resources @ CounterWebApp >>> [INFO] [INFO] <<< maven-eclipse-plugin:2.10:eclipse (default-cli) < generate-resources @ CounterWebApp <<< [INFO] [INFO] --- maven-eclipse-plugin:2.10:eclipse (default-cli) @ CounterWebApp --- [INFO] Adding support for WTP version 2.0. [INFO] Using Eclipse Workspace: null [INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER [INFO] Not writing settings - defaults suffice [INFO] Wrote Eclipse project for "CounterWebApp" to C:\worksp\CounterWebApp. [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.982 s [INFO] Finished at: 2015-10-28T20:24:57+08:00 [INFO] Final Memory: 15M/146M [INFO] ------------------------------------------------------------------------
3.2 導入到 Eclipse IDE – File -> Import… -> General -> Existing Projects into workspace.
圖像說明: 在 Eclipse 中,若是看到項目頂部有地球圖標,意味着這是一個 Web 項目。
在Maven中,Web項目的設置都經過這個單一的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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tomato</groupId> <artifactId>CounterWebApp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>CounterWebApp Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <jdk.version>1.7</jdk.version> <spring.version>4.1.1.RELEASE</spring.version> <jstl.version>1.2</jstl.version> <junit.version>4.11</junit.version> <logback.version>1.0.13</logback.version> <jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version> </properties> <dependencies> <!-- Unit Test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <!-- Spring Core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${jcl-over-slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> </dependencies> <build> <finalName>CounterWebApp</finalName> <plugins> <!-- Eclipse project --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <!-- Always download and attach dependencies source code --> <downloadSources>true</downloadSources> <downloadJavadocs>false</downloadJavadocs> <!-- Avoid type mvn eclipse:eclipse -Dwtpversion=2.0 --> <wtpversion>2.0</wtpversion> </configuration> </plugin> <!-- Set JDK Compiler Level --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> <!-- For Maven Tomcat Plugin --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/CounterWebApp</path> </configuration> </plugin> </plugins> </build> </project>
注意,爲方便起見,聲明 maven-eclipse-plugin,並配置wtpversion 來避免輸入參數 -Dwtpversion=2.0。如今,每次使用 mvn eclipse:eclipse,Maven將這個項目導入轉換爲 Eclipse Web 項目。
#以前 mvn eclipse:eclipse --> Eclipse Java project (JAR) mvn eclipse:eclipse -Dwtpversion=2.0 --> Eclipse Java web project (WAR) #以後 mvn eclipse:eclipse --> Eclipse Java web project (WAR)
在這一步中,在上一步配置完pom.xml後,從新執行 mvn eclipse:eclipse 這個命令,咱們將建立Spring MVC的一些文件和logback日誌框架的文件夾,最終的項目結構以下所示:
. |____pom.xml |____src | |____main | | |____java | | | |____com | | | | |____tomato | | | | | |____controller | | | | | | |____BaseController.java | | |____resources | | | |____logback.xml | | |____webapp | | | |____WEB-INF | | | | |____mvc-dispatcher-servlet.xml | | | | |____pages | | | | | |____index.jsp | | | | |____web.xml
5.1 建立 Spring MVC 的控制器類。
/src/main/java/com/tomato/controller/BaseController.java
package com.tomato.controller; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class BaseController { private static int counter = 0; private static final String VIEW_INDEX = "index"; private final static org.slf4j.Logger logger = LoggerFactory.getLogger(BaseController.class); @RequestMapping(value = "/", method = RequestMethod.GET) public String welcome(ModelMap model) { model.addAttribute("message", "Welcome"); model.addAttribute("counter", ++counter); logger.debug("[welcome] counter : {}", counter); // Spring uses InternalResourceViewResolver and return back index.jsp return VIEW_INDEX; } @RequestMapping(value = "/{name}", method = RequestMethod.GET) public String welcomeName(@PathVariable String name, ModelMap model) { model.addAttribute("message", "Welcome " + name); model.addAttribute("counter", ++counter); logger.debug("[welcomeName] counter : {}", counter); return VIEW_INDEX; } }
5.2 建立Spring配置文件。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.tomato.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
5.3 更新讓現有的 web.xml 支持 Servlet 2.5(默認的Servlet2.3 太舊了), 而且還經過 Spring 監聽器 ContextLoaderListener 集成了Spring框架。
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Counter Web Application</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
5.4 移動文件 index.jsp 到 WEB-INF/pages 目錄下, 爲了保護直接訪問。並更新內容:
/src/main/webapp/WEB-INF/pages/index.jsp
5.5 在資源文件夾(resources)中建立 logback.xml 文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration debug="false"> 3 <!--定義日誌文件的存儲地址 勿在 LogBack 的配置中使用相對路徑 --> 4 <property name="LOG_HOME" value="/home" /> 5 <!-- 控制檯輸出 --> 6 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 7 <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> 8 <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日誌消息,%n是換行符 --> 9 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - 10 %msg%n</pattern> 11 </encoder> 12 </appender> 13 <!-- 按照天天生成日誌文件 --> 14 <appender name="FILE" 15 class="ch.qos.logback.core.rolling.RollingFileAppender"> 16 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 17 <!--日誌文件輸出的文件名 --> 18 <FileNamePattern>${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.log</FileNamePattern> 19 <!--日誌文件保留天數 --> 20 <MaxHistory>30</MaxHistory> 21 </rollingPolicy> 22 <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> 23 <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日誌消息,%n是換行符 --> 24 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - 25 %msg%n</pattern> 26 </encoder> 27 <!--日誌文件最大的大小 --> 28 <triggeringPolicy 29 class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 30 <MaxFileSize>10MB</MaxFileSize> 31 </triggeringPolicy> 32 </appender> 33 34 <!-- 日誌輸出級別 --> 35 <root level="INFO"> 36 <appender-ref ref="STDOUT" /> 37 </root> 38 </configuration>
在第5步中建立全部文件之後,這裏有一些方法能夠用來部署和測試Web項目,咱們這裏推薦使用6.2中的方法。
6.1 要編譯,測試和項目打包成一個WAR文件,輸入:
mvn package
一個新的 WAR 文件將在 project/target/CounterWebApp.war產生,只需複製並部署到Tomcat 發佈的目錄。
6.2 若是想經過 Eclipse 服務器這個項目插件(Tomcat 或其它容器)調試,這裏再輸入:
mvn eclipse:eclipse
若是一切順利,該項目的依賴將被裝配附加到 Web部署項目。圖片: 右鍵點擊 project -> Properties -> Deployment Assembly
6.3 Maven 的 Tomcat 插件聲明(加入到 pom.xml):
<!-- For Maven Tomcat Plugin -->
鍵入如下命令(有時網絡不通暢須要執行2-3次):
mvn tomcat7:run tp://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details 20:37:32,089 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Se g level of logger [com.tomato.controller] to DEBUG 20:37:32,089 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Se g additivity of logger [com.tomato.controller] to false 20:37:32,090 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - ching appender named [STDOUT] to Logger[com.tomato.controller] 20:37:32,090 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction tting level of ROOT logger to ERROR 20:37:32,090 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - ching appender named [STDOUT] to Logger[ROOT] 20:37:32,090 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationActi End of configuration. 20:37:32,091 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@3bead5 Registering current configuration as safe fallback point 十月 28, 2015 20:37:32 下午 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring root WebApplicationContext 十月 28, 2015 20:37:33 下午 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring FrameworkServlet 'mvc-dispatcher' 十月 28, 2015 20:37:33 下午 org.apache.coyote.http11.Http11Protocol init 信息: Initializing Coyote HTTP/1.1 on http-8080 十月 28, 2015 20:37:33 下午 org.apache.coyote.http11.Http11Protocol start 信息: Starting Coyote HTTP/1.1 on http-8080
這將啓動Tomcat,部署項目默認在端口8080。
解決方案:
1.右鍵點擊項目--選擇Properties
選擇Deployment Assembly,在右邊點擊Add按鈕,在彈出的窗口中選擇Java Build Path Entries
2.點擊Next,選擇Maven Dependencies
3.點擊Finish,而後能夠看到已經把Maven Dependencies添加到Web應用結構中了
操做完後,從新部署工程,再也不報錯了。而後咱們再到.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\目錄下,發現工程WEB-INF目錄下自動生成了lib目錄,而且全部的依賴jar包也都已經部署進來。問題所以解決。
最後,在瀏覽器裏輸入http://localhost:8080/CounterWebApp/,回車就能看到結果了
代碼地址:http://download.csdn.net/download/caobingyi/10253692
原文地址:https://www.yiibai.com/maven/create-a-web-application-project-with-maven.html