01-springmvc quick start, component parses

1. Spring Integration with Web Environment

1.1 ApplicationContext application context acquisition mode

Application context object is to get through the new ClasspathXmlApplicationContext (spring configuration file) mode, but must write new ClasspathXmlApplicationContext (spring configuration file) every time you get Bean from the container, such malpractice is a configuration file loading times, application context object is created repeatedly.

In the Web project, you can start using ServletContextListener monitor Web applications, Web application when we can start to load the Spring configuration file, create the application context object ApplicationContext, in its stores to the largest domain servletContext domain, so that you can obtained from the ApplicationContext application context field at an arbitrary position.

1.2 Spring provides a tool to get the application context

The above analysis not done manually, Spring provides a listener is ContextLoaderListener package of the above functions, the inside of the listener Spring configuration file is loaded, the application context object created and stored in the ServletContext domain, provides a client tool for WebApplicationContextUtils users get the application context object.

So we need to do two things:

① configuration ContextLoaderListener listener in web.xml (spring-web coordinates introduced)

② use WebApplicationContextUtils get the application context object ApplicationContext

1.3 introduction of the integrated web coordinate Spring

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.0.5.RELEASE</version>
</dependency>

Configuring listener ContextLoaderListener

<!--全局参数-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器-->
<listener>
	<listener-class>
       org.springframework.web.context.ContextLoaderListener
   </listener-class>
 </listener>

1.5 context object obtained by the application tool

ApplicationContext applicationContext =    
    WebApplicationContextUtils.getWebApplicationContext(servletContext);
    Object obj = applicationContext.getBean("id");

Knowledge Points

Spring integrated web environment steps

① listener configuration ContextLoaderListener

② obtain application context using WebApplicationContextUtils

2. SpringMVC Introduction

2.1 SpringMVC Overview

SpringMVC is based on the type of request-driven lightweight Java Web framework for the realization of MVC design model, which belongs to SpringFrameWork successor, has been integrated in the Spring Web Flow.

SpringMVC has become one of the most mainstream of the current MVC framework, and as Spring3.0 release, beyond Struts2, to become the best MVC framework. It is through a set of annotations to make a simple Java classes to become controller processes the request, without having to implement any interface. It also supports RESTful programming style requests.

2.3 SpringMVC Quick Start

Demand: client sends a request, the server receives the request, and performing a logical view of the jump.

Development steps

① import SpringMVC associated coordinates

② SpringMVC core configuration controller DispathcerServlet

③ create Controller classes and view pages

④ The annotation configuration mapping address service method Controller class

⑤ SpringMVC core configuration file spring-mvc.xml

⑥ client sends a request to test

Code

Spring and SpringMVC ① introduction of coordinates, the coordinates of introducing the Servlet and Jsp

 <!--Spring坐标-->
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>5.0.5.RELEASE</version>
 </dependency>
 <!--SpringMVC坐标-->
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>5.0.5.RELEASE</version>
 </dependency>
<!--Servlet坐标-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>
<!--Jsp坐标-->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
</dependency>

② configuration SpringMVC core controller in web.xml

<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.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>

③ Controller and create business methods

public class QuickController {
	public String quickMethod(){
		System.out.println("quickMethod running.....");
		return "index";
	}
}

③ create a view page index.jsp

<html>
<body>
    <h2>Hello SpringMVC!</h2>
</body>
</html>

④ Configuration Notes

@Controller
public class QuickController {
	@RequestMapping("/quick")
	public String quickMethod(){
		System.out.println("quickMethod running.....");
			return "index";
	}
}

⑤ create a spring-mvc.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"
    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">
    <!--配置注解扫描-->
    <context:component-scan base-package="com.itheima"/>
</beans>

⑥ access test address

http://localhost:8080/itheima_springmvc1/quick 

Console Print
Here Insert Picture Description

Page display

Here Insert Picture Description

2.3 SpringMVC flow chart

Here Insert Picture Description

2.4 Knowledge Points

SpringMVC development steps

① import SpringMVC associated coordinates

② SpringMVC core configuration controller DispathcerServlet

③ create Controller classes and view pages

④ The annotation configuration mapping address service method Controller class

⑤ SpringMVC core configuration file spring-mvc.xml

⑥ client sends a request to test

3. SpringMVC components resolved

3.1 SpringMVC implementation process

Here Insert Picture Description

① a user sends a request to the front-end controller DispatcherServlet.

②DispatcherServlet HandlerMapping processor receives a request to call mapper.

③ The processor finds particular processor mapping (according to the xml configuration, lookup annotations), generates the object and a processor blocker (if any is generated) together back to DispatcherServlet.

④DispatcherServlet call HandlerAdapter processor adapter.

⑤HandlerAdapter after adapting to call a specific processor (Controller, also known as back-end).

⑥Controller perform complete return ModelAndView.

⑦HandlerAdapter the controller ModelAndView the results back to the DispatcherServlet.

⑧DispatcherServlet ModelAndView will pass ViewReslover view resolver.

⑨ViewReslover return specific View parsed.

⑩DispatcherServlet render view (model data coming filled view) The View. DispatcherServlet response to the user.

3.2 SpringMVC component parses

  1. Front Controller: DispatcherServlet

A user request arrives at the front end of the controller, it is equivalent in MVC C, DispatcherServlet center of the entire process is controlled by the

It calls the other components of processing the user's request, the presence DispatcherServlet reduces coupling between components.

  1. Processor mapper: HandlerMapping

I.e. the processor responsible for finding Handler HandlerMapping user request, SpringMVC provide different mapper achieve different

Mapping, for example: profiles, to achieve interface mode, annotation methods.

  1. Processor Adapter: HandlerAdapter

HandlerAdapter execution by a processor, this is the mode of application adapters, the adapter can be extended by more types of treatment

Device for execution.

  1. Processor: Handler

It is what we want to write in the development of specific business controller. DispatcherServlet by the user forwards the request to the Handler. by

Handler for specific user requests are processed.

  1. View resolvers: View Resolver

View is responsible for processing the Resolver View to generate the results, the Resolver View The first resolves the name into a physical view of the logical view name, i.e., the specific page address, then generates a view object View, View finally render the processing result to the user through display pages.

  1. View: View

SpringMVC View framework provides a lot of view types of support, including: jstlView, freemarkerView, pdfView and so on. The most common view is jsp. You need to model data show generally by page or tag page by page template technology to users, programmers need to develop a specific page based on business needs

3.3 SpringMVC parsing comment

@RequestMapping

Action: means for establishing a correspondence between the request and a processing request URL method

position:

The class, request access to the first level directory of the URL. Do not write here, then, is equivalent to the root directory of the application

The method, the second stage of the request URL access to the directory, and a directory based on the use of labeled @ReqquestMapping access the virtual path together with a composition

Attributes:

value: Specifies the URL for the request. And its effect is the same path attributes

method: mode request specifies

params: for the request parameters specified condition. It supports simple expression. Required request parameters and key value must be exactly the same configuration and

E.g:

params = { "accountName"}, the parameter indicates that the request must be accountName

params = { "moeny! 100"}, indicates the request parameter is not money 100

1.mvc namespace introduced

命名空间:xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
约束地址: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

Component scans

Based SpringMVC Spring container, so SpringMVC during operation, needs to be stored in the Controller Spring container, if @Controller annotation label, then you need to use <context: component-scan base-package = "com.itheima.controller" / > assembly for scanning.

3.4 SpringMVC XML configuration parsing

SpringMVC components has a default configuration, the default configuration file DispatcherServlet.properties components are configured, the configuration file address org / springframework / web / servlet / DispatcherServlet.properties, the default configuration file parser view, as follows:

org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver

Look at the parser source code, you can see the default settings of the parser, as follows:

REDIRECT_URL_PREFIX = "redirect:"  --重定向前缀
FORWARD_URL_PREFIX = "forward:"    --转发前缀(默认值)
prefix = "";     --视图名称前缀
suffix = "";     --视图名称后缀
  1. View resolvers

We can modify the view of the property by way of injection of prefixes and suffixes

<!--配置内部资源视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/views/"></property>
  <property name="suffix" value=".jsp"></property>
</bean>

3.5 Knowledge Points

SpringMVC related components

Front Controller: DispatcherServlet

Processor mapper: HandlerMapping

Processor Adapter: HandlerAdapter

Processor: Handler

View resolvers: View Resolver

View: View

SpringMVC annotations and configuration

Request Mapping Notes: @RequestMapping

View resolver configuration:

REDIRECT_URL_PREFIX = “redirect:”

FORWARD_URL_PREFIX = “forward:”

prefix = “”;

suffix = “”;

ue=".jsp">


### 3.5 知识要点

**SpringMVC的相关组件** 

前端控制器:DispatcherServlet

处理器映射器:HandlerMapping

处理器适配器:HandlerAdapter

处理器:Handler

视图解析器:View Resolver

视图:View

**SpringMVC的注解和配置** 

请求映射注解:@RequestMapping

视图解析器配置:

REDIRECT_URL_PREFIX = "redirect:"  

FORWARD_URL_PREFIX = "forward:"    

prefix = "";     

suffix = "";     

Published 50 original articles · won praise 21 · views 3709

Guess you like

Origin blog.csdn.net/wmlwml0000/article/details/103743696
Recommended