SpringBoot sets the default homepage

1. If the rendering engine, JSP and other VIEW rendering technologies are used, it can be solved by the method of addViewController.

which is:

@Configuration
public class DefaultView extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/Blog").setViewName("forward:index.jsp");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }
}

or

@Controller
@RequestMapping("/")
public class IndexController {
    @RequestMapping("/Blog")
    public String index()  {
        return "forward:index.html";
    }
}


2. If the front-end and back-end separation modes are completely adopted, that is, all resources at the front end are placed in the path configured by addresourceHandler

which is

 @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/temples/**")
                .addResourceLocations("classpath:/temples/");
        super.addResourceHandlers(registry);
    }

At this time, it cannot be solved by configuring addViewController, an exception will be thrown

which is

javax.servlet.ServletException: Could not resolve view with name 'forward:/temples/index.html' in servlet with name 'dispatcherServlet'

You can only redirect to the default homepage through response.redirect ("temples / index.html"),

Note: I did not find related methods in the WebMvcConfigurationSupport class. There is no other solution.

which is

@Controller
@RequestMapping("/")
public class IndexController {

    @RequestMapping("/")
    public void index(HttpServletResponse response) throws IOException {
        
        response.sendRedirect("/temples/index.html");

    }
}

3 Finally, it is best to configure nginx not to add front-end files in the background project code.

ojbk
Published 21 original articles · won 24 · views 20,000 +

Guess you like

Origin blog.csdn.net/qq_30332665/article/details/88575825