新建一个spring boot项目

1.注明出处 http://tengj.top/2017/02/26/springboot1/
2.自动生成项目,推荐Spring Initializr,它从本质上来说就是一个Web应用程序,它能为你生成Spring Boot项目结构。访问地址 http://start.spring.io/
3.Spring Initializr有几种用法:
3.1 通过Web界面使用


3.1.1选择构建工具Maven Project、Spring Boot版本1.5.9以及一些工程基本信息,可参考下图所示

这里写图片描述

3.1.2点击Generate Project下载项目压缩包
3.1.3导入到你的工程,如果是IDEA,则需要:
    a.菜单中选择File–>New–>Project from Existing Sources...
    b.选择解压后的项目文件夹,点击OK
    c.点击Import project from external model并选择Maven,点击Next到底为止。
    d.若你的环境有多个版本的JDK,注意到选择Java SDK的时候请选择Java 7以上的版本

3.2通过IntelliJ IDEA使用(idea内部集成自动生成)
    3.2.1.在File菜单里面选择 New > Project,然后选择Spring Initializr,接着如下图一步步操作即可。

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

4.启动项目,编写程序入口类

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class Chapter1Application {

    @RequestMapping("/")
    public String index(){
        return "Hello Spring Boot";
    }
    public static void main(String[] args) {
        SpringApplication.run(Chapter1Application.class, args);
    }
}

运行该类,打开浏览器访问http://localhost:8080

5.使用springboot自带tomcat运行,运行jsp页面

5.1新建webapp文件夹及WEB-INF文件夹jsp文件夹,webapp文件夹名固定

这里写图片描述

5.2pom.xml导入包
<!--WEB支持-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--jsp页面使用jstl标签-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

<!--用于编译jsp-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
5.3 application.properties配置
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
5.4 编写一个访问类
@Controller
@RequestMapping("/learn")
public class LearnController {

    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}
5.6 启动程序入口类,访问http://localhost:8080/learn/index

6.在外部tomcat上运行

6.1修改pom.xml

    a. 打包修改为war包
<packaging>war</packaging>
    b.移除内部tomcat的依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
c.修改scope必须是provided
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
7.修改程序入口类
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }


    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

8.idea Edit Configuration配置,启动项目
9.另一种方法

9.1修改打包方式为war
9.2直接添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
9.3修改程序入口类

10. 启动项目

当前课程中博客项目的实战源码是我在 GitHub上开源项目 My-Blog,目前已有 3000 多个 star:本课程是一个 Spring Boot 技术栈的实战类课程,课程共分为 3 大部分,前面两个部分为基础环境准备和相关概念介绍,第三个部分是 Spring Boot 个人博客项目功能的讲解,通过本课程的学习,不仅仅让你掌握基本的 Spring Boot 开发能力以及 Spring Boot 项目的大部分开发使用场景,同时帮你提前甄别和处理掉将要遇到的技术难点,认真学完这个课程后,你将会对 Spring Boot 有更加深入而全面的了解,同时你也会得到一个大家都在使用的博客系统源码,你可以根据自己的需求和想法进行改造,也可以直接使用它来作为自己的个人网站,这个课程一定会给你带来巨大的收获。作者寄语本课程录制于 2020 年,代码基于 Spring Boot 2.x 版本。到目前为止,Spring Boot 技术栈也有一些版本升级,比如 Spring Boot 2.7 发版、Spring Boot 3.x 版本发布正式版本。对于这些情况,笔者会在本课程实战项目的开源仓库中创建不同的代码分支,保持实战项目的源码更新,保证读者朋友们不会学习过气的知识点。课程特色 课程内容紧贴 Spring Boot 技术栈,涵盖大部分 Spring Boot 使用场景。开发教程详细完整、文档资源齐全、实验过程循序渐进简单明了。实践项目页面美观且实用,交互效果完美。包含从零搭建项目、以及完整的后台管理系统和博客展示系统两个系统的功能开发流程。技术栈新颖且知识点丰富,学习后可以提升大家对于知识的理解和掌握,对于提升你的市场竞争力有一定的帮助。实战项目预览    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值