maven 创建web项目

       接触现在做的项目有一段时间了,发现现在这个项目每个文件包有很清析,和直接用eclipse直接建立的web项目不一样,后来在网上找了一些资料,才发现原来我们这个项目是采用了maven这个插件来建立web项目的。下面就介绍一种采用maven插件建立web项目的方法。

  一、web项目需要的运行环境:

  

    1、java version “1.6.0_18″

    2、Maven v3.0.4(直接在网上下载,地址:http://maven .apache.org/download.html

    3、Eclipse 3.7

    4、Struts2.3.4.1

    5、tomcat 6.0

    二、安装maven插件

    1、直接将下截获下来的maven插件解压到你想要的安装目录,这里我选择的是:F:\apache-maven-2.2.1

    2、配置环境变量:在系统环境变量中加入M2_HOME 变量,变量值为:F:\apache-maven-2.2.1,注意用分号分隔。

    3、打开cmd命令框,输入 : mvn -v 确定是否安装成功。

    

     三、maven构建web工程

       1打开cmd 命令框,进入工程目录,我的目录为:G:\MywebProject。

       2.输入命令行:mvn archetype:create -DgroupId=com.micmiu.struts2.demo -DartifactId=Hao 

              -DarchetypeArtifactId=maven-archetype-webapp - DinteractiveMode=false                                       

               “Hao”为项目名称。

            

        运行后的效果如上图如示,出现‘BUILD SUCCESSFUL’就表示工程建立成功。这时你就会发现你的工程目录下多一个文件,这个文件夹就是所建立的maven工程。

    四、将maven 导入 eclipse中。

      1、打开cmd命令框,进入工程文件夹中,输入如下命令:

          mvn eclipse:eclipse -Dwtpversion=1.0

        

        此时,你进入Hao这个工程文件夹中就会发现,出现了.project 为后缀的文件,这个文件就是eclipse所能识别的工程文件名。

      2、打开ecliplse ,选择:File---Import---Exciting Project into Workspace.将工程导入eclipse。

          此时,web项目已建立完毕。接下来,就是web.xml的配置。整个web项目目录结构如下图所示:

     五、struts2配置

        1.在WEB-INF/lib下导入struct 2相关 jar包。commons-fileupload-1.2.2.jar,commons-io-2.0.1.jar,commons-lang-2.5.jar,commons-logging-1.1.1.jar

         freemarker-2.3.18.jar,javassist-3.11.0.GA.jar(可选),ognl-3.0.3.jar,struts2-core-2.3.1.jar,xwork-core-2.3.1.jar。

        

        2、web.xml配置

          

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
               xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
                <display-name>Struct2</display-name>
              <filter>
                <filter-name>struts2</filter-name>
                <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
              </filter>
              <filter-mapping>
                  <filter-name>struts2</filter-name>
                  <url-pattern>/*</url-pattern>
              </filter-mapping>

              <welcome-file-list>
                <welcome-file>index.jsp</welcome-file>
                    </welcome-file-list>

          </web-app>

      3.struts.xml配置

          在src/main/resource 下新建一个struts.xml文件,配置如下:

      

          <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd">
          <struts>
              <constant name="struts.devMode" value="true" />
              <package name="basicstruts2" extends="struts-default">
                <action name="index">
                <result>/index.jsp</result>
                </action>

          </struts>

        至此,struts2已经配置完成。

     六、部署项目

        1、将web项目发布到tomcat中去,具体过程略。

        2、起动tomcat ,在浏览器中输入:http://localhost/Hao/,若能出现index.jsp页面,则配置成功。

        

 

        

   

      

        

 

posted @ 2012-10-20 15:24  Phoenix Nirvana  阅读(4519)  评论(0编辑  收藏  举报