目录
1.controller类如何接收dispatcher转发的请求
一.使用SpringMVC框架的开发步骤
1.环境搭建
Step1:创建maven工程
Step2:创建web项目
Step3:集成tomcat服务器
Step4:修改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>org.example</groupId> <artifactId>Pro1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>Pro1 Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- springMVC依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.19</version> </dependency> <!-- servlet依赖--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> </build> </project>
Step5:创建SpringMVC核心配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" 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-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 添加包扫描--> <context:component-scan base-package="controller"/> <!-- 添加视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置前缀--> <property name="prefix" value="/admin/"></property> <!-- 配置后缀--> <property name="suffix" value=".jsp"></property> </bean> <!-- 将访问的请求去掉后缀--> <mvc:annotation-driven> <mvc:path-matching suffix-pattern="true" /> </mvc:annotation-driven> </beans>
Step6:修改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_4_0.xsd" version="4.0"> <!-- 注册SpringMVC的框架,就是配置DispatcherServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 读取SpringMVC核心配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- DispatcherServlet拦截的请求,只接收*.action的请求--> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
2.请求流程
1.页面请求先经过web.xml文件查看是否有SpringMVC框架,所以要在web.xml中配置dispatcherServlet的信息,就相当于声明了这个servlet。指明这个servlet接收的请求的类型。
2.dispatcherServlet这个类也有很多初始化信息需要配置,所以SpringMVC核心配置文件中配置dispatcherServlet后,要在web.xml中加载信息。
3.初始化的信息包括要告诉dispatcher转发请求的目标对象,也就是具体转发给哪个controller类,所以要先用包扫描去创建这些类。
4.当controller类处理完请求后,还要经过dispatcher处理,则需要指明需要转发的资源,所以要标明前后缀。