<?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.wanye</groupId> <artifactId>com.wanye.springboot</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
package com.wanye; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by wanye on 2017/5/20. */ @SpringBootApplication public class Start { public static void main(String[] args) { SpringApplication.run(Start.class, args); } }
package com.wanye.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; /** * Created by wanye on 2017/5/20. */ @RestController // @Controller + @ResponseBody public class HelloController { @RequestMapping("/hello") public Map<String, String> hello(){ Map<String, String> hello = new HashMap<String, String>(); hello.put("data", "hello 小紅"); hello.put("status", "SUCCESS"); return hello; } }
經過main()方法來啓動
訪問http://localhost:8080/hello 咱們能夠看到頁面返回了數據,而且自動轉換成JSON格式,接下來咱們講解剛剛用到的註解html
在整合JSP/FreeMarker以前,咱們先了解一下spring boot對於controller的支持java
<?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.wanye</groupId> <artifactId>com.wanye.springboot</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 整合jsp --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- 整合jsp --> </dependencies> </project>
<html> <head> <title>jsp</title> </head> <body> hello jsp </body> </html>
# 頁面默認前綴目錄 spring.mvc.view.prefix=/WEB-INF/jsp/ # 響應頁面默認後綴 spring.mvc.view.suffix=.jsp
package com.wanye.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by wanye on 2017/5/20. */ @Controller public class JSPController { @RequestMapping("/jsp/home") public String home() { return "home"; } }
#必須⽤用sping-boot:run啓動 mvn clean spring-boot:run
訪問http://localhost:8080/jsp/home 頁面返回」hello jsp」,說明整合JSP成功,該請求可以訪問到home.jsp這個文件web
刪除剛剛jsp的pom配置,並修改spring boot 啓動依賴的jarspring
<?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.wanye</groupId> <artifactId>com.wanye.springboot</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </dependencies> </project>
<html> <head> <title></title> </head> <body> hello freemarker. ${time?string("yyyy-MM-dd hh:mm:ss")} </body> </html>
package com.wanye.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.Date; /** * Created by wanye on 2017/5/20. */ @Controller public class FreemarkerController { @RequestMapping("/ftl/home1") public String home1(Model model) { model.addAttribute("time", new Date(System.currentTimeMillis())); return "home"; } @RequestMapping("/ftl/home2") public ModelAndView home2() { ModelAndView res = new ModelAndView("home"); res.addObject("time", new Date(System.currentTimeMillis())); return res; } }
這裏經過兩種方式,向頁面傳遞參數「time」。你們本身瞭解一下Model, ModelAndView的使用方法,我這裏就不進行詳細的講述了。數據庫
兩種方式均可以:主函數main()啓動或者spring-boot:run
訪問http://localhost:8080/ftl/home1 頁面,驗證是否輸出了當前時間。關於FreeMarker語法,你們本身瞭解一下,不是本文關注的重點apache
本文講述了(json,jsp,freemarker)配置及整合方法,並針對web開發經常使用的註解的概念及功能進行了介紹,留下了一個疑問:爲何整合jsp後必須經過spring-boot:run方式啓動?歡迎你們留言討論。json
@SpringBootApplication 等價於 @Configuration + @ComponentScan + @EnableAutoConfigurationsegmentfault
若本文對你有幫助,望點贊。爲了提升你們學習效果,錄製了同步的視頻課程,還望你們支持視頻課程tomcat