SpringBoot设置默认主页的方法步骤

这篇文章主要介绍了SpringBoot设置认主页的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.若采用渲染引擎,JSP等VIEW渲染技术,可以通过addViewController的方式解决

即:@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); } }或者@Controller @RequestMapping("/") public class IndexController { @RequestMapping("/Blog") public String index() { return "forward:index.html"; } }

2.若完全采用前后端分离的模式,即前端所有资源都放在addresourceHandler配置的路径下

@Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/temples/**") .addResourceLocations("classpath:/temples/"); super.addResourceHandlers(registry); }

此时不能通过配置addViewController的方式解决,会抛出异常

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

只能通过response.redirect(“temples/index.html”)的方式重指向认主页,

注:我在WebMvcConfigurationSupport类中并未找到相关方法。也无其他解决方案。

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

3最后 最好通过Nginx配置 不要在后台项目代码添加前端的文件

到此这篇关于SpringBoot设置认主页的方法步骤的文章就介绍到这了,更多相关SpringBoot设置认主页内容搜索编程之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程之家!

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...