关于SpringBoot使用过程中的一些问题1.0
问题
问题1:乱码问题,在编写配置内容的时候出现中文乱码情况
分析:IDEA基本设置里propeties Files(*.properties)语言为GBK,应该改为UTF-8。
解决方案:在File-Settings-Editor-File Encodings-修改Defalut encoding for properties files 为UTF-8,且勾上Transparent native-to-ascii conversion
问题2:报错,出现警告:Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
分析:中文翻译为配置数据源失败:未指定“url”属性,并且无法配置嵌入的数据源。其本质原因就是没有导入相关数据库,导致报错。
解决方案:
1.添加如下代码,解决冲突
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
2.添加数据库相关参量
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_itcast?serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=123456
问题3:导入jquery报错,静态路径404
分析:这里就是SpringBoot最大的坑点,用相对路径时候会发生这样的报错,因为它太贴心了,已经帮我们默认了在static的静态路径。
解决方案:
1.删除前面/static再加上自己的项目path,即可,如下
<script src="/Test/js/jquery-3.3.1.min.js"></script>
备注本人的项目path设置如下(application.properties):
server.servlet.context-path=/Test
2.推荐使用
在配置中添加静态访问路径
spring.mvc.static-path-pattern=/static/**
问题4:使用jquery没有找到mapping
相关代码:
index.ftl里的核心代码
<button onclick="Test()">按钮</button>
<script type="text/javascript">
function Test() {
$.post("hello",{},function (result) {
alert(result);
});
}
</script>
相关hello
@Controller
@RequestMapping("/ajax")
public class AjaxController {
@RequestMapping(value = "/hello",method = RequestMethod.POST)
public String hello(){
return "hello world";
}
}
分析:这里犯了个低级错误,就是@Controller标签,@RestContoller标签区别。
简单来说就是@RestContoller=@Controller+@ResponseBody
解决方案:
1.把@Controller改为@RestController
2.添加@ResponseBody标签
@Controller
@RequestMapping("/ajax")
public class AjaxController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "hello world";
}
}
总结
本次内容为SpringBoot初次学习过程中遇到的一些问题,仅仅是错题集,供后来学习者参考。
本文记录了SpringBoot初次学习时遇到的问题及解决办法。包括编写配置出现中文乱码,需改IDEA编码;数据源配置失败,要导入数据库;导入jquery静态路径404,调整路径或添加静态访问路径;使用jquery没找到mapping,修改标签。供后来学习者参考。


被折叠的 条评论
为什么被折叠?



