Spring mvc Model新增屬性

2021-09-02 16:35:03 字數 895 閱讀 6558

為了讓模板引擎知道這些變數的值,我們需要在@controller做一些工作:

import org.springframework.ui.model;

public string index(@pathvariable("id") long id, model model) {

// 這裡我們模擬一些資料

model.addattribute("title", "this is a blog with id = " + id);

model.addattribute("createdtime", "2015-08-11");

model.addattribute("content", "this is content");

return "index";

在上面的**中,index()方法增加了乙個model型別的引數。通過spring mvc框架提供的model,可以呼叫其addattribute方法,這樣thymeleaf可以訪問model中的變數從而進行模板渲染。上述例子中可以看到,title變數的值是根據url中的@pathvariable來確定的,雖然簡單,但是這已經是乙個動態頁面了。

在servlet程式設計中,如果希望在頁面中動態渲染資訊,一般需要往httprequest中新增屬性,然後在jsp中獲取。其實model的屬性實際上也是放在httprequest的屬性中,但是spring mvc提供了更高層的抽象,幫你遮蔽了httprequest,你看到的只有直接以mvc中m(即model)。

如果你依然希望希望httprequest、httpresponse和httpsession等原生的servlet api物件,往controller方法中增加對應型別的引數即可,你在方法中就能直接使用了,spring mvc會傳遞給你正確的物件,

android LinearLayout新增分隔線

linearlayout支援直接顯示分隔線。設定標籤的 android showdividers屬性可以再linearlayout的相應位置顯示分隔線。如果有多個linearlayout,顯示效果和在 linearlayout之間加分隔線是一樣的。android showdividers屬性可以設定...

prometheus grafana監控新增新節點

已部署好prometheus grafana,並能正常執行 在client端先部署好已經編寫好的node exporter 編寫指令碼並執行 bin bash wget http ip node.tar.gz 獲取編寫好的node tar zxvf node.tar.gz c usr local 解...

FZYZOJ 2148 小添添的灰機貓

時間限制 1000ms 記憶體限制 131072kb 成功從wtm晉公升為acm的小添添越來越sxbk了,開始將魔爪伸向了自己的灰h機j貓。事情是這樣的,小添添有k只貓,分別是0 k 1。他先用rand k隨機出了n個數,告訴你他需要臨幸的灰機貓的編號,如果乙隻貓多次出現,也只算一次。因為小添添一開...