Maven 构建springMVC 应用

作者: 天涯笑笑生 | 来源:发表于2017-10-17 10:38 被阅读3次
    • 新建项目
      Create New Project --> Maven -->maven-archetype-webapp
    图片.png

    -->Next


    图片.png

    -->Next
    可以修改maven版本等信息
    -->Next
    可以修改项目位置等信息
    -->Finish

    • pom 中导入springMVC的相关库
        <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.lin</groupId>  
            <artifactId>ssm_project</artifactId>  
            <version>0.0.1-SNAPSHOT</version>  
            <packaging>war</packaging>  
            <properties>  
                <!-- spring版本号 -->  
                <spring.version>3.2.8.RELEASE</spring.version>  
                <!-- log4j日志文件管理包版本 -->  
                <slf4j.version>1.6.6</slf4j.version>  
                <log4j.version>1.2.12</log4j.version>  
                <!-- junit版本号 -->  
                <junit.version>4.10</junit.version>  
                <!-- mybatis版本号 -->  
                <mybatis.version>3.2.1</mybatis.version>  
            </properties>  
          
            <dependencies>  
                <!-- 添加Spring依赖 -->  
                <dependency>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-core</artifactId>  
                    <version>${spring.version}</version>  
                </dependency>  
                <dependency>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-webmvc</artifactId>  
                    <version>${spring.version}</version>  
                </dependency>  
                <dependency>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-context</artifactId>  
                    <version>${spring.version}</version>  
                </dependency>  
                <dependency>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-context-support</artifactId>  
                    <version>${spring.version}</version>  
                </dependency>  
                <dependency>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-aop</artifactId>  
                    <version>${spring.version}</version>  
                </dependency>  
                <dependency>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-aspects</artifactId>  
                    <version>${spring.version}</version>  
                </dependency>  
                <dependency>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-tx</artifactId>  
                    <version>${spring.version}</version>  
                </dependency>  
                <dependency>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-jdbc</artifactId>  
                    <version>${spring.version}</version>  
                </dependency>  
                <dependency>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-web</artifactId>  
                    <version>${spring.version}</version>  
                </dependency>  
          
                <!--单元测试依赖 -->  
                <dependency>  
                    <groupId>junit</groupId>  
                    <artifactId>junit</artifactId>  
                    <version>${junit.version}</version>  
                    <scope>test</scope>  
                </dependency>  
          
                <!-- 日志文件管理包 -->  
                <!-- log start -->  
                <dependency>  
                    <groupId>log4j</groupId>  
                    <artifactId>log4j</artifactId>  
                    <version>${log4j.version}</version>  
                </dependency>  
                <dependency>  
                    <groupId>org.slf4j</groupId>  
                    <artifactId>slf4j-api</artifactId>  
                    <version>${slf4j.version}</version>  
                </dependency>  
                <dependency>  
                    <groupId>org.slf4j</groupId>  
                    <artifactId>slf4j-log4j12</artifactId>  
                    <version>${slf4j.version}</version>  
                </dependency>  
                <!-- log end -->  
          
                <!--spring单元测试依赖 -->  
                <dependency>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring-test</artifactId>  
                    <version>${spring.version}</version>  
                    <scope>test</scope>  
                </dependency>  
          
                <!--mybatis依赖 -->  
                <dependency>  
                    <groupId>org.mybatis</groupId>  
                    <artifactId>mybatis</artifactId>  
                    <version>${mybatis.version}</version>  
                </dependency>  
          
                <!-- mybatis/spring包 -->  
                <dependency>  
                    <groupId>org.mybatis</groupId>  
                    <artifactId>mybatis-spring</artifactId>  
                    <version>1.2.0</version>  
                </dependency>  
          
                <!-- mysql驱动包 -->  
                <dependency>  
                    <groupId>mysql</groupId>  
                    <artifactId>mysql-connector-java</artifactId>  
                    <version>5.1.29</version>  
                </dependency>  
            </dependencies>  
          
        </project>  
    

    根据需求添加相应的包

    • 配置web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
    
      <display-name>SpringMVCDemo Web Application</display-name>
    
      <!--配置拦截所有的请求-->
      <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
      <!--避免post中文乱码-->
      <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>
    
    </web-app>
    
    
    • 配置mvc-dispatcher-servlet.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"
           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">
    
        <!--指明 controller 所在包,并扫描其中的注解-->
        <context:component-scan base-package="com.gaussic.controller"/>
    
        <!-- 静态资源(js、image等)的访问 -->
        <mvc:default-servlet-handler/>
    
        <!-- 开启注解 -->
        <mvc:annotation-driven/>
    
        <!--ViewResolver 视图解析器-->
        <!--用于支持Servlet、JSP视图解析-->
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>
    

    注:servlet-name 和mvc-dispatcher-servlet.xml的名字要对应

    • 创建 controller
    @Controller
    @RequestMapping("/mvc")
    public class mvcController {
    
        @RequestMapping("/hello")
        public String hello(){        
            return "hello";
        }
    }
    

    最后配置服务器启动,测试

    注:此代码仅仅是示例,具体根据自己项目需求编写

    相关文章

      网友评论

        本文标题:Maven 构建springMVC 应用

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