springboot搭建web项目完整步骤

您所在的位置:网站首页 web项目模板 springboot搭建web项目完整步骤

springboot搭建web项目完整步骤

2023-07-17 06:52| 来源: 网络整理| 查看: 265

1、使用idea工具创建springboot项目 第一步:

在这里插入图片描述 后续直接点next,下一步 直到进入依赖选择页面,Web选择Spring Web,Template Engines选择Thymeleaf(模板引擎若在项目建立之后需要改动,可在application.yml进行配置即可),SQL选择JDBC API,Mybatis,MySQL Driver 在这里插入图片描述 2、建好项目后会自动生成pom.xml并自动添加了相应的maven依赖

4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.4.RELEASE com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.1 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin

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.Driver

4、环境搭建好了,可以正式开始写代码。

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); } }

在这里插入图片描述 5、浏览器输入http://localhost:8080/myIndex,成功访问!localhost:8080/

遇到的问题

浏览器报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