Basic use of Spring MVC

springMVC is an MVC framework. It controls the entire process of the request. From the moment the request enters the application server to the corresponding departure, the MVC framework is inseparable.

 

request process

  • DisptacherServlet receives the request and reads the data in the request
  • According to the information (url) in the request, DisptacherServlet goes to Handlermapping to find the resource corresponding to the url (if not, it will report 404)
  • If the resource points to a Controller, send the request and request data to the Controller
  • The Controller processes the request (generally, the Controller hands the processing action to the business layer for processing), and returns the processed data and view name to the DisptacherServlet
  • DisptacherServlet finds the ViewResolver view resolver according to the view name, passes the data to the corresponding view processing
    view (jsp, html, etc.), and renders the data after getting the data.

There are several steps to configure using an mvc framework

  • Initialize DisptacherServlet (where Handlermapping and Controller are configured)
  • 配置 Handler Mapping (ServletMapping)
  • Identifies the Controler, configures the url to be processed by the Controler and the returned view name
  • Configure view resolvers

The second step and the third step are together, because the Controler is the Servlet. The configuration of Handlermapping and Controller is equivalent to <servlet-mapping> and <servlet> in web.xml . DisptacherServlet needs to get the springmvc configuration (servlel, servletmapping) from web.xml in order to schedule the whole process.

 

DisptacherServlet

Start SpringMVC by registering the DispatcherServlet. DisptacherServlet is the dispatch center of SpringMVC, where the request will enter SpringMVC for the first time.
DisptacherServlet is configured with SpringMVC operation information.

Before Servlet3.0, SpringMVC was configured in web.xml, and then SpringMVC was initialized according to the configuration in web.xml.

 

DisptacherServlet

After servlet 3.0, ServletContainerInitializer appeared. At the beginning of the project, it will automatically scan for classes that implement the ServletContainerInitializer interface, and dynamically register Servlet, Listener, and Filter. Using this technology, DisptacherServlet can be configured by implementing ServletContainerInitializer (registration). controller, ViewResolve) to achieve the effect of SpringMVC zero configuration.

 

ServletContainerInitializer

The role of ServletContainerInitializer is similar to that of web.xml, all of which are deployed servlet, listener, filter, but one is configured with java code, and the other is configured with xml.

The class that implements the ServletContainerInitializer interface should be annotated with @HandlesTypes (//The class to be dynamically registered must be Servlet or Listener or Filter)
There is only one method onStart(Set<Class>,ServletContext) in the ServletContainerInitializer interface.
When the application starts, the onStart method will be called, and the parameter Set<Class> is the class in @HandlesTypes, which means that these classes can be dynamically registered

to achieve the core method of dynamic registration:
addServlet(String name,Servlet servlet)
addFilter(String name ,Filter filter)
addListener(Listener listener)
These are the methods for registering Servlet, listener, filter

 

Now the easiest is to inherit AbstractAnnotationConfigDispatcherServletInitializer, rewrite getServletConfigClasses, getServletMappings, getRootConfigClasses to configure DisptacherServlet

 

Configure DisptacherServlet

This step, which is similar to configuring web.xml, is to deploy and register the servlet.
Here we use the method of inheriting and rewriting AbstractAnnotationConfigDispatcherServletInitializer to configure DisptacherServlet

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

 // 得到中间层(service、dao、aop、po等)的配置
 // Spring配置,得到bean
 @Override
 protected Class<?>[] getRootConfigClasses() {
   return new Class[]{DAOConf.class,ServiceConf.class,AOPConf.class};
 }

 // 得到controler和ViewResolver的配置
 @Override
 protected Class<?>[] getServletConfigClasses() {
   return new Class[]{SpringMvcConf.class}; 
 }

 // 标识哪些url要经过这个DisptacherServlet处理
 @Override
 protected String[] getServletMappings() {
   return new String[]{"/"};//所有url都被DisptacherServlet处理
 }

}

 

SpringMvc配置

这一步是完成SpringMvcConf.class,这个类配置了一些controller和ViewResolver,相当于对DispatcherServlet进行配置。

@Configuration
@EnableWebMvc
// 扫描控制器
@ComponentScan([email protected](type=FilterType.ANNOTATION,
    value=Controller.class))
public class SpringMvcConf extends WebMvcConfigurerAdapter {
 
 // 配置视图解析器
 // html解析
 @Bean
 public ViewResolver htmlResolver(){
   InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
   viewResolver.setPrefix("/WEB-INF/view/");
   viewResolver.setSuffix(".html");
   return viewResolver;
 }
 // 静态资源处理
 // 当DisptacherServlet接收到了他匹配的请求,但是找不到相应的Controller,
 // 就会把这个请求返回给默认的处理(比如交给tomcat处理)
 @Override
 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
   configurer.enable();
 }
}

 

这里关键有几步:
@EnableWebMvc,开启springmvc
@ComponentScan开启自动扫包,扫描所有带有@Controller的Bean
添加ViewResolver,这里的配置是使,视图路径为”/WEB-INF/view/”+Controller返回的视图名+”.html”
因为之前配置了所有路径都被DisptacherServlet接收,这会导致一些静态资源找不到controller去处理,结果出现404,configurer.enable()开启了之后,当springmvc不能处理的时候会交回给默认的处理

 

http://blog.zswlib.com/2016/07/15/springmvc%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326333747&siteId=291194637