1. 寫在前面
- 寫代碼不要著急,注意配置文件或目錄名不要存在空格
2. 開發(fā)環(huán)境/ 工具準備
- JDK1.8.0_25
C:\Users\Administrator>java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b18)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
- Maven3.3.3
C:\Users\Administrator>mvn -v
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-
Maven home: D:\appData\maven\apache-maven-3.3.3\bin\..
Java version: 1.8.0_25, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_25\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"
- eclipse
3. 新建 Maven 項目
由于我這里已經(jīng)建過一個 spring-boot-example
了温治,所以就直接跳過了饭庞。
4. 配置 pom.xml 依賴
以下是我的 pom.xml
配置:
<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.example.springboot</groupId>
<artifactId>spring-boot-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- Spring Boot parent dependency -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 開發(fā)web項目必須依賴的 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot 自帶的 tomcat 服務器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- jstl 標簽庫 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- spring-boot默認已經(jīng)不再支持jsp視圖展示,要支持jsp需要加這個依賴 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
5. 建立項目目錄結構
以下是我的項目目錄截圖:
這里需要注意了熬荆, Application.java
為 Spring Boot
的應用程序配置文件舟山,是整個項目的啟動文件,必須放在 com.example
中, 即所有模塊(dao累盗、entity寒矿、service、web)的父包中若债。
6. 配置 application.properties
在你的 src/main/resources
目錄下配置 application.properties
文件:
spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
application.message: Hello Phil
webapp
目錄結構:
其中符相,index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>spring-boot example</title>
</head>
<body>
<p>index.jsp</p>
</body>
</html>
welcome.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>spring-boot example</title>
</head>
<body>
<p>welcome, spring-boot!</p>
</body>
</html>
7. 編寫程序啟動入口類 Application.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
8. 編寫 HelloController.java
package com.example.web;
import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@Value("${application.message: Hello World}")
private String message = "Hello World";
@RequestMapping("/")
public String main () {
return "index";
}
@RequestMapping("/welcome")
public String welcome(Map<String, Object> model){
model.put("time", new Date());
model.put("message", this.message);
return "welcome";
}
}
9. 啟動項目
- 打開終端,輸入下面命令行:
mvn spring-boot:run
訪問: localhost:8080
顯示 :
訪問 localhost:8080/welcome
顯示:
Enjoy it~