現在要用 spring boot 來改寫 spring MVC - getting started 這一篇的程式,同樣的,架構採用 spring MVC,spring framework 最讓人詬病的就是設定非常複雜,spring boot 的出現主要就在解決這個問題。現在開始來看一下程式怎麼寫 …
上圖是程式碼放罝的位置,這是 Gradle 的習慣。
- embedded tomcat 不支援 jsp,所以這個程式的顯示都使用 thymeleaf 板型的 html,這是 spring boot 提供的,支援 html 5。
- 網頁相關的檔案放在 /src/main/resources 目錄下,html 放在 templates 子目錄,css、javascript 放在 static 子目錄。
- spring boot 的設定預設放在 application.properties,這個檔通常放在 classpath 的根目錄 【註】。
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'idv.steven'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.2.1'
compile group: 'javax.inject', name: 'javax.inject', version: '1'
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
package idv.steven.demo.database.entity;
import java.sql.Timestamp;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Entity
@Table(name="users")
@Data
public class UsersEntity {
@Id
private String name;
private String password;
private Timestamp createtime;
}
package idv.steven.demo.database.dao; import org.springframework.data.repository.CrudRepository; import idv.steven.demo.database.entity.UsersEntity; public interface UsersDao extends CrudRepository{ }
server.port=8080 spring.datasource.url=jdbc:mariadb://192.168.0.103:3306/demo spring.datasource.username=root spring.datasource.password=p@ssw0rd##
- 資料庫連線相關設定,只要在 application.properties 中如上設定即可,通常只要設定 url、username、password,不需要設定 driver-class-name,spring boot 從 url 就能判斷現在要使用的是那一種資料庫。
- 第一行 server.port 是設定 tomcat 要啟動後監聽那一個 port,預設就是 8080,如果本來就是要使用 8080,這一行可以刪除。與 tomcat 相關的設定還有:
- server.session-timeout (以秒為單位)
- server.context-path (預設為 / )
- server.tomcat.uri-encoding (預設為 UTF-8)
- server.tomcat.compression (預設為 off)
package idv.steven.demo.controller;
import javax.inject.Inject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import idv.steven.demo.database.dao.UsersDao;
import idv.steven.demo.database.entity.UsersEntity;
import idv.steven.demo.model.IndexForm;
@Controller
public class IndexController {
@Inject
private UsersDao daoUsers;
@RequestMapping("/index")
public String login(IndexForm form) {
if (form.getUserName() != null) {
UsersEntity user = daoUsers.findOne(form.getUserName());
if (user != null && user.getPassword().equals(form.getPassword())) {
return "success";
}
}
else {
return "login";
}
return "fail";
}
}
<html xmlns:th="http://www.thymeleaf.org">
<head>
<link rel="sytlesheet" th:href="@{bootstrap/css/bootstrap.min.css}"></link>
<link rel="sytlesheet" th:href="@{bootstrap/css/bootstrap-theme.min.css}"></link>
</head>
<body>
<form action="index.do" method="post">
帳號: <input name="userName" type="text" />
密碼: <input name="password" type="password" />
<input type="submit" value="登入" />
</form>
</body>
</html>
package idv.steven.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApp { public static void main(String[] args) { SpringApplication.run(DemoApp.class, args); } }
如果是在 eclipse 中開發程式,可以在專案名稱上按右鍵,選用「Run As > Spring Boot App」,spring boot 就會啟動 embedded tomcat,然後可以打開瀏覽器測試。
如上,輸入帳號、密碼後按【登入】,即會導向 Controller,再由 Controller 判斷要導向 success.html 或 fail.html。
【註】
application.properties 可以放在以下四個位置:
- 外置,在相對於應用程式運行目錄的 /config 子目錄裡。
- 外置,在應用程式運行的目錄裡。
- 內置,在 config package 內。
- 內置,在 classpath 根目錄。
〈參考資料〉https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files
【補充】
上面的程式是 compile 成 jar 檔,執行在 spring boot 包裝進 jar 檔裡的 embedded tomcat 上,如果想要 compile 成 war 檔並執行在一般的 tomcat,要如下修改:
- DemoApp.java (啟動類別)
package idv.steven.demo; 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 DemoApp extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApp.class); } public static void main(String[] args) { SpringApplication.run(DemoApp.class, args); } }
- build.gradle
apply plugin: 'war'
gradle war







