摘要:大家自己了解一下的使用方法,我這里就不進(jìn)行詳細(xì)的講述了。啟動(dòng)方式兩種方式都可以主函數(shù)啟動(dòng)或者驗(yàn)證訪問(wèn)頁(yè)面,驗(yàn)證是否輸出了當(dāng)前時(shí)間。為了提高大家學(xué)習(xí)效果,錄制了同步的視頻課程,還望大家支持視頻課程
Spring Boot - 初識(shí) Hello World 索引
Spring Boot - 初識(shí) Hello World
Spring Boot - Servlet、過(guò)濾器、監(jiān)聽器、攔截器
Spring Boot - 靜態(tài)資源處理、啟動(dòng)加載、日志處理
Spring Boot - 數(shù)據(jù)庫(kù)配置
Spring Boot - 部署Deploy
準(zhǔn)備Jdk8
Ide intelliJ IDEA 或者 eclipse
Maven 3
返回Json格式數(shù)據(jù) 修改pom依賴創(chuàng)建啟動(dòng)類4.0.0 com.wanye com.wanye.springboot 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.5.1.RELEASE org.springframework.boot spring-boot-starter-web
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); } }創(chuàng)建Controller
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啟動(dòng)方式hello(){ Map hello = new HashMap (); hello.put("data", "hello 小紅"); hello.put("status", "SUCCESS"); return hello; } }
通過(guò)main()方法來(lái)啟動(dòng)驗(yàn)證
訪問(wèn)http://localhost:8080/hello 我們可以看到頁(yè)面返回了數(shù)據(jù),并且自動(dòng)轉(zhuǎn)換成JSON格式,接下來(lái)我們講解剛剛用到的注解
整合JSP/FreeMarker在整合JSP/FreeMarker之前,我們先了解一下spring boot對(duì)于controller的支持
模版引擎:spring boot支持FreeMarker 、Groovy 、Thymeleaf (Spring 官?網(wǎng)使?用這個(gè))、Velocity 、JSP
接收參數(shù):@RequestBody、@RequestParam、@ModelAttribute、JSONObject、HttpEntity 等
通過(guò)JSP模板引擎渲染4.0.0 com.wanye com.wanye.springboot 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.5.1.RELEASE org.springframework.boot spring-boot-starter-web org.apache.tomcat.embed tomcat-embed-jasper provided javax.servlet jstl
增加?目錄“src/main/webapp/WEB-INF/jsp/”,將jsp?文件放?入這個(gè)?目錄中,示例home.jsp代碼(只用來(lái)驗(yàn)證是否訪問(wèn)到該文件)
jsp hello jsp
在?目錄“resources”中,增加application.properties配置?文件
# 頁(yè)面默認(rèn)前綴目錄 spring.mvc.view.prefix=/WEB-INF/jsp/ # 響應(yīng)頁(yè)面默認(rèn)后綴 spring.mvc.view.suffix=.jsp
創(chuàng)建JSPController
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啟動(dòng) mvn clean spring-boot:run
訪問(wèn)http://localhost:8080/jsp/home 頁(yè)面返回”hello jsp”,說(shuō)明整合JSP成功,該請(qǐng)求能夠訪問(wèn)到home.jsp這個(gè)文件
通過(guò)FreeMarker模板引擎渲染刪除剛剛jsp的pom配置,并修改spring boot 啟動(dòng)依賴的jar
4.0.0 com.wanye com.wanye.springboot 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.5.1.RELEASE org.springframework.boot spring-boot-starter-freemarker
在resources下創(chuàng)建templates文件夾,將.ftl文件放?
application.properties文件中無(wú)需配置(刪除jsp配置)
home.ftl文件
hello freemarker. ${time?string("yyyy-MM-dd hh:mm:ss")}
創(chuàng)建FreemarkerController
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; } }
這里通過(guò)兩種方式,向頁(yè)面?zhèn)鬟f參數(shù)“time”。大家自己了解一下Model, ModelAndView的使用方法,我這里就不進(jìn)行詳細(xì)的講述了。
兩種方式都可以:主函數(shù)main()啟動(dòng)或者spring-boot:run
訪問(wèn)http://localhost:8080/ftl/home1 頁(yè)面,驗(yàn)證是否輸出了當(dāng)前時(shí)間。關(guān)于FreeMarker語(yǔ)法,大家自己了解一下,不是本文關(guān)注的重點(diǎn)
總結(jié)本文講述了(json,jsp,freemarker)配置及整合方法,并針對(duì)web開發(fā)常用的注解的概念及功能進(jìn)行了介紹,留下了一個(gè)疑問(wèn):為什么整合jsp后必須通過(guò)spring-boot:run方式啟動(dòng)?歡迎大家留言討論。
注解含義
@SpringBootApplication 等價(jià)于 @Configuration + @ComponentScan + @EnableAutoConfiguration
@Configuration (可以理解為spring的xml配置) +
@ComponentScan (進(jìn)行組件掃描,如果掃描到有@Component、@Controller、@Service等這些注解的類,并注冊(cè)為Bean,可以自動(dòng)收集所有的Spring組件,包括@Configuration類) +
@EnableAutoConfiguration (開啟自動(dòng)配置,這個(gè)注解告訴Spring Boot根據(jù)添加的jar依賴猜測(cè)你想如何配置Spring,建議標(biāo)記在啟動(dòng)類上)
@RestController 等價(jià)于 @Controller + @ResponseBody 返回json數(shù)據(jù)格式(springboot默認(rèn)使用jackson組件進(jìn)行轉(zhuǎn)換)
@RequestMapping 提供路由信息,注冊(cè)訪問(wèn)路徑
最后若本文對(duì)你有幫助,望點(diǎn)贊。為了提高大家學(xué)習(xí)效果,錄制了同步的視頻課程,還望大家支持視頻課程
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/71140.html
摘要:為什么整合后必須通過(guò)方式啟動(dòng)背景在整合這篇文章中,我們用了兩種啟動(dòng)方式方法啟動(dòng)測(cè)試發(fā)現(xiàn),通過(guò)啟動(dòng)能夠正常渲染頁(yè)面,而通過(guò)方法啟動(dòng)無(wú)法渲染,本文分析下原因。通過(guò)來(lái)啟動(dòng)對(duì)應(yīng)的服務(wù)器。 為什么整合jsp后必須通過(guò)spring-boot:run方式啟動(dòng)? 背景 在Spring Boot - 整合Jsp/FreeMarker這篇文章中,我們用了兩種啟動(dòng)方式 mvn clean spring-b...
摘要:過(guò)濾器監(jiān)聽器攔截器上一篇,我們講解了配置及整合方法,不清楚的可以點(diǎn)擊了解的兩種實(shí)現(xiàn)方式通過(guò)手動(dòng)注入實(shí)現(xiàn)一個(gè)返回的方法,并將該對(duì)象注冊(cè)到中。 Spring Boot - Servlet、過(guò)濾器、監(jiān)聽器、攔截器 上一篇,我們講解了spring boot(json,jsp,freemarker)配置及整合方法,不清楚的可以點(diǎn)擊了解 Servlet的兩種實(shí)現(xiàn)方式 通過(guò)@Bean手動(dòng)注入實(shí)現(xiàn)一個(gè)...
摘要:是一個(gè)基于的框架??刂破鲗⒁晥D響應(yīng)給用戶通過(guò)視圖展示給用戶要的數(shù)據(jù)或處理結(jié)果。有了減少了其它組件之間的耦合度。 相關(guān)閱讀: 本文檔和項(xiàng)目代碼地址:https://github.com/zhisheng17/springmvc 轉(zhuǎn)載請(qǐng)注明出處和保留以上文字! 了解 Spring: Spring 官網(wǎng):http://spring.io/ 一個(gè)好的東西一般都會(huì)有一個(gè)好的文檔解釋說(shuō)明,如果你...
摘要:框架搭建首先下載相應(yīng)的包,對(duì)于包有兩種方式使用創(chuàng)建依賴從而導(dǎo)入所需的包。總結(jié)主要進(jìn)行頁(yè)面的請(qǐng)求接受與響應(yīng)。組件包括前端控制器,處理器映射器,處理器適配器,視圖解析器,處理器,視圖。 我之前的文章介紹了如何搭建SSH框架以及如何利用這一框架來(lái)進(jìn)行web應(yīng)用開發(fā),最近我又接觸了SSM框架即Spring+SpringMVC+Mybatis三大框架的整合,而且目前該框架就SSH框架而言使用的較...
閱讀 2272·2021-10-09 09:41
閱讀 3430·2021-09-13 10:34
閱讀 1937·2019-08-30 12:59
閱讀 572·2019-08-29 17:27
閱讀 1073·2019-08-29 16:07
閱讀 2966·2019-08-29 13:15
閱讀 1321·2019-08-29 13:14
閱讀 1573·2019-08-26 12:18