使用Maven进行web项目创建

  1. eclipse中配置Maven基础环境

  2. 在pom.xml中引入相关依赖(SSM)

    	<properties>
    		<servlet-version>3.0.1</servlet-version>
    		<jsp-version>2.2</jsp-version>
    		<jstl-version>1.2</jstl-version>
    		<spring-version>4.1.6.RELEASE</spring-version>
    		<log4j-version>1.2.17</log4j-version>
    		<jackson-version>2.4.1</jackson-version>
    		<tomcat-version>2.2</tomcat-version>
    	</properties>
    	<dependencies>
    		<dependency>
    				<groupId>javax.servlet</groupId>
    				<artifactId>javax.servlet-api</artifactId>
    				<version>${servlet-version}</version>
    				<scope>provided</scope>
    			</dependency>
    			<dependency>
    				<groupId>javax.servlet.jsp</groupId>
    				<artifactId>jsp-api</artifactId>
    				<version>${jsp-version}</version>
    				<scope>provided</scope>
    			</dependency>
    			<dependency>
    				<groupId>jstl</groupId>
    				<artifactId>jstl</artifactId>
    				<version>${jstl-version}</version>
    			</dependency>
    			<!-- spring -->
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-webmvc</artifactId>
    				<version>${spring-version}</version>
    			</dependency>
    			<!-- log4j 日志 -->
    			<dependency>
    				<groupId>log4j</groupId>
    				<artifactId>log4j</artifactId>
    				<version>${log4j-version}</version>
    			</dependency>
    			<!-- jackson -->
    			<dependency>
    				<groupId>com.fasterxml.jackson.core</groupId>
    				<artifactId>jackson-databind</artifactId>
    				<version>${jackson-version}</version>
    			</dependency>
    	</dependencies>
    	<build>
    		<resources>
    			<resource>
    				<directory>src/main/java</directory>
    				<includes>
    					<include>**/*.xml</include>
    				</includes>
    			</resource>
    			<resource>
    				<directory>src/main/resources</directory>
    				<includes>
    					<include>**/*.xml</include>
    					<include>**/*.properties</include>
    				</includes>
    			</resource>
    		</resources>
    			<plugins>
    				<plugin>
    					<groupId>org.apache.tomcat.maven</groupId>
    					<artifactId>tomcat7-maven-plugin</artifactId>
    					<version>${tomcat-version}</version>
    					<configuration>
    						<port>80</port>
    						<path>/</path>
    					</configuration>
    				</plugin>
    			</plugins>
    	</build>

    注意:在servlet-api和jsp-api依赖时一定要添加<scope>provided</scope>,代表编译器生效,但是并不会打包发布到tomcat中。

  3. 在webapp目录下完善META-INF、WEB-INF、web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 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_3_0.xsd">
    	<!-- 上下文参数 -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath*:applicationContext-*.xml</param-value>
    	</context-param>
    	<!-- 监听器 -->
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<!-- SpringMVC前端控制器 -->
    	<servlet>
    		<servlet-name>springmvc</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>classpath:springmvc.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>springmvc</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    	<!-- 字符编码过滤器 -->
    	<filter>
    		<filter-name>encoding</filter-name>
    		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    		<init-param>
    			<param-name>encoding</param-name>
    			<param-value>utf-8</param-value>
    		</init-param>
    	</filter>
    	<filter-mapping>
    		<filter-name>encoding</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    	                 
    </web-app>

     

  4. 在resource目录下完善applicationContext-spring.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx" 
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName">
        <!-- 注解扫描 -->
        <context:component-scan base-package="com.wd.service.impl"></context:component-scan>
        <!-- 扫描属性文件 -->
        <context:property-placeholder location="classpath*:*.properties"/>
    </beans>

    springmvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xmlns:context="http://www.springframework.org/schema/context"
    	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
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 扫描注解 -->
        <context:component-scan base-package="com.wd.controller"></context:component-scan>
        <!-- 注解驱动 -->
        <mvc:annotation-driven></mvc:annotation-driven>
        <!-- 静态资源 -->
        <mvc:resources location="/WEB-INF/js/" mapping="/js/**"></mvc:resources>
        <mvc:resources location="/WEB-INF/images/" mapping="/images/**"></mvc:resources>
        <mvc:resources location="/WEB-INF/css/" mapping="/css/**"></mvc:resources>
        <!-- 视图解析器 -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        	<property name="prefix" value="/WEB-INF/jsp/"></property>
        	<property name="suffix" value=".jsp"></property>
        </bean>
    </beans>

    log4J.properties

    log4j.rootCategory=ERROR, CONSOLE 
    
    
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%C %d{YYYY-MM-dd hh:mm:ss}  %m %n
    
    log4j.appender.LOGFILE=org.apache.log4j.FileAppender
    log4j.appender.LOGFILE.File=E:/my.log
    log4j.appender.LOGFILE.Append=true
    log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.LOGFILE.layout.ConversionPattern=%m %n
    

     

  5. 在src目录下根据项目需求新建Controller、Service、ServiceImpl

posted @ 2019-10-23 17:48  Steven-Russell  阅读(5)  评论(0编辑  收藏  举报  来源