Spring註解大全(三)

2021-10-18 10:49:19 字數 1124 閱讀 1594

目錄:

所有方法在呼叫前,先執行此@modelattribute方法.

a.標註在有返回值的方法上

當modelattribute設定了value,方法返回的值會以這個value為key,以引數接受到的值作為這個key對應的value,組成k-v鍵值對,存入到model中,如下面的方法執行之後,最終相當於 model.addattribute(「user_name」, name);假如 @modelattribute沒有自定義value,則相當於

model.addattribute(「name」, name);

往前台傳資料,可以傳物件,可以傳list,通過el表示式 ${}可以獲取到,

類似於request.setattribute("sts",sts)效果一樣。

@modelattribute(value="user_name")

public string before2(@requestparam(required = false) string name, model model)

b.標註在沒有返回值的方法上

需要手動model.add方法

@modelattribute

public void before(@requestparam(required = false) integer age, model model)

c.標註在方法的引數上

會將客戶端傳遞過來的引數按名稱注入到指定物件中,並且會將這個物件自動加入modelmap中,便於view層使用.

public resp mod2(@modelattribute("user_name") string user_name, 

@modelattribute("name") string name,

@modelattribute("age") integer age,model model)

Spring註解大全

spring4之後加入的註解,原來在 controller中返回json需要 responsebody來配合,如果直接用 restcontroller替代 controller就不需要再配置 responsebody,預設返回json格式。crossorigin restcontroller pub...

Spring註解大全(二)

目錄 定義切點,切點表示式 execution 許可權訪問符 返回值型別 方法所屬的類名包路徑.方法名 形參型別 異常型別 aspect component public class afterthrowingaspect 前置增強,配合 pointcut一起使用 aspect component ...

三 Spring 註解注入

目錄 一 component 二 3個 component註解衍生註解 三 autowired 自動根據型別注入 四 scope prototype 多例 單例的配置 預設singleton單例 首先簡單說明一下什麼是註解注入,回顧之前我們的案例,不管是裝配物件 bean 還是注入物件 bean 都...