SpringBoot设置默认主页的方法步骤
时间:2021-07-19 08:01:25|栏目:JAVA代码|点击: 次
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配置 不要在后台项目代码里添加前端的文件。
上一篇:解决java执行cmd命令调用ffmpeg报错Concat error - No such filter '[0,0]'问题
栏 目:JAVA代码
下一篇:java中将一个List等分成n个list的工具方法(推荐)
本文地址:http://www.codeinn.net/misctech/159255.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




