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

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项目了

发布了4 篇原创文章 · 获赞 1 · 访问量 156

猜你喜欢

转载自blog.csdn.net/weixin_46255209/article/details/104359015