持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第22天,点击查看活动详情
springboot项目部署
设置欢迎页:
WebMvcAutoConfiguration类中的welcomePageHandlerMapping() -->getIndexHtml() --> location + "index.html" ,即 任意一个静态资源目录中的 Index.html就是欢迎页
网站中 网页标签的Logo是固定名字 : favicon.ico 自定义 favicon.ico :阅读 源码得知 :只需要将 favicon.ico文件 放入 任意静态资源目录中即可。
总结:
- 通过源码发现静态资源的目录
- 用静态资源:只需要将静态资源放入 以上目录即可
- 其他特定的文件(欢迎页、ico),只需要 根据约定(index.html favicon.ico) 放入该目录即可
如何自定义静态资源目录(Properties文件中的 prefix+属性) : spring.resources.static-locations=classpath:/res/, classpath:/img/
以上就将 静态资源目录设置为了classpath:/res/, classpath:/img/ ,注意 自定义静态资源目录后 以前默认的目录会失效
动态资源: JSP(spring boot默认不支持) 推荐:模板引擎 thymeleaf 网页= 模板+数据
引入thymeleaf:到官网查询 thymeleaf的依赖(Maven)
使用thymeleaf:代码在哪里写? ThymeleafAutoCongifutation 、 XxProperties 通过ThymeleafProperties源码得知: 使用thymeleaf只需要将 文件放入目录:"classpath:/templates/"; 文件的后缀: ".html";
注意:在以前传统的web项目中:静态资源修改后 是不需要重启的;但是在spring boot项目中,修改后 需要重启。
welcome to thymeleaf....
以上,先从${welcome}中取值,如果有 则直接显示;如果没有,则在显示welcome to thymeleaf....th就是替换原有html的值:th:html属性名=值 ;
welcome to thymeleaf....
th:xx (参见第10章 Attrubite Pre....) th:text 获取文本值 显示 将hello 渲染为h1后的效果 th:utext 获取文本值(不转义) 显示
hello
符号 th:text="以外 其他符号? 查看第四章 Standard Express....
Spring boot整合JSP开发 之前spring boot默认 自带一个内置的tomcat,不需要打war包,直接通过Jar即可运行。
但是,如果要整合jsp开发,就需要 单独配置一个 外置的tomcat ,需要打war包。 Spring boot整合JSP开发步骤:
新建boot项目, war
注意: org.springframework.boot spring-boot-starter-tomcat provided provided:意思是 将项目打包时,不需要将内置的tomcat一起打包。
2.建立基本的web项目所需要的目录结构
webapps/WEB-INF(需要) webapps/WEB-INF/web.xml (不需要) webapps/index.jsp 3.创建tomcat实例、部署项目 访问: 域名:端口/项目名/文件名 http://localhost:8080/SbJSP/index.jsp
分析:
如果是一个war包的spring boot项目,在启动服务器tomcat时, 会自动调用ServletInitializer类中 的configure方法,configure方法会调用spring boot的主配置类 从而启动spring boot; 即在启动tomcat服务器时 会1启动tomcat 2启动spring boot