Spring MVC workflow and module capabilities
The workflow for the request processing of Spring Web MVC Dispatcherservlet is as follows:
Work flow
(1) After receiving an HTTP request, Dispatcherservlet selects and invokes the appropriate controller according to the handlermapping.
(2) The controller accepts the request and invokes the appropriate service method based on the use of the GET or POST method. The service method sets the model data based on the defined business logic and returns the view name to Dispatcherservlet.
(3) Dispatcherservlet will get help from viewresolver to define the view for requesting a check.
(4) Once the view is determined, Dispatcherservlet will pass the model data to the view and finally render it in the browser.
module functions
- Disapatcherservlet: The central controller, as a unified access point, for global process control.
- Handlermapping: The mapping processor is responsible for telling the central controller to call which controller
- Controller: Handle the specific business and return the view name to Dispatcherservlet
- Viewresolver&view: Help Dispatcherservlet check the definition view
- Interceptor: Interceptors that intercept our defined requests and do the processing work ps:spring MVC struts2 and other MVC frameworks use the front-end Controller mode adapter mode, etc.
Initial learning: Using functions
- Core components: Dispatcherservlet Controller handlermapping (mapping processor, responsible for mapping the mapping policy when the central controller is forwarded to the controller) Modelandview Viewresolver Interceptors
- Process
2.1 Configuring the Web. xml file
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- struts习惯使用/*,在springmvc不管用 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2.2 Configuring the Spring.xml file
<!-- 一旦有扫描器的定义,mvc:annotation-driven不需要,扫描器中包含驱动-->
<context:component-scan base-package="cn.itcast.controller"/>
<!-- 前缀+ viewName +后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- webroot到某一指定的文件夹的路径 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 视图名称的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
2.3 Adding annotations
- Add note @controller to indicate that the controller class is adding methods
Add @requestmapping ("/xxx.do") to indicate the way to access the control layer @RequestMapping ("/xxxxx") can also be used to declare namespaces on a class
2.4 Model data Processing
The return value of the method can be Modelandview,newmodelandview ("XXX", map), which is equivalent to putting the result data in the request
- Define the map directly in the parameter list of the method, which is the map inside the Modelandview, which is handled by the view parser
(The above two methods are not recommended)
- Recommended: Define the Model object in the parameter list of the method, which is equivalent to putting the result data in the request
2.5 redirects
- Controller internal redirection, redirect: Plus the value of requestmapping in the same controller
return "redirect:homenews-list.do";
- Redirection between controllers: You must specify the namespace of the controller and specify the value of requestmapping, redirect: Must be added/, starting from the root directory, for example:
return "redirect:/homenews/homenews-list.do";
2.6 Use of interceptors
- Configure the Spring MVC configuration file First:
<!-- 拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**/*"/>
<bean class="com.hmaccelerate.interceptors.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
- Then implement the relative interface
From for notes (Wiz)
Spring MVC Notes