springboot中三种xxxx.setAttribute()并与python中flask作对应比较+容器的通俗理解
生活随笔
收集整理的這篇文章主要介紹了
springboot中三种xxxx.setAttribute()并与python中flask作对应比较+容器的通俗理解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
對應關系如下:
| Java | python |
| Spring boot | Flask |
| import javax.servlet.http.HttpServletRequest; | from flask import g |
| import javax.servlet.http.HttpSession; HttpSession.setAttribute() | from flask import session |
| import org.springframework.web.context.request.WebRequest;
| return {'returnCode': 0, 'returnValue': returnValue} |
| Thymeleaf | Jinja2 |
?
完整代碼舉例如下:
package org.fkit.thymeleaftest.controller;import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.fkit.thymeleaftest.domain.Book; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.WebRequest;@Controller public class ThymeleafController {/** 保存數據到作用范圍域,用于測試Thymeleaf表達式訪問數據* */@RequestMapping("/regexptest")public String regexptest(HttpServletRequest request,HttpSession session){System.out.println(request.getParameter("loginName"));System.out.println(request.getParameter("password"));//接受頁面請求中的參數(這里其實是視圖函數) //接收參數后直接打印,其實這里應該擴展為去數據庫進行校驗request.setAttribute("book", "瘋狂Spring Boot講義");// 保存數據到session作用范圍域當中session.setAttribute("school", "瘋狂軟件");// 保存數據到ServletContext(application)作用范圍域當中request.getServletContext().setAttribute("name", "Thymeleaf模板引擎");return "success1";}/** 保存數據到作用范圍域,用于測試Thymeleaf的條件判斷* */@RequestMapping("/iftest")public String iftest(WebRequest webRequest){// 保存數據到request作用范圍域,Spring MVC更推薦使用WebRequestwebRequest.setAttribute("username", "fkit", RequestAttributes.SCOPE_REQUEST);webRequest.setAttribute("age", 21, RequestAttributes.SCOPE_REQUEST);webRequest.setAttribute("role", "admin", RequestAttributes.SCOPE_REQUEST); //s:變量名稱 //o:變量取值 //role:變量return "success2";}/** 保存數據到作用范圍域,用于測試Thymeleaf的循環獲取數據* */@RequestMapping("/eachtest")public String eachtest(WebRequest webRequest){// 模擬數據庫數據保存到List集合List<Book> books = new ArrayList<>();// 下面將對象初始化以后加入列表books.add(new Book(1, "瘋狂Java講義-補充", "java.jpg", "李剛 編著", 109.00));books.add(new Book(2, "輕量級Java EE企業應用實戰", "ee.jpg", "李剛 編著", 108.00));books.add(new Book(3, "Spring+MyBatis應用實戰", "SpringMyBatis.jpg", "瘋狂軟件 編著", 58.00));books.add(new Book(4, "瘋狂Android講義", "android.jpg", "李剛 編著", 108.00));books.add(new Book(5, "瘋狂Ajax開發", "ajax.jpg", "李剛 編著", 79.00));// 保存數據到request作用范圍域webRequest.setAttribute("books", books, RequestAttributes.SCOPE_REQUEST);/*這里的數據是通過thymeleaf傳遞到網頁上*/ // 這里“books”是與html頁面對應的變量名 // 上面的webRequest是用來設置傳遞給html的變量的return "success3";}}關于上述三個java的thymeleaf用法的具體區別,也可以參考[1]
?
?
Springboot中的容器到底是什么意思呢?
我們想必聽過docker,也就是一個系統里面再做一個系統,
所以Spring boot中的容器概念其實類似于Javaweb版本的docker.
?
Reference:
[1]Difference of 3 xxx.setAttribute()
總結
以上是生活随笔為你收集整理的springboot中三种xxxx.setAttribute()并与python中flask作对应比较+容器的通俗理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4K 分辨率 899 元到手:联合创新
- 下一篇: 误删/usr/bin/python2.7