eclipse的springboot项目访问静态资源(html、css) 、 ajax的url无需加localhost前缀

Honoria ·
更新时间:2024-09-20
· 932 次阅读

1.将视图文件放在resource的templates目录下

2. 第二步在application.properties配置文件中添加

#访问静态视图的路径 spring.thymeleaf.prefix=classpath:/templates/ spring.resources.static-locations=classpath:/templates/

注意:首页的index.html文件必须放在templates/根下

3.运行项目,打开浏览器输入localhost:8080。
在这里插入图片描述
如果光输入localhost:8080打不开首页,就换成localhost:8080/index.html试试

4.要达开html文件夹下的文件,可以这样输入
localhost:8080/html/xxx.html

5.在这个环境下,ajax的url请求就可以省略掉前缀了

$.ajax({ type : 'post', url : 'http/addRace', contentType : 'application/json;charset=utf-8', data:formdata, success : function(status){ if(status==1){ alert("添加成功!"); //initStudentTable(); }else{ alert("添加失败!"); } } });

6.然后就可以快乐的开发你的web项目了


作者:a-zack



springboot HTML Eclipse 静态 localhost AJAX url CSS

需要 登录 后方可回复, 如果你还没有账号请 注册新账号