一、Spring MVC介绍
1.Spring MVC是Spring体系的轻量级Web MVC框架
2.Spring MVC的核心Controller控制器,用于处理请求,产生响应
3.Spring MVC基于Spring IOC容器运行,所有对象被IOC管理
二、Spring MVC搭建套路
- Maven依赖spring-webmvc
- web.xml配置DispatcherServlet
- 配置applicationContext的MVC标记
- 开发测试用Controller
三、项目结构
项目结构
四、pom文件代码
<?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>org.caiweiwei</groupId> <artifactId>Jetty</artifactId> <version>1.0-SNAPSHOT</version> <!--必须改成war--> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.6.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.14.v20181114</version> <configuration> <webApp> <!--找到修改后的xml文件--> <defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor> </webApp> <!--启动后去加载webapp网页与资源--> <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory> <httpConnector> <port>80</port> </httpConnector> </configuration> </plugin> </plugins> </build> </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"> <servlet> <servlet-name>springmvc</servlet-name> <!-- 用于拦截请求,创建对应的Controller进行处理 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--加载配置文件--> <param-value>classpath:applicationContext.xml</param-value> </init-param> <!-- 在启动时对Servlet进行加载,0代表被先被加载 --> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
六、applicationContext.xml配置文件代码
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="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"> <!-- 启用Spring注解形式扫描对象 --> <context:component-scan base-package="com.caiweiwei"></context:component-scan> <!-- 启用Spring MVC的注解模式 --> <mvc:annotation-driven></mvc:annotation-driven> <!--将静态资源排除在外--> <mvc:default-servlet-handler></mvc:default-servlet-handler> </beans>