基於Spring Boot的登入demo
本專案基於Spring Boot框架,搭建了一個簡單的登入微服務。 Spring Boot相對於傳統的SSM(Spring MVC + Mybatis + Spring)框架用起來更加簡單,不需要進行復雜的配置,方便靈活。 Spring Boot讓我們的Spring應用變的更輕量化。比如:你可以僅僅依靠一個Java類來執行一個Spring應用。你也可以打包你的應用為jar並通過使用java -jar來執行你的Spring Web應用。
使用Spring Boot可以很方便的建立微服務。
效果圖
效果圖
應用技術
Spring Boot + bootstrap + thymeleaf
專案搭建
使用Intellij中的Spring Initializr來快速構建Spring Boot
選單欄中選擇File=>New=>Project..
一直點下一步
p1
p2
p3
p4
最後點選Finish
聯網自動從網站上下載Spring Boot的模板,稍作等待框架就搭好啦。
專案目錄結構
上面步驟中的專案名字和這個截圖有點不一樣(login) src/main/java/ 為程式碼檔案 src/main/resources/ 為資原始檔
為了保證專案資源結構的清晰,我們把 src/main/java/ 再進一步進行劃分: bean 目錄存放的是要用到的實體類 controller 目錄存放的是控制層類
src/main/resources/template/ 為靜態頁面的模板檔案,這裡用了thymeleaf模板渲染引擎框架(據說Spring Boot推薦) src/main/resources/application.properties 為Spring Boot的配置檔案
p5
maven配置
我們做的是Java web專案,在其預設生成的maven配置檔案中新增web和thymeleaf依賴。 pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
application.properties配置
這裡我們配置 thymeleaf模板渲染引擎
# Enable template caching.
spring.thymeleaf.cache=true
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true
# Content-Type value.
spring.thymeleaf.servlet.content-type=text/html
# Enable MVC Thymeleaf view resolution.
spring.thymeleaf.enabled=true
# Template encoding.
spring.thymeleaf.encoding=UTF-8
# Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.excluded-view-names=
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.mode=HTML
# Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.prefix=classpath:/templates/
# Suffix that gets appended to view names when building a URL.
spring.thymeleaf.suffix=.html
其餘的Spring Boot屬性配置檔案參考(本專案沒有配置,使用的預設):Spring Boot屬性配置檔案詳解
Bean
本專案為使用者登入,只考慮使用者這一個角色,包含使用者名稱和密碼。
package me.tianle.login.bean;
public class User {
private String name;
private String password;
public User(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Controller
web專案的控制器寫在這裡,處理頁面的請求,前後臺互動
@Controller:修飾class,用來建立處理http請求的物件 @RestController:Spring4之後加入的註解,原來在@Controller中返回json需要@ResponseBody來配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,預設返回json格式。 @RequestMapping:配置url對映
目前版本沒有新增資料庫,能否登入判斷邏輯直接寫死在程式碼中。
package me.tianle.login.controller;
import me.tianle.login.bean.User;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class LoginController {
@RequestMapping("/")
public ModelAndView index() {
return new ModelAndView("index");
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute User user) {
String name = user.getName();
String password = user.getPassword();
if (name.equals("qinya") && password.equals("tianle")) {
return "Success";
} else {
return "Failed";
}
}
}
專案打包執行
使用maven進行打包。
mvn install
Spring Boot裡面嵌入了Tomcat,直接執行
java -jar xxx.jar
Java高架構師、分散式架構、高可擴充套件、高效能、高併發、效能優化、Spring boot、Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分散式專案實戰學習架構師視訊免費學習加群:835638062 點選連結加入群聊【Java高階架構】:https://jq.qq.com/?_wv=1027&k=5S3kL3v