公园 - 云代码空间
——
在本教程中,我们将演示如何使用 Maven 创建一个 Java Web 项目(Spring MVC)。
用到的技术/工具:
您可以通过使用Maven的maven-archetype-webapp模板来创建一个快速启动Java Web应用程序的项目。在终端(* UNIX或Mac)或命令提示符(Windows)中,导航至您想要创建项目的文件夹。
键入以下命令:
$ mvn archetype:generate -DgroupId=com.yiibai -DartifactId=CounterWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
具体示例 :
C:\worksp>mvn archetype:generate -DgroupId=com.yiibai -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.yiibai [INFO] Parameter: groupId, Value: com.yiibai [INFO] Parameter: artifactId, Value: CounterWebApp [INFO] Parameter: packageName, Value: com.yiibai [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 目录结构也会自动创建。
查看生成的项目结构布局:
.|____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.yiibai</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.5
<!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 项目的配置文件
经典介绍:
源码结构
愿意了解框架技术或者源码的朋友直接求求交流分享技术:2042849237
分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨
更多详细源码参考来源:http://minglisoft.cn/technology