Springboot Some of the basic core things, we need to practice a project to confirm that the things we learn can be applied, initially, we will choose to write a landing page, which is almost every site has a façade.
Before writing, there are some knowledge points that need to record--url orientation.
For example, when we visit the "/" and access to the "/index.html" such as the path, we hope they can point to the same page, but we do not write a bunch of controllers to achieve, so later maintenance is very cumbersome, so here introduced a adapter method.
How to do it, first look at the code, and then do the explanation:
/*** Due to SpringBoot2.0 before we used the webmvcconfigureradapter to do URL redirection, now it has expired, * and then we have two ways to achieve the above features: * 1. Inheritance Webmvcconfigurationsupport method (in two ways) * 2. Implement the Webmvcconfigurer interface (recommended here, relatively convenient) **///webmvcconfigurationsupport notation 1@Configuration Public classMymvcconfigextendsWebmvcconfigurationsupport {@Bean PublicWebmvcconfigurationsupport Webmvcconfigurationsupport () {Webmvcconfigurationsupport support=NewWebmvcconfigurationsupport () {@Overrideprotected voidaddviewcontrollers (Viewcontrollerregistry registry) {Registry.addviewcontroller ("/"). Setviewname ("index")); Registry.addviewcontroller ("/index.html"). Setviewname ("index"); } @Override Public voidaddresourcehandlers (Resourcehandlerregistry registry) {Registry.addresourcehandler ("/static/**"). Addresourcelocations ("classpath:/resources/static/")); Super. Addresourcehandlers (registry); } }; returnSupport ; }
--
// webmvcconfigurationsupport notation 2 @Configuration Public class extends webmvcconfigurationsupport { @Override protectedvoid Addviewcontrollers (Viewcontrollerregistry registry) { Registry.addviewcontroller ("/"). Setviewname (" Index "); Registry.addviewcontroller ("/index.html"). Setviewname ("index");} }
To see the recommended Method 2, is to implement the interface directly, it seems to know a lot simpler:
@Configuration Public class Implements webmvcconfigurer { @Override publicvoid addviewcontrollers ( Viewcontrollerregistry registry) { Registry.addviewcontroller ("/"). Setviewname ("index"); Registry.addviewcontroller ("/index.html"). Setviewname ("index"); }
The Addviewcontroller can be accessed by setting the path specified to the Setviewname page, which is typically configured under Templates's template package.
You can practice it for a look.
Springboot Diary--Actual combat article--url orientation