创建SpringMVC项目过程
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
在以前使用Servlet时候:写好对应的请求处理类,需要在web.xml中配置映射,才能将请求对应到处理类。
因此要使用Spring MVC,需要配置web.xml将所有请求交给Spring MVC来处理,因此将所有请求映射到Spring MVC的org.springframework.web.servlet.DispatcherServlet类。
在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配置文件
在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>
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>说明
在SpringMVC的各个组件中,处理器映射器(HandleMapping)、处理器适配器(HandlerAdapteMr)、视图解析器称为SpringMVC的三大组件。
使用<mvc:annotation-driven>自动加载RequestMappingHandleMapping(处理映射器)和RequestMappingHandlerAdapteMr(处理适配器),可用在SpringMVC.xml配置文件中使用<mvc:annotation-driven>替代注解处理器和适配器的配置。
它就相当于在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项目过程的更多相关文章
- 【SpringMVC】使用Myeclipse创建SpringMVC项目【超详细教程】
之前一直是使用Eclipse创建Web项目,用IDEA和MyEclipse的创建SpringMVC项目的时候时不时会遇到一些问题,这里把这个过程记录一下,希望能帮助到那些有需要的朋友.我是用的是MyE ...
- springmvc学习笔记---idea创建springmvc项目
前言: 真的是很久没搞java的web服务开发了, 最近一次搞还是读研的时候, 想来感慨万千. 英雄没落, Eclipse的盟主地位隐隐然有被IntelliJ IDEA超越的趋势. Spring从2. ...
- 工具idea 基于maven 创建springMVC项目
SpringMVC Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单.这些控制器一般不 ...
- 在eclipse中使用maven创建springMVC项目
一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...
- eclipse创建springmvc项目
一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...
- vs在微软官方tfs创建私有项目过程
谁也不是成天创建新项目,每次一创建就跟没干过这活似的,这次把它记下,再用的时候来翻,也希望能给别人点帮助. 上https://dev.azure.com/,tfs原来的网址会往这里跳,现在都在往dev ...
- SpringMVC教程--eclipse中使用maven创建springMVC项目
一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...
- SpringMVC教程--Idea中使用Maven创建SpringMVC项目
1.新建项目 参照idea教程中的创建maven项目https://www.cnblogs.com/daxiang2008/p/9061653.html 2.POM中加入依赖包 (1)指定版本 (2) ...
- Maven3.2创建webapp项目过程中问题以及解决方案
用maven组件来创建web项目,maven的好处一大堆,但是在创建项目的时候问题也很多,诸多不顺,网上找了很多资料,貌似都没能解决问题. 环境:jdk1.7.0_80,eclipse4.4,mave ...
随机推荐
- Django路由层与视图层
表与表之间建关系 图书管理系统为例 书籍表 出版社表 作者表 三个表之间的关系: 考虑表之间的关系:换位思考 1.书籍和出版社是一对多,外键字段建立在书籍表中 2.书籍和作者是多对多, 需要建立第三方 ...
- 26)PHP,数据库表格中项的数据类型
类型展示: tinyint-----1个字节 smallint----2个字节 mediumint--3个字节 int------4个字节 bigint---8个字节 字符串类型 最基本最重要的2个: ...
- 吴裕雄--天生自然python Google深度学习框架:深度学习与深层神经网络
- J - Association of Cats and Magical Lights Kattis - magicallights (树状数组+dfs序)
Rar the Cat((™)) is the founder of ACM (Association of Cats and Magical Lights). This is the story o ...
- python实现使用代码进行代理配置
#!/usr/local/bin/python3.7 import urllib.request import urllib.parse # 创建handler handler = urllib.re ...
- 5.redis主从配置
Redis的主从复制 1.什么是主从复制 持久化保证了即使redis服务重启也不会丢失数据,因为redis服务重启后会将硬盘上持久化的数据恢复到内存中,但是当redis服务器的硬盘损坏了可能会导致数据 ...
- java170道面试题汇总+详细解析
2013年年底的时候,我看到了网上流传的一个叫做<Java面试题大全>的东西,认真的阅读了以后发现里面的很多题目是重复且没有价值的题目,还有不少的参考答案也是错误的,于是我花了半个月时间对 ...
- ./config\make\make install命令详解
这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤 一.基本信息 1../configure 是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不 ...
- 吴裕雄--天生自然 R语言开发学习:回归(续四)
#------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...
- Myeclipse 错误An internal error has occurred 解决办法
1. 给MyEclipse的快捷方式加个参数再重新启动一次. 步骤如下:右键选中快捷方式属性选项,在快捷方式页,目标一项最后加上-clean选项,如"C:\MyEclipse6\e ...