SpringBoot设置默认首页的两种方式

首先要将Index.html放在scc/main/resource/static下面
然后
方式一:通过定义控制器的方式

package com.wjh.action;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

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

方式二:

package com.wjh.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@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);
    }
}

在浏览器中输入http://localhost:8080/Blog 就可以进入到首页了。

猜你喜欢

转载自blog.csdn.net/flower_CSDN/article/details/81200682