Usage of WebMvcConfigurerAdapter [transfer]

Reprinted from http://blog.csdn.net/cloume/article/details/48439429

 

When I used to write Spring MVC, to add a new page access, I always added a Controller or added a method to an existing Controller, and then jumped to the set page. Considering that there will be data interaction between the View and the background in most application scenarios, this kind of processing is understandable, but we must also just want to pass a URL Mapping and then jump directly to the page without controller processing. Today, when I was doing Spring Security related configuration, I stumbled across that Spring also provides us with a way! That is WebMvcConfigurerAdapter! Not much nonsense, just look at the code:

 

[java]  view plain copy  
 
  1. package com.cloume.agvs.configuration;  
  2.   
  3. import org.springframework.context.annotation.Configuration;  
  4. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;  
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;  
  6.   
  7. @Configuration  
  8. publicclass MVCConfiguration extends WebMvcConfigurerAdapter {   
  9.       
  10.     @Override  
  11.     publicvoid addViewControllers(ViewControllerRegistry registry){   
  12.         registry.addViewController("/login").setViewName("login");  
  13.     }  
  14. }  

Then through the above configuration, you can directly access the login.html page through "http://localhost:8080/projectContext/login" without adding LoginController or processing "login"!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326599431&siteId=291194637