springboot中三种xxxx.setAttribute()并与python中flask作对应比较+容器的通俗理解
生活随笔
收集整理的這篇文章主要介紹了
springboot中三种xxxx.setAttribute()并与python中flask作对应比较+容器的通俗理解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
對(duì)應(yīng)關(guān)系如下:
| 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 {/** 保存數(shù)據(jù)到作用范圍域,用于測試Thymeleaf表達(dá)式訪問數(shù)據(jù)* */@RequestMapping("/regexptest")public String regexptest(HttpServletRequest request,HttpSession session){System.out.println(request.getParameter("loginName"));System.out.println(request.getParameter("password"));//接受頁面請(qǐng)求中的參數(shù)(這里其實(shí)是視圖函數(shù)) //接收參數(shù)后直接打印,其實(shí)這里應(yīng)該擴(kuò)展為去數(shù)據(jù)庫進(jìn)行校驗(yàn)request.setAttribute("book", "瘋狂Spring Boot講義");// 保存數(shù)據(jù)到session作用范圍域當(dāng)中session.setAttribute("school", "瘋狂軟件");// 保存數(shù)據(jù)到ServletContext(application)作用范圍域當(dāng)中request.getServletContext().setAttribute("name", "Thymeleaf模板引擎");return "success1";}/** 保存數(shù)據(jù)到作用范圍域,用于測試Thymeleaf的條件判斷* */@RequestMapping("/iftest")public String iftest(WebRequest webRequest){// 保存數(shù)據(jù)到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";}/** 保存數(shù)據(jù)到作用范圍域,用于測試Thymeleaf的循環(huán)獲取數(shù)據(jù)* */@RequestMapping("/eachtest")public String eachtest(WebRequest webRequest){// 模擬數(shù)據(jù)庫數(shù)據(jù)保存到List集合List<Book> books = new ArrayList<>();// 下面將對(duì)象初始化以后加入列表books.add(new Book(1, "瘋狂Java講義-補(bǔ)充", "java.jpg", "李剛 編著", 109.00));books.add(new Book(2, "輕量級(jí)Java EE企業(yè)應(yīng)用實(shí)戰(zhàn)", "ee.jpg", "李剛 編著", 108.00));books.add(new Book(3, "Spring+MyBatis應(yīng)用實(shí)戰(zhàn)", "SpringMyBatis.jpg", "瘋狂軟件 編著", 58.00));books.add(new Book(4, "瘋狂Android講義", "android.jpg", "李剛 編著", 108.00));books.add(new Book(5, "瘋狂Ajax開發(fā)", "ajax.jpg", "李剛 編著", 79.00));// 保存數(shù)據(jù)到request作用范圍域webRequest.setAttribute("books", books, RequestAttributes.SCOPE_REQUEST);/*這里的數(shù)據(jù)是通過thymeleaf傳遞到網(wǎng)頁上*/ // 這里“books”是與html頁面對(duì)應(yīng)的變量名 // 上面的webRequest是用來設(shè)置傳遞給html的變量的return "success3";}}關(guān)于上述三個(gè)java的thymeleaf用法的具體區(qū)別,也可以參考[1]
?
?
Springboot中的容器到底是什么意思呢?
我們想必聽過docker,也就是一個(gè)系統(tǒng)里面再做一個(gè)系統(tǒng),
所以Spring boot中的容器概念其實(shí)類似于Javaweb版本的docker.
?
Reference:
[1]Difference of 3 xxx.setAttribute()
總結(jié)
以上是生活随笔為你收集整理的springboot中三种xxxx.setAttribute()并与python中flask作对应比较+容器的通俗理解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4K 分辨率 899 元到手:联合创新
- 下一篇: iFi 悦尔法推出新款 GO pod 解