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中的應用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM