Springboot中靜態(tài)文件的兩種引入方式總結
thymeleaf 模式
依賴中引入
<!-- 渲染靜態(tài)頁面 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
可選配置
如果你有
WebMvcConfigurationSupport 的一些類引用. 你需要放行他們
如果你引用了 springSecurity
你也需要放行他們
thymeleaf 需要通過controller層轉向view 層
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; /** * @ClassName: * @Descripton: * @Author: sansy * @Date: 2019/5/16 10:12 * @Version: 2.0 */ @RestController public class IndexController { @RequestMapping(value = "/index", method = RequestMethod.GET) public ModelAndView index() { System.out.println("/index進入controller控制器"); ModelAndView mav = new ModelAndView(); mav.setViewName("index"); return mav; } @RequestMapping(value = "/home", method = RequestMethod.GET) public ModelAndView home() { System.out.println("/home進入controller控制器"); ModelAndView mav = new ModelAndView(); mav.setViewName("index"); return mav; } @RequestMapping(value = "/error", method = RequestMethod.GET) public ModelAndView error() { System.out.println("/error進入controller控制器"); ModelAndView mav = new ModelAndView(); mav.setViewName("index"); return mav; } @RequestMapping(value = "/login", method = RequestMethod.GET) public ModelAndView login() { System.out.println("/login進入controller控制器"); ModelAndView mav = new ModelAndView(); mav.setViewName("index"); return mav; } @RequestMapping(value = "/", method = RequestMethod.GET) public ModelAndView indexs() { System.out.println("/ 進入controller控制器"); ModelAndView mav = new ModelAndView(); mav.setViewName("index"); return mav; } @RequestMapping(value = "/404", method = RequestMethod.GET) public ModelAndView error404() { System.out.println("/404 進入controller控制器"); ModelAndView mav = new ModelAndView(); mav.setViewName("index"); return mav; } }
yml 做如下配置
構架這樣構架
非thymeleaf 模式
首先去掉依賴
刪除controller的指向view層
如果你想帶控制器也是可以的 (帶的話 指向index. 不帶的話 默認指向index .可以理解成一個絕對路徑,一個相對路徑)
yml文件中這樣配置
是為了能夠直接訪問 根目錄下的text文件
構架如下
完成.
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java中JDBC實現(xiàn)往MySQL插入百萬級數(shù)據(jù)的實例代碼
這篇文章主要介紹了java中JDBC實現(xiàn)往MySQL插入百萬級數(shù)據(jù)的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01聊聊BeanUtils.copyProperties和clone()方法的區(qū)別
這篇文章主要介紹了聊聊BeanUtils.copyProperties和clone()方法的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Java整合mybatis實現(xiàn)過濾數(shù)據(jù)
這篇文章主要介紹了Java整合mybatis實現(xiàn)過濾數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-01-01使用springboot logback動態(tài)獲取application的配置項
這篇文章主要介紹了使用springboot logback動態(tài)獲取application的配置項,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08