The basic configuration and operation principle of Spring mvc

1.spring mvc framework to build

Requirements: In your browser and enter a request login.do, jump to the successful login interface.

The first step, create a web project, import the jar package
note:
The second step, the configuration of the spring core and the spring mvc listener front controller in web.xml
<? xml Version = "1.0" encoding = "UTF-8"?> 
<Web-App xmlns = "http://xmlns.jcp.org/xml/ns/javaee" 
xmlns: xsi = "HTTP: // the 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 "> 
<-! core listeners spring of 
action: when the web container (tomcat) started, create an object factory class of spring, the object is bound to the web container context (environment) 
in. 
Before we use spring framework, the first thing is to create a factory class object: 
ApplicationContext context = new new ClassPathXmlApplciationContext ( "applicationContext.xml" ); 
We are now using the web project, or need to create a factory class object? We need to create. 
Create a factory class object when. Factory just create a class object is enough to create more appropriate when the project started. 
-> 
<listener>
<listener- class > org.springframework.web.context.ContextLoaderListener </ listener- class > 
</ listener> 
<! --- 
specified spring main configuration file location and name manually, if the keyword is not specified, then the spring will be default from the WEB -INF / applicationContext.xml 
we are accustomed to spring before the main configuration file into the src below, it is generally specify this. 
-> 
<context-param> 
<param-name> the contextConfigLocation </ param-name> 
<param-value> CLASSPATH: the applicationContext.xml </ param-value> 
</ context-param> 
! <- configuration of the spring mvc front controller 
action: intercepting a request specified, spring mvc frame to be processed
 -> 
<the servlet> 
<the servlet-name> Spring-MVC </ the servlet-name> 
<servlet- class> org.springframework.web.servlet.DispatcherServlet </ servlet- class > 
<-! location and name of the main configuration file specifies the spring mvc 
If this parameter is not specified, spring from the WEB default loading [servlet -INF / in name] - servlet.xml 
for this example, the generated profile name to Spring -mvc- servlet.xml
 -> 
<the init-param> 
<param-name> the contextConfigLocation </ param-name> 
<param-value> CLASSPATH: mvc.xml-Spring </ param-value> 
</ the init-param> 
</ the servlet> 
<-Mapping the servlet> 
<the servlet-name> Spring-MVC </ the servlet-name> 
<-! intercept all the end .do request, to the spring mvc process, if you want to intercept all requests can write / * -> 
. <url-pattern> * do </ url-pattern> 
</servlet-mapping>
</web-app>
The third step is to create a spring and spring mvc master configuration file
spring main configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
</beans>

spring mvc master configuration file:

<? XML Version = "1.0" encoding = "UTF-. 8"?> 
<Beans xmlns = "http://www.springframework.org/schema/beans" 
xmlns: the 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 "> 
<-! - configured spring mvc processor mapper 
action: specify how to find spring mvc controller the controller 
the controller is spring mvc controller, quite before the servlet code. 
Before the servlet processing request is in, spring mvc processing request in the controller. Many projects have a controller, which in the end looking for a controller handling 
requests is 
RequestMappingHandlerMapping In this implementation of the set by the controller to find the specific classes or methods @RequestMapping above 
process controller. 
-> 
<the bean class= "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> 
</ the bean> 
<-! configured spring mvc processor adapter 
action: java class defines what characteristics is considered a qualified controller . There are a lot of underlying in spring mvc adapter is achieved. 
RequestMappingHandlerAdapter if a class defines a comment in a @Controller, and even if he is a qualified control
 -> 
<bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
</ bean> 
<! - configure spring mvc jsp default view resolver 
in the spring mvc can return multiple views (jsp interface srvlet before the equivalent of) 
like the default jsp, can also be json, may also be a number of template files . You can also return to excel, word, pdf and so on. 
-> 
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<!'/WEB-INF/jsp/index.jsp');
requestDispatch.forward('/WEB-INF/jsp/user.jsp');
...
requestDispatch.forward('/WEB-INF/jsp/xxx.jsp');
-->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!--配置返回视图的后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
The fourth step is to create a controller class in spring mvc
/ ** 
* Spring MVC controller class 
* / 
@Controller 
public  class the LoginController {
 / ** 
* access registration request to jump to the main menu 
* @return 
* / 
// @RequestMapping a path definition request 
@RequestMapping ( "/ login" )
 public String the Login () { 
System.out.println ( "Welcome" );
 // return the view name is a 
return "index" ; 
} 
}
A fifth step, the main spring mvc package configuration file located in the scanning controller
note:
It is noted here: all controllers must packets, the packet can not be scanned in the spring in the main configuration file spring mvc master configuration file. service and dao
The spring package must be scanned in a main profile, main spring mvc not scan profile. 
The sixth step, create jsp interface
Seventh step, test
Direct start will be reported the following error:
 
Because the spring jar package does not deploy to tomcat go, you need to open the "Project structers":
2.spring principle 
Running processes:
请求login.do--->web.xml中前端控制器--->spring mvc 主配置文件--》处理器映射器--》处理器适配器=》控制器
==》jsp的视图解析器=》视图的解析==》视图的渲染==》给用户返回结果。
DispatcherServlet 前端控制器 总调度
HandlerMapping 处理器映射器 接口 可以有很多实现 每种实现描述怎么找到控制器的方法
HandlerAdapter 处理器适配器 接口 可以有什么多实现,每种实现定义了什么样特征的java类才算是一个合格的控
制器
ViewResolver 视图解析 接口 可以有很多实现 每种实现定义了返回什么样的视图,今天返回的是jsp,以后的话还
会返回json,文件上传解析器,excle 解析,freeemarker,word 等等 
3.spring mvc 简化配置 

 

Guess you like

Origin www.cnblogs.com/duguangming/p/10932555.html