创建SpringMVC项目过程
2024-10-08 18:27:19
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....-->
最新文章
- SAP CRM 将组件整合至导航栏中
- SQL Server安全概念简析
- 品读吴军";之";系列
- composer环境配置
- bootstrap框架----像素
- 《深入浅出MFC:》
- 在VS2103环境中集成Doxygen工具
- motan源码分析四:客户端调用服务
- postfix防垃圾邮件
- HTML5 window/iframe跨域传递消息 API
- 实现promise
- PLSQL设置细节
- Hbase建表时遇到的问题This could be a sign that the server has too many connections
- 让eclipse调试和豌豆荚并存
- Android Studio中新建和引用assets文件
- iOS开发-消息通知机制(NSNotification和NSNotificationCenter)
- mysql日期问题
- Python初学者第十三天 三级菜单程序小作业
- bootstrap设计进度条和圆点
- jdk、jre、spring、java ee、java se