日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

springboot中三种xxxx.setAttribute()并与python中flask作对应比较+容器的通俗理解

發(fā)布時間:2023/12/31 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot中三种xxxx.setAttribute()并与python中flask作对应比较+容器的通俗理解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

對應(yīng)關(guān)系如下:

Javapython
Spring bootFlask

import javax.servlet.http.HttpServletRequest;
HttpServletRequest.getServletContext().setAttribute("name", "Thymeleaf模板引擎");

from flask import g
import javax.servlet.http.HttpSession;
HttpSession.setAttribute()
from flask import session

import org.springframework.web.context.request.WebRequest;


webRequest.setAttribute("books", books, RequestAttributes.SCOPE_REQUEST);

return {'returnCode': 0, 'returnValue': returnValue}
ThymeleafJinja2

?

完整代碼舉例如下:

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"));//接受頁面請求中的參數(shù)(這里其實是視圖函數(shù)) //接收參數(shù)后直接打印,其實這里應(yīng)該擴(kuò)展為去數(shù)據(jù)庫進(jì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<>();// 下面將對象初始化以后加入列表books.add(new Book(1, "瘋狂Java講義-補(bǔ)充", "java.jpg", "李剛 編著", 109.00));books.add(new Book(2, "輕量級Java EE企業(yè)應(yīng)用實戰(zhàn)", "ee.jpg", "李剛 編著", 108.00));books.add(new Book(3, "Spring+MyBatis應(yīng)用實戰(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頁面對應(yīng)的變量名 // 上面的webRequest是用來設(shè)置傳遞給html的變量的return "success3";}}

關(guān)于上述三個java的thymeleaf用法的具體區(qū)別,也可以參考[1]

?

?

Springboot中的容器到底是什么意思呢?

我們想必聽過docker,也就是一個系統(tǒng)里面再做一個系統(tǒng),

所以Spring boot中的容器概念其實類似于Javaweb版本的docker.

?

Reference:

[1]Difference of 3 xxx.setAttribute()

總結(jié)

以上是生活随笔為你收集整理的springboot中三种xxxx.setAttribute()并与python中flask作对应比较+容器的通俗理解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。