Springboot訪問templates html頁面過程詳解
springboot項目默認是不允許直接訪問templates下的文件的,是受保護的。
如果要訪問templates下的文件,推薦使用thymeleaf。
注:使用thymeleaf這一點要牢牢記住!
如何使用:
1、pom依賴
<!--thymeleaf 模板依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、配置文件
#模板熱部署、禁用 thymeleaf 緩存spring.thymeleaf.cache=false
3、html文件位于resources的templates/目錄下,例如
注意html文件名,這里使用goodsShow,在不區分大小寫的情況下與后臺返回字符串匹配
4、后臺返回字符串形式訪問html(也可以使用ModelAndView,這里不做展示)
@Controller@RequestMapping('/goods')public class GoodsController { private static final Logger log = LoggerFactory.getLogger(GoodsController.class); @GetMapping public String goodsShow() { return 'goodsShow'; }}
5、瀏覽器訪問輸入:ip:端口號/上下文根/goods
本地訪問:localhost:端口號/上下文根/goods
例如:localhost:8080/goods
6、具體項目可以參考:https://github.com/guocanzhen/jQueryAjaxJavaWeb
里面還附有jQuery AJAX在springboot中的應用。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: