maven 构建项目

  1. 构建简单的web项目
    • mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
    • http://maven.apache.org/guides/mini/guide-ide-eclipse.html 把maven项目导入eclipse
    • mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo 这句话的作用应该是生成m2_pro环境变量,m2_pro就是使eclipse的项目找到maven的本地库
    • mvn eclipse:eclipse 就是让这个项目转为eclispses可以识别的项目(该命令是在只有一个Pom的情况下,如有多个pom请查看上面的链接)
    • 在maven的web项目中,要理解目录结构,首先所有文件是存放在src/main里,然后main文件夹可以分为java,resources,webapp目录,java是存放java文件,resources是存放各种配置文件,如log4j,webpp就是存放页面的地方
  • 构建简单的servlet项目
    • 首先要配置pom 
      <dependency>
      	<groupId>org.jboss.spec.javax.servlet</groupId>
      	<artifactId>jboss-servlet-api_3.0_spec</artifactId>
      	<version>1.0.2.Final</version>
      <dependency>
    • 调用mvn install之后,写一个简单的servlet
      import java.io.IOException;
      import java.io.PrintWriter;
      
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      public class SimpleServlet extends HttpServlet{
      
      	@Override
      	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      			throws ServletException, IOException {
      		// TODO Auto-generated method stub
      		PrintWriter out = resp.getWriter();
      		out.println("Servlet Executed");
      		out.flush();
      		out.close();
      	}
      
      }
      
    • 然后是配置web.xml文件
      	<servlet>
      		<servlet-name>swfupload</servlet-name>
      		<servlet-class>com.swf.SimpleServlet</servlet-class>
      	</servlet>
      	<servlet-mapping>
      		<servlet-name>swfupload</servlet-name>
      		<url-pattern>/swfupload</url-pattern>
      	</servlet-mapping>
    • 最后测试,在浏览器里输入localhost:8080/{project-name}/swfupload,会看到servlet excuted
  • maven spring mvc项目
    • 配置pom.xml
      <dependency>
      	<groupId>org.springframework</groupId>
      	<artifactId>spring-core</artifactId>
      	<version>3.2.3.RELEASE</version>
      </dependency>
      <dependency>
      	<groupId>org.springframework</groupId>
      	<artifactId>spring-web</artifactId>
      	<version>3.2.3.RELEASE</version>
      </dependency>
      <dependency>
      	<groupId>org.springframework</groupId>
      	<artifactId>spring-webmvc</artifactId>
      	<version>3.2.3.RELEASE</version>
      </dependency>
      <!-- include spring @Transactional -->
      <dependency>
      	<groupId>org.springframework</groupId>
      	<artifactId>spring-tx</artifactId>
      	<version>3.2.3.RELEASE</version>
      </dependency>
      
      <!-- standard.jar包 -->
      <dependency>
      	<groupId>taglibs</groupId>
      	<artifactId>standard</artifactId>
      	<version>1.1.2</version>
      </dependency>
      

      详细解释:

    • 配置applicationContext.xml
       1 <?xml version="1.0" encoding="UTF-8"?>  
       2 <beans  
       3     xmlns="http://www.springframework.org/schema/beans"  
       4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       5     xmlns:context="http://www.springframework.org/schema/context"  
       6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
       7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
       8     <import  
       9         resource="service.xml" />  
      10     <import  
      11         resource="dao.xml" />  
      12 </beans> 
    • 配置servlet.xml ,这个作用就是DispatchServlet的上下文,负责web mvc的controller
       1 <?xml version="1.0" encoding="UTF-8"?>
       2 <beans xmlns="http://www.springframework.org/schema/beans"
       3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
       5         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
       6     <context:component-scan base-package="com.controller" />
       7     <bean id="urlMapping"
       8         class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
       9     <bean
      10         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
      11     <!-- <bean
      12         class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> -->
      13 </beans> 
    • 配置service.xml
       1 <?xml version="1.0" encoding="UTF-8"?>  
       2 <beans  
       3     xmlns="http://www.springframework.org/schema/beans"  
       4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       5     xmlns:context="http://www.springframework.org/schema/context"  
       6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
       7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
       8     <context:component-scan  
       9         base-package="com.service" />  
      10 <!-- <bean
      11         class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> -->
      12 </beans>
    • 配置dao.xml
       1 <?xml version="1.0" encoding="UTF-8"?>  
       2 <beans  
       3     xmlns="http://www.springframework.org/schema/beans"  
       4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       5     xmlns:aop="http://www.springframework.org/schema/aop"  
       6     xmlns:context="http://www.springframework.org/schema/context"  
       7     xmlns:tx="http://www.springframework.org/schema/tx"  
       8     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
       9         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
      10         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
      11         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">  
      12     <context:component-scan  
      13         base-package="com.dao" />  
      14         <!-- <bean
      15         class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> -->
      16 </beans>
    • 配置web.xml
       1 <web-app>
       2     <display-name>Archetype Created Web Application</display-name>
       3     <servlet>
       4         <servlet-name>swfupload</servlet-name>
       5         <servlet-class>com.swf.SimpleServlet</servlet-class>
       6     </servlet>
       7     <servlet-mapping>
       8         <servlet-name>swfupload</servlet-name>
       9         <url-pattern>/swfupload</url-pattern>
      10     </servlet-mapping>
      11     <!-- 应用路径 -->
      12     <context-param>
      13         <param-name>webAppRootKey</param-name>
      14         <param-value>spring.webapp.root</param-value>
      15     </context-param>
      16     <!-- Log4J 配置 -->
      17     <!-- <context-param>
      18         <param-name>log4jConfigLocation</param-name>
      19         <param-value>classpath:log4j.xml</param-value>
      20     </context-param>
      21     <context-param>
      22         <param-name>log4jRefreshInterval</param-name>
      23         <param-value>60000</param-value>
      24     </context-param> -->
      25     <!--Spring上下文 配置  指出appliactionContext.xml的位置-->
      26     <context-param>
      27         <param-name>contextConfigLocation</param-name>
      28         <param-value>classpath:applicationContext.xml</param-value>
      29     </context-param>
      30     <!-- 字符集 过滤器 -->
      31     <filter>
      32         <filter-name>CharacterEncodingFilter</filter-name>
      33         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      34         <init-param>
      35             <param-name>encoding</param-name>
      36             <param-value>UTF-8</param-value>
      37         </init-param>
      38         <init-param>
      39             <param-name>forceEncoding</param-name>
      40             <param-value>true</param-value>
      41         </init-param>
      42     </filter>
      43     <filter-mapping>
      44         <filter-name>CharacterEncodingFilter</filter-name>
      45         <url-pattern>/*</url-pattern>
      46     </filter-mapping>
      47     <!-- Spring 监听器 -->
      48     <listener>
      49         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      50     </listener>
      51     <listener>
      52         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
      53     </listener>
      54     <!-- Spring 分发器 -->
      55     <servlet>
      56         <servlet-name>spring</servlet-name>
      57         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      58         <init-param>
      59             <param-name>contextConfigLocation</param-name>
      60             <param-value>classpath:servlet.xml</param-value>
      61         </init-param>
      62     </servlet>
      63     <servlet-mapping>
      64         <servlet-name>spring</servlet-name>
      65         <url-pattern>*.do</url-pattern>
      66     </servlet-mapping>
      67     <welcome-file-list>
      68         <welcome-file>index.html</welcome-file>
      69         <welcome-file>index.htm</welcome-file>
      70         <welcome-file>index.jsp</welcome-file>
      71         <welcome-file>default.html</welcome-file>
      72         <welcome-file>default.htm</welcome-file>
      73         <welcome-file>default.jsp</welcome-file>
      74     </welcome-file-list>
      75 </web-app>

       

    • 下面就是项目的目录结构
       

posted @ 2013-08-02 14:30  popping  阅读(432)  评论(0编辑  收藏  举报