美文网首页
IDEA创建Maven项目(Spring MVC)

IDEA创建Maven项目(Spring MVC)

作者: 洁芬 | 来源:发表于2017-11-14 11:06 被阅读47次

    1.File-New-Project

    Paste_Image.png

    2.Maven-Next


    Paste_Image.png

    3.填写“GruopId”和“ArtifactId”,然后Next

    Paste_Image.png

    GruopId:一般为包名如com.leshang.项目
    ArtifactId:一般为模块名

    4.填写“Project name”和“Project location”,然后Finish


    Paste_Image.png

    5.修改pom.xml文件
    新的项目的“pom.xml”文件是这样的:

    <?xml version="1.0" encoding="UTF-8"?>
    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.leshang.test</groupId>
        <artifactId>test</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    </project>
    

    修改为

    <?xml version="1.0" encoding="UTF-8"?>
    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.leshang.test</groupId>
        <artifactId>test</artifactId>
        <version>1.0-SNAPSHOT</version>
        
        <packaging>war</packaging>
        <name>test</name>
        
        
         <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.8</java.version>
            <org.springframework.version>4.3.0.RELEASE</org.springframework.version>
            <org.springframework.security.version>4.0.3.RELEASE</org.springframework.security.version>
            <com.fasterxml.jackson.version>2.6.5</com.fasterxml.jackson.version>
            <org.apache.httpcomponents.version>4.5.2</org.apache.httpcomponents.version>
        </properties>
        
           <build>
            <finalName>test</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <uriEncoding>UTF-8</uriEncoding>
                        <path>/</path>
                        <port>80</port>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        
         <dependencies>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <type>jar</type>
                <scope>provided</scope>
            </dependency>
    
            <!--Logger-->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.5</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.16</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.16</version>
            </dependency>
              <!--AspectJ-->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.8.8</version>
            </dependency>
            <!--Springmvc-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
             <!--jstl-->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            
              <!--HttpClient-->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpmime</artifactId>
                <version>${org.apache.httpcomponents.version}</version>
            </dependency>
        </dependencies>
        
    

    packaging 标签要声明为war (web项目都为war)
    properties标签的内容相当于变量,想怎么写就怎么写
    build标签的finalName是最终打包后的war名

    依赖中的servlet必须要,而且scopeprovided
    Logger是日志包log4j 1.x版本用这几个就行
    AspectJ 是做切面的包
    jstl 就是可以在jsp中用<c:if>标签的包
    httpclient 要用来接接口,到时从其他项目复制WebUtils到工具包里面
    最后import Changes一下


    之后在main目录下新建一个文件夹,名叫webapp里面再新建一个WEB-INF的文件夹,里面新建一个web.xml的文件

    Paste_Image.png

    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"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             id="WebApp_ID" version="3.0">
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <filter>
            <filter-name>encodingFilter</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>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <servlet>
            <servlet-name>springServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>0</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    自定义的filterlistenerservlet都要在这里配置


    resources目录下新建spring.xmlspring-mvc.xmllog4j.properties

    Paste_Image.png

    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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--资源文件加载 有多个properties的话用,隔开如 classpath:common.properties,classpath:common2.properties-->
        <context:property-placeholder location="classpath:common.properties"/>
    
        <!--组件扫描 base-package要根据之际情况修改-->
        <context:component-scan base-package="com.leshang.erp.client.web">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
    </beans>
    

    spring-mvc.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:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 切面 -->
        <aop:aspectj-autoproxy proxy-target-class="true"/>
    
        <mvc:annotation-driven>
            <!-- 处理请求返回json字符串的中文乱码问题 -->
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
    
        <!-- 扫描注解 -->
        <context:component-scan base-package="com.leshang.erp.client.web">
            <!-- 排除Service注解 -->
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        </context:component-scan>
        
        <!-- 没有view是默认处理器,要有 -->
        <mvc:default-servlet-handler/>
    
         <!-- 前缀后缀配置 -->
        <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <!--静态资源-->
        <mvc:resources mapping="assets/**" location="/WEB-INF/assets/"/>
    </beans>
    

    log4j.properties

    log4j.rootLogger=Debug,stdout
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p [%F:%L] : %m%n
    

    直接复制进去就能用

    其他properties根据直接情况添加到自己写的properties中,命名要有前缀,如api.domain

    之后在java中建包和文件,写maven脚本运行

    Paste_Image.png Paste_Image.png

    相关文章

      网友评论

          本文标题:IDEA创建Maven项目(Spring MVC)

          本文链接:https://www.haomeiwen.com/subject/oiofzttx.html