1. 写在前面
- 写代码不要着急,注意配置文件或目录名不要存在空格
2. 开发环境/ 工具准备
- 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 项目
由于我这里已经建过一个 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>
<!-- 开发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默认已经不再支持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~