Springboot Diary--Actual combat article--url orientation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.