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:
- package com.cloume.agvs.configuration;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
- @Configuration
- publicclass MVCConfiguration extends WebMvcConfigurerAdapter {
- @Override
- publicvoid addViewControllers(ViewControllerRegistry registry){
- registry.addViewController("/login").setViewName("login");
- }
- }
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"!