首页 > 编程语言 > 详细

Springboot:员工管理之首页(十(2))

时间:2020-04-19 01:07:55      阅读:167      评论:0      收藏:0      [点我收藏+]
访问首页可以通过两种方式:
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");
    }
}

3:首页展示:

技术分享图片

Springboot:员工管理之首页(十(2))

原文:https://www.cnblogs.com/applesnt/p/12684598.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!