IDEA+Gradle+SpringBoot 创建spring web项目,SpringBoot集成html,JSP

 

前提:

  1. 本地安装了jdk,并配置系统变量JAVA_HOME,Path
  2. 本地下载了gradle-4.10u并配hu系统变量GRAhuLEchHOME,Path

1.打开IDEA,创建project项目

2.项目管理工具选择gradle,点击Next

3.勾选项目基本组成部分

4.点击Finish创建项目

5.项目目录结构如下,标红部分是自己创的

注意,如果demoo2项目文件夹为普通文件,无法新建package,需要点击如下图标,将src标注为Source

6.编辑WelcomeController的内容

7.编辑index.html的内容

8.配置build.gradle的内容

文本如下

plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-web-services'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    compile group:'org.springframework.boot',name:'spring-boot-devtools',version: '2.0.4.RELEASE'//热部署
}

9.配置application.properties文件,内容如下

 
 

文本如下:

spring.datasource.url=jdbc:mysql:/localhost:3306/test?useUnicode=true&CharacterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


spring.thymeleaf.cache=false

10.配置完毕,启动项目

注意,默认端口是8080,你可以在application.properties文件中添加server.port=8090进行修改端口号,浏览器中输入:

localhost:8090/welcome即可。 

 

spring boot默认是集成html页面的,如果页面需要传参,那么就需要集成jsp

SpringBoot集成jsp

1.application.properties文件内容更改为:

spring.datasource.url=jdbc:mysql:/localhost:3306/test?useUnicode=true&CharacterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#主要配置
spring.thymeleaf.cache=false
spring.thymeleaf.enabled=false
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/,classpath:/static,classpath:/templates
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

2.build.gradle依赖如下

plugins {
    id 'org.springframework.boot' version '2.1.5.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.zy'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
 

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-web-services'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.h2database:h2'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    
    //注意点
    compile 'javax.servlet:jstl:1.2'
    compile 'org.apache.tomcat.embed:tomcat-embed-jasper'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile group:'javax.servlet',name:'javax.servlet-api'
    compile group:'org.springframework.boot',name:'spring-boot-starter-tomcat'
}

3.新增目录结构:

4.启动类集成SpringBootServletInitializer类,重写 configure 方法

 代码如下:

package com.zy.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DemoApplication.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

启动项目正常访问即可