IDEA使用maven新建一个SpringMVC项目 (学习SpringMVC 第一阶段创建项目)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39184388/article/details/83998290

     今天女朋友问我 如何建一个springmvc项目 写这篇博客给她看 

第一步 新建项目

    

    组名可根据写的项目要求修改  ArtifactId创建到后面默认是项目名

    

    

    

    

    

    新建完成后的目录结构

    

第二步  创建目录结构

    

第三步  添加springmvc的依赖 (使用maven添加依赖就是在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.example</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>test Maven Webapp</name>
    <url>http://www.example.com</url>

    <!--版本管理-->
    <properties>
        <spring.version>4.3.6.RELEASE</spring.version>
        <servlet-api.version>3.0.1</servlet-api.version>
        <jsp-api.version>2.0</jsp-api.version>
    </properties>

    <dependencies>
        <!-- SpringMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- JSP相关 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet-api.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp-api.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

    下载完 就说明添加依赖完成

    

第四步 配置tomcat

    

          我的idea还没有配置过本地tomcat  如果已经配置过可跳过

          

         选择本地tomcat点击ok

          

    已经设置过tomcat的 从这里开始把项目发布到tomcat

    

    

    然后点击ok 完成配置

    

    

第五步 修改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">
  <!--配置spring的配置文件位置   我配置文件的-*是一个通配符  -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext-*.xml</param-value>
  </context-param>
  <!--配置spring监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--设置字符集过滤器-->
  <filter>
    <filter-name>springUtf8Encoding</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>springUtf8Encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--设置视图层servlet-->
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置文件地存放位置 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/applicationContext-mvc.xml</param-value>
    </init-param>
    <!-- 优先执行加载操作:在服务器启动地同时加载对应文件  -->
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <!--
    url-pattern设置的值为浏览器地址栏中输入的值 可任意填 访问时需要加上你配置的后缀
    如: 我配置为 <url-pattern>*.test</url-pattern>
      浏览器url需要输入 http://localhost:8080/XXX.test 这里的后缀需要填写你配置的值
    -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

第六步  添加springmvc配置文件 放在resources/spring目录下  命名为applicationContext-mvc.xml

<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-4.0.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <context:component-scan base-package="com.test.*.controller"/>

    <!-- 拦截器 -->
    <!--
        <mvc:interceptors>
            &lt;!&ndash; 多个拦截器,顺序执行 &ndash;&gt;
            <mvc:interceptor>
                <mvc:mapping path="/**"/>
                <mvc:exclude-mapping path="/resources/**"/>
                <bean class="拦截器包.拦截器类名"/>
            </mvc:interceptor>
        </mvc:interceptors>
    -->

    <!--静态资源路径-->
    <mvc:resources location="resources/" mapping="/resources/**"/>

    <!-- 在实际开发中通常都需配置 mvc:annotation-driven标签,这个标签是开启注解 -->
    <!--
              SpringMVC开发需要注解
              url解析器
              处理器
              <mvc:annotation-driven/>
     -->
    <mvc:annotation-driven/>

    <!-- 视图解析器:  配置地时返回值的处理方式
              前缀:/WEB-INF/views/
              后缀:.jsp
              index         /   index . jsp
     -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/"/> <!--设置controller返回试图的前缀-->
        <property name="suffix" value=".jsp"/>  <!--设置controller返回试图的后缀-->
    </bean>

</beans>

可以运行一下 查看是否报错 这里已经配置好springmvc了

下篇我会写常用的springmvc知识 可作为学习的参考

猜你喜欢

转载自blog.csdn.net/qq_39184388/article/details/83998290