码上敲享录 > Spring框架常见问题详解分享 > springmvc访问根路径时默认跳转到指定页

springmvc访问根路径时默认跳转到指定页

上一章章节目录下一章 2021-07-20已有1091人阅读 评论(0)

springmvc访问根路径时默认跳转到指定页


解决方法:

以下代码就是配置springmvc访问根路径时默认跳转到首页,例如访问localhost:8080时跳转到首页。

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration

class DefaultPageForword extends WebMvcConfigurerAdapter {


   public void addViewControllers(ViewControllerRegistry registry) {

       String defaultPage = "/index";//这里可以读取配置文件的值

       registry.addViewController("/").setViewName("forward:" + defaultPage);

       registry.setOrder(-2147483648);

       super.addViewControllers(registry);

   }

}


向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交