Spring Boot項目構建,springboot構建
環境準備
IDEA+JDK 1.8+Maven+mysql+SSM
1、使用Spring Boot架構可以大大加速Web應用的開發過程,首先在Maven項目依賴中引入spring-boot-starter-web:
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.tianmaying</groupId> <artifactId>spring-web-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-web-demo</name> <description>Demo project for Spring WebMvc</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.5.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
運行應用:mvn spring-boot:run或在IDE中運行main()方法,在瀏覽器中訪問http://localhost:8080,Hello World!就出現在了頁面中。只用了區區十幾行Java代碼,一個Hello World應用就可以正確運行了,那麼這段代碼究竟做了什麼呢?我們從程式的入口SpringApplication.run(Application.class, args);開始分析:
使用@Controller實現URL路由
現代Web應用往往包括很多頁面,不同的頁面也對應著不同的URL。對於不同的URL,通常需要不同的方法進行處理並返回不同的內容。
匹配多個URL
@RestControllerpublic class Application { @RequestMapping("/") public String index() { return "Index Page"; } @RequestMapping("/hello") public String hello() { return "Hello World!"; }}
@RequestMapping可以註解@Controller類:
@RestController@RequestMapping("/classPath")public class Application { @RequestMapping("/methodPath") public String method() { return "mapping url is /classPath/methodPath"; }}
method方法匹配的URL是/classPath/methodPath"。
提示
可以定義多個@Controller將不同URL的處理方法分散在不同的類中
URL中的變數——PathVariable
在Web應用中URL通常不是一成不變的,例如微博兩個不同使用者的個人首頁對應兩個不同的URL:http://weibo.com/user1,http://weibo.com/user2。我們不可能對於每一個使用者都編寫一個被@RequestMapping註解的方法來處理其請求,Spring MVC提供了一套機制來處理這種情況:
@RequestMapping("/users/{username}")public String userProfile(@PathVariable("username") String username) { return String.format("user %s", username);}@RequestMapping("/posts/{id}")public String post(@PathVariable("id") int id) { return String.format("post %d", id);}
在上述例子中,URL中的變數可以用{variableName}來表示,同時在方法的參數中加上@PathVariable("variableName"),那麼當請求被轉寄給該方法處理時,對應的URL中的變數會被自動賦值給被@PathVariable註解的參數(能夠自動根據參數類型賦值,例如上例中的int)。
支援HTTP方法
對於HTTP請求除了其URL,還需要注意它的方法(Method)。例如我們在瀏覽器中訪問一個頁面通常是GET方法,而表單的提交一般是POST方法。@Controller中的方法同樣需要對其進行區分:
@RequestMapping(value = "/login", method = RequestMethod.GET)public String loginGet() { return "Login Page";}@RequestMapping(value = "/login", method = RequestMethod.POST)public String loginPost() { return "Login Post Request";}
模板渲染
在之前所有的@RequestMapping註解的方法中,傳回值字串都被直接傳送到瀏覽器端並顯示給使用者。但是為了能夠呈現更加豐富、美觀的頁面,我們需要將HTML代碼返回給瀏覽器,瀏覽器再進行頁面的渲染、顯示。
一種很直觀的方法是在處理請求的方法中,直接返回HTML代碼,但是這樣做的問題在於——一個複雜的頁面HTML代碼往往也非常複雜,並且嵌入在Java代碼中十分不利於維護。更好的做法是將頁面的HTML代碼寫在模板檔案中,渲染後再返回給使用者。為了能夠進行模板渲染,需要將@RestController改成@Controller:
import org.springframework.ui.Model;@Controllerpublic class HelloController { @RequestMapping("/hello/{name}") public String hello(@PathVariable("name") String name, Model model) { model.addAttribute("name", name); return "hello" }}
在上述例子中,傳回值"hello"並非直接將字串返回給瀏覽器,而是尋找名字為hello的模板進行渲染,我們使用Thymeleaf模板引擎進行模板渲染,需要引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
接下來需要在預設的模板檔案夾src/main/resources/templates/目錄下添加一個模板檔案hello.html:
<!DOCTYPE HTML><html xmlns:th="http://www.thymeleaf.org"><head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body> <p th:text="'Hello, ' + ${name} + '!'" /></body></html>
th:text="'Hello, ' + ${name} + '!'"也就是將我們之前在@Controller方法裡添加至Model的屬性name進行渲染,並放入<p>標籤中(因為th:text是<p>標籤的屬性)。模板渲染還有更多的用法,請參考Thymeleaf官方文檔。
處理靜態檔案
瀏覽器頁面使用HTML作為描述語言,那麼必然也脫離不了CSS以及JavaScript。為了能夠瀏覽器能夠正確載入類似/css/style.css, /js/main.js等資源,預設情況下我們只需要在src/main/resources/static目錄下添加css/style.css和js/main.js檔案後,Spring MVC能夠自動將他們發布,通過訪問/css/style.css, /js/main.js也就可以正確載入這些資源。
轉載:http://blog.csdn.net/xiaoyu411502/article/details/47864969