使用idea创建第一个springBoot项目

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/woshimuyi1025/article/details/78970221

idea下载地址:https://www.jetbrains.com/idea/

创建项目步骤:






配置pom依赖的包,然后更新依赖包,pom.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yql</groupId>
    <artifactId>springBootDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>



配置resources下面的Web资源文件,这里我就配置两个文件,一个是用来存放静态文件夹的static文件,还有一个就是用来存放HTML的资源文件夹templates。这里需要特别主要的是:static文件中一般存放css,js,image等静态资源文件,而templates文件中一般存放各种HTML文件。而且这两个文件都是默认存在的,路径不需要特别的配置就可以直接引用了。aplication.properties是个配置文件,这里面可以配置SpringBoot的相关信息。大家需要注意的是这个文件名千万不要写错,也不要放错位置,不然都不会生效的。书写springBoot入口函数和springMVC控制器测试


springBoot入口函数:

DemoApplication代码:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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



HelloController代码:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller
public class HelloController {
    @RequestMapping("/index")
    public String index(){
        return "welcome";
    }
}
application.propertis代码:
#修改tomcat的默认的端口号,将8080改为8888
server.port=8888

csstest.css代码:
body {
    padding: 0px;
    margin: auto;
    font-family: "黑体", "仿宋", Arial, "Arial Unicode MS", System;
    background-color: #00F;
    font-size: 20px;
    text-align: left;
}

welcome.html代码:
<html>
<head>
    <title>Title</title>
</head>
<link href="css/csstest.css" rel="stylesheet"/>
<body>
<p>welcome page is login.........</p>
</body>
</html>


在DemoApplication页面右击然后run
如果报错:Error:java: 无效的源发行版: 1.8 
请确认以下地方jdk版本:
  • File -> Project Structure -> Project Settings
  • File -> Project Structure -> Module Settings -> Tab: Sources: Language Level
  • File -> Project Structure -> Module Settings -> Tab: Dependencies: Module SDK
  • File -> Settings -> Compiler -> Java Compiler -> Target bytecode version
将jdk版本与本地统一后,先file--invalidate caches--Invalidate清理一下缓存,然后再次启动访问。访问地址:http://127.0.0.1:8888/index



            
阅读更多
换一批

没有更多推荐了,返回首页