springboot搭建web项目完整步骤 |
您所在的位置:网站首页 › web项目模板 › springboot搭建web项目完整步骤 |
1、使用idea工具创建springboot项目 第一步:
3、配置mysql数据源,因为引入了mysql依赖,所以这一步不能省略,使用application.yml配置,需要将application.properties删除: spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: root driver-class-name: com.mysql.jdbc.Driver4、环境搭建好了,可以正式开始写代码。 HelloController.java @Controller //可以返回视图 //自动为springboot应用进行配置 //@EnableAutoConfiguration public class HelloController { @RequestMapping("/myIndex") public String index() { System.out.println("hello.springboot的第一个controller"); return "index1"; } }-在resources/template路径下创建 index1.html Title 你好,这是我的第一个springboot页面,thymeleaf 在所有java文件的父路径下创建springboot启动类,DemoApplication.java //标识为springboot启动类,必须是父路径,其他包路径必须是其子路径 //@ComponentScan(basePackages = "com") @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
浏览器报Whitelabel Error Page controller返回的字符串找不到对应的html 解决思路: 1、url访问出错,若是controller类使用了@RequestMapping(“xx”),方法中使用@RequestMapping(“index”),那么url应是localhost:8080/xx/index 若是controller类不使用@RequestMapping,则访问的url是localhost:8080/index 2、controller注解写成了RestController,,返回的是json格式数据 3、项目启动类创建的路径不对,必须是父路径,其他包路径必须是其子路径,我就是这里出错的,排查了几个小时才得以解决,以此为戒。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |