SpringMVC entry procedures
1. Create a WEB project, introduced the development of the jar package
<!-- 版本锁定 --> <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> </dependencies>
Configuring the core controller (configuration the DispatcherServlet)
DispatcherServlet core controller configuration in the web.xml configuration file
<-! SPRINGMVC core Controller -> < the servlet > < the servlet-name > DispatcherServlet </ the servlet-name > < the servlet-class > org.springframework.web.servlet.DispatcherServlet </ the servlet-class > <! - - initialization parameters Servlet reads springmvc configuration file, creating spring containers -> < the init-param > < param-name > the contextConfigLocation </ param-name > < param-value > CLASSPATH: springmvc.xml </ param -value > </param-the init > <-! loaded at startup objects configuration servlet, servlet initiated requests when creating a default here is to load the configuration file, you need to load at startup -> < the Load-ON-the Startup > 1 </ the Load-ON -startup > </ the servlet > < the servlet-Mapping > < the servlet-name > DispatcherServlet </ the servlet-name > < URL-pattern > / </ URL-pattern > </ the servlet-Mapping >
3. Write springmvc.xml profile
<?xml version="1.0" encoding="UTF-8"?> <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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd " <-! configured to scan the container when creating a spring package -> < context: Scan-Component Base-Package =" com.fgy " > </ context: Component-Scan > ! <- configuration view resolver -> < the bean ID =" the viewResolver " class =" org.springframework.web.servlet.view.InternalResourceViewResolver " > < Property name =" prefix " value =" / the WEB-INF / Pages / " > </ Property > <property name="suffix" value=".jsp"> > </ Property > </ bean> <! - configured spring mvc open support annotation of <mvc: Annotation-Driven> </ mvc: Annotation-Driven> -> </ Beans >
4. Preparation index.jsp and controller classes HelloController
1. index.jsp
<body> <a href="hello">入门程序</a> </body>
2. HelloController
/** * 控制器 */ @Controller public class HelloController { /** * 接收请求 * @return */ @RequestMapping(path = "/hello") public String sayHello() { System.out.println("hello springmvc"); return "success"; } }
5. Create a directory under WEB-INF folder pages, written success.jsp success page
< Body > jump success! </ Body >
6. Start Tomcat server for testing
Package
1.DispatcherServlet: Front Controller
用户请求到达前端控制器,它就相当于 mvc 模式中的 c,dispatcherServlet 是整个流程控制的中心,
由它调用其它组件处理用户的请求,dispatcherServlet 的存在降低了组件之间的耦合性。
2.HandlerMapping:处理器映射器
HandlerMapping 负责根据用户请求找到 Handler 即处理器,SpringMVC 提供了不同的映射器实现不同的映射方式,
例如:配置文件方式,实现接口方式,注解方式等。
3.Handlerr:处理器
它就是我们开发中要编写的具体业务控制器。由 DispatcherServlet 把用户请求转发到 Handler。
由Handler 对具体的用户请求进行处理。
4.HandlAdapter:处理器适配器
通过 HandlerAdapter 对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行。
5.View Resolver:视图解析器
View Resolver 负责将处理结果生成 View 视图,View Resolver 首先根据逻辑视图名解析成物理视图名,
即具体的页面地址,再生成 View 视图对象,最后对 View 进行渲染将处理结果通过页面展示给用户。
6.View:视图
SpringMVC 框架提供了很多的 View 视图类型的支持,包括:jstlView、freemarkerView、pdfView等。最常用的视图就是 jsp。
一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面。
<mvc:annotation-driven> 说明:
在 SpringMVC 的各个组件中,处理器映射器、处理器适配器、视图解析器称为 SpringMVC 的三大组件。
使用 <mvc:annotation-driven> 自动加载 RequestMappingHandlerMapping(处理映射器)和 RequestMappingHandlerAdapter ( 处 理 适 配 器 ),
在 SpringMVC.xml 配置文件中配置<mvc:annotation-driven>
它就相当于在 xml 中配置了:
<!-- HandlerMapping --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerM apping"></bean> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerA dapter"></bean> <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- HandlerExceptionResolvers --> <bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExcept ionResolver"></bean> <bean class="org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolv er"></bean> <bean class="org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver" ></bean>
注意:
在开发中,都需要写上此标签(虽然从入门案例中看,不写也行,但该标签有具体的使用场景)。
明确:
我们只需要编写处理具体业务的控制器以及视图。
RequestMapping 注解:
1. RequestMapping注解的作用是建立请求URL和处理方法之间的对应关系
2. RequestMapping注解可以作用在方法和类上
作用在类上:第一级的访问目录
作用在方法上:第二级的访问目录
3. RequestMapping的属性
1. path 指定请求路径的url
2. value value属性和path属性是一样的
3. mthod 指定该方法的请求方式 method = RequestMethod.GET
4. params 指定限制请求参数的条件
它支持简单的表达式。要求请求参数的 key 和 value 必须和配置的一模一样。params = {"username=hehe"}
params = {"accountName"},表示请求参数必须有 accountName
params = {"moeny!100"},表示请求参数中 money 不能是 100。
5. headers 发送的请求中必须包含的请求头 headers = {"Accept"}