springboot返回modelandview頁(yè)面的實(shí)例
1、添加依賴
這個(gè)應(yīng)該是web項(xiàng)目相關(guān)的jar
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<!-- jstl JSP標(biāo)準(zhǔn)標(biāo)簽庫(kù) --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 返回jsp頁(yè)面還需要這個(gè)依賴 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
2、application.properties
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
我這里是parent是1.5.10,所以jsp的配置應(yīng)該如下
#jsp path spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp 老版本的應(yīng)該是這個(gè) spring.view.prefix=/WEB-INF/jsp/ spring.view.suffix=.jsp
3、控制器
因?yàn)槭欠祷仨?yè)面,所以不能用@RestController返回json格式
package com.example.demo.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; @Controller /*@ComponentScan*/ @RequestMapping("/test") public class TestController { private final Logger log = LoggerFactory.getLogger(this.getClass()); @RequestMapping(value = "queryMaterialType", method = RequestMethod.POST) public Object test(){ log.info("--------------->>打印日志"); return "hellow world"; } //@RestController,返回json數(shù)據(jù) //@Controller,返回login.jsp頁(yè)面 @RequestMapping(value = "/login", method = RequestMethod.GET) public String login(HttpServletRequest request,HttpServletResponse response){ return "login"; } //無(wú)論是@RestController還是@Controller都不影響返回頁(yè)面 @RequestMapping(value = "/loginPage", method = RequestMethod.GET) public ModelAndView loginPage(HttpServletRequest request,HttpServletResponse response){ ModelAndView mav = new ModelAndView(); mav.setViewName("login"); return mav; } }
補(bǔ)充知識(shí):springBoot前后分離項(xiàng)目,通過(guò)ModelAndView返回給app或前臺(tái)靜態(tài)頁(yè)面
1.先做靜態(tài)頁(yè)模板aaa.html,放到springboot項(xiàng)目的根目錄下,如下如中,新建一個(gè)templates的文件夾,將靜態(tài)頁(yè)放到這里面就可以了
靜態(tài)頁(yè)代碼為
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"> <meta name="viewport" content="width=device-width, initial-scale=0.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> <meta content="yes" name="apple-mobile-web-app-capable"> <meta content="black" name="apple-mobile-web-app-status-bar-style"> <meta content="telephone=no" name="format-detection"> <title>標(biāo)題</title> <style type="text/css"> body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; background: #f3f3f3; font-family: "Microsoft YaHei ", "微軟雅黑", "arial"; } h1 { height: 1px; width: 100%; margin: 10px 0; background: #f1f1f1; } img { width: 100%; height: auto; } .bodys { width: 100%; height: auto; overflow: Hidden; padding-top: 10px; padding-bottom: 50px; background: #fff; } .head { width: 96%; min-height: 30px; padding: 18px 2% 2px 2%; line-height: 25px; text-align: left; font-size: 20px; font-weight: bold; color: #111; } .time { width: 96%; height: 20px; line-height: 20px; font-size: 11px; text-align: left; padding: 0 2%; color: #999; } .info { width: 96%; height: auto; padding: 10px 2%; line-height: 25px; text-align: left; font-size: 15px; } </style> </head> <body> <div class="bodys"> <div class="head" th:text="${itle}">未知</div> <div class="time" th:text="${addDate}">未知</div> <h1></h1> <div class="info" th:utext="${content}">未知</div> </div> </body> </html>
2.然后主要是 controller層,業(yè)務(wù)邏輯根據(jù)自己的需求來(lái)
@RequestMapping("/html") @Controller public class AppCommonHtmlController { @RequestMapping(value = "/ceshi", method = RequestMethod.GET) public ModelAndView getCeishi(“根據(jù)自己業(yè)務(wù)傳入需要的參數(shù)”) { ModelAndView modelAndView=new ModelAndView(); //根據(jù)自己的業(yè)務(wù),和靜態(tài)頁(yè)中的參數(shù)對(duì)應(yīng)上就行,也可以放入實(shí)體類,和靜態(tài)頁(yè)面對(duì)應(yīng)就行了 modelAndView.addObject("title",“標(biāo)題”); modelAndView.addObject("addDate",“添加時(shí)間”); modelAndView.addObject("content",“內(nèi)容”); //存入靜態(tài)頁(yè)的名稱,就可以把處理好的靜態(tài)頁(yè)返回給app或前臺(tái) modelAndView.setViewName("aaa"); return modelAndView; } }
然后瀏覽器輸入:http://localhost:8888/項(xiàng)目名/html/ceshi
該方法多適用于app端,需要根據(jù)不同的情況得到不一樣內(nèi)容的靜態(tài)頁(yè)展示到手機(jī)上,就可以通過(guò)這種方法,做一個(gè)靜態(tài)頁(yè)的模板,通過(guò)el表達(dá)式給模板不同的內(nèi)容,然后app端可以通過(guò)訪問(wèn)的ip直接獲取到靜態(tài)頁(yè)
下面的方法也可以,效果同上面一樣
靜態(tài)頁(yè)代碼
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"> <meta name="viewport" content="width=device-width, initial-scale=0.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> <meta content="yes" name="apple-mobile-web-app-capable"> <meta content="black" name="apple-mobile-web-app-status-bar-style"> <meta content="telephone=no" name="format-detection"> <title>靜態(tài)頁(yè)</title> <style type="text/css"> body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; background: #f3f3f3; font-family: "Microsoft YaHei ", "微軟雅黑", "arial"; } h1 { height: 1px; width: 100%; margin: 10px 0; background: #f1f1f1; } img { width: 100%; height: auto; } .bodys { width: 100%; height: auto; overflow: Hidden; padding-top: 10px; padding-bottom: 50px; background: #fff; } .head { width: 96%; min-height: 30px; padding: 18px 2% 2px 2%; line-height: 25px; text-align: left; font-size: 20px; font-weight: bold; color: #111; } .time { width: 96%; height: 20px; line-height: 20px; font-size: 11px; text-align: left; padding: 0 2%; color: #999; } .info { width: 96%; height: auto; padding: 10px 2%; line-height: 25px; text-align: left; font-size: 15px; } </style> </head> <body> <div class="bodys"> <div class="head" th:text="${bbb.noticeTitle}">未知</div> <div class="time" th:text="${bbb.publishDate}">未知</div> <h1></h1> <div class="info" th:utext="${bbb.noticeContent}">未知</div> </div> </body> </html>
controller代碼
@RequestMapping(value = "/ceshi", method = RequestMethod.GET) public String getCeishi(“業(yè)務(wù)邏輯需要的入?yún)ⅰ? Model model) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); JSONObject jsonObject = JSONObject.fromObject(“需要傳到靜態(tài)頁(yè)的動(dòng)態(tài)數(shù)據(jù)”); jsonObject.remove("publishDate");//時(shí)間,具體作用不清楚,個(gè)人猜測(cè)是原本數(shù)據(jù)格式不支持,需要轉(zhuǎn)換一下,所以要先刪除后重新賦值 jsonObject.put("publishDate", sdf.format(notice.getPublishDate()));//時(shí)間重新賦值 model.addAttribute("bbb", jsonObject);//將轉(zhuǎn)換好的數(shù)據(jù)存入model中 return "aaa"; //對(duì)應(yīng)好靜態(tài)頁(yè)的名稱return出去就可以了 }
以上這篇springboot返回modelandview頁(yè)面的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMvc使用GoogleKaptcha生成驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了SpringMvc項(xiàng)目中使用GoogleKaptcha 生成驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09Elasticsearch查詢及聚合類DSL語(yǔ)句寶典示例詳解
這篇文章主要為大家介紹了Elasticsearch查詢及聚合類DSL語(yǔ)句寶典示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Java實(shí)現(xiàn)國(guó)產(chǎn)加密算法SM4的示例詳解
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)國(guó)產(chǎn)加密算法SM4(ECB和CBC兩種模式),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01mybatis打印SQL,并顯示參數(shù)的實(shí)例
這篇文章主要介紹了mybatis打印SQL,并顯示參數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12使用Java把文本內(nèi)容轉(zhuǎn)換成網(wǎng)頁(yè)的實(shí)現(xiàn)方法分享
這篇文章主要介紹了使用Java把文本內(nèi)容轉(zhuǎn)換成網(wǎng)頁(yè)的實(shí)現(xiàn)方法分享,利用到了Java中的文件io包,需要的朋友可以參考下2015-11-11java實(shí)現(xiàn)上傳圖片進(jìn)行切割的方法
這篇文章主要介紹了java實(shí)現(xiàn)上傳圖片進(jìn)行切割的方法,以完整實(shí)例形式分析了Java針對(duì)上傳圖片進(jìn)行切割的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02一個(gè)Servlet是如何處理多個(gè)請(qǐng)求的?
以前我一直以為一個(gè)Servlet只能處理一個(gè)請(qǐng)求,后來(lái)發(fā)現(xiàn)是自己太菜了,可以借助攜帶一個(gè)參數(shù)來(lái)完成多個(gè)請(qǐng)求的處理,根據(jù)參數(shù)的不同,在核心的service方法中調(diào)用不同的業(yè)務(wù)方法,來(lái)實(shí)現(xiàn)處理多個(gè)servlet請(qǐng)求的目的,廢話不多說(shuō),直接上代碼,需要的朋友可以參考下2021-06-06Java實(shí)戰(zhàn)之用springboot+netty實(shí)現(xiàn)簡(jiǎn)單的一對(duì)一聊天
這篇文章主要介紹了Java實(shí)戰(zhàn)之用springboot+netty實(shí)現(xiàn)簡(jiǎn)單的一對(duì)一聊天,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)Java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04將本地SpringBoot項(xiàng)目發(fā)布到云服務(wù)器的方法
這篇文章主要介紹了如何將本地SpringBoot項(xiàng)目發(fā)布到云服務(wù)器,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12