springboot学习记录:
1、Springboot 通过controller返回页面需要在pom中添加thymeleaf依赖,否则不能正确返回页面
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、页面要放在对应的文件下才能正确的访问到
3、解决跨域问题
static文件夹下的页面通过idea访问
页面代码
通过idea访问的页面地址为http://localhost:63342/cors/static/index.html?_ijt=r75octqt77unk2p2k3sgmnlbr4,可以观察到该地址端口63342与后台端口不一致,即进行了跨域请求。
当springboot不进行任何配置,直接进行跨域访问时,浏览器会报错
解决办法:
-
在controller方法上添加@CrossOrigin注解
-
在controller类上添加@CrossOrigin注解
-
添加全局配置,新建配置类CorsConfig继承WebMvcConfiguererAdapter
也可以在yml文件中书写配置,如下图:
下图跨域返回界面,也可得controller返回的页面是templates文件中的页面