Springboot:员工管理之首页(十(2))
访问首页可以通过两种方式:
1:编写controller
2:自定义扩展视图解析器(推荐使用)
1:编写Controller
com\springboot\controller\IndexController.java #首页跳转controller
package com.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
/*跳转首页*/
@RequestMapping({"/","/index.html"})
public String toIndex(){
return "index";
}
}
2:添加扩展视图解析器
com\springboot\config\MyConfig.java
package com.springboot.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 MyConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
/*访问首页视图解析器*/
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
}