SpringBoot中的自定义路径怎么配置/根目录配置方法

359 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

创建一个MyMvcConfiguration区实现WebMvcConfigurer完成配置

下面代码是要在http://localhost:8080/http://localhost:8080/signuphttp://localhost:8080/signup.html的情况下页面直接显示signup.html的内容

package com.example.project.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyMvcConfiguration implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("signup");
        registry.addViewController("/signup.html").setViewName("signup");
        registry.addViewController("/signup").setViewName("signup");
    }
}

原始方法

原始方法就是直接用controller类进行配置

package com.example.project.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SignUpController {
    @RequestMapping({"/","/signup.html","signup"})
    public String signUp(){
        return "signup";

    }
}