二 @requestheader、@cookievalue
@requestheader 註解,可以把request請求header部分的值繫結到方法的引數上。
示例**:
這是乙個request 的header部分
01.host localhost:8080
03.accept-language fr,en-gb;q=0.7,en;q=0.3
04.accept-encoding gzip,deflate
05.accept-charset iso-8859-1,utf-8;q=0.7,*;q=0.7
06.keep-alive 300
public void list(@requestheader("accept-encoding") string encoding,
@requestheader("keep-alive") long keepalive)
上面的**,把request header部分的 accept-encoding的值,繫結到引數encoding上了, keep-alive header的值繫結到引數keepalive上。
@cookievalue 可以把request header中關於cookie的值繫結到方法的引數上。
例如有如下cookie值
public void listheaderinfo(@cookievalue("jsessionid") string cookie)
三 @sessionattributes, @modelattribute
@sessionattributes:
該註解用來繫結httpsession中的attribute物件的值,便於在方法中的引數裡使用。
該註解有value、types兩個屬性,可以通過名字和型別指定要使用的attribute 物件
@sessionattributes("pet")
public class edituserform
@modelattribute
該註解有兩個用法,乙個是用於方法上,乙個是用於引數上;
a) @sessionattributes 啟用的attribute 物件上;
b) @modelattribute 用於方法上時指定的model物件;
c) 上述兩種情況都沒有時,new乙個需要繫結的bean物件,然後把request中按名稱對應的方式把值繫結到bean中。
// you can customize the name via @modelattribute("myaccount")
@modelattribute
public account addaccount(@requestparam string number)
public string processsubmit(@modelattribute pet pet) {
首先查詢 @sessionattributes有無繫結的pet物件,若沒有則查詢@modelattribute方法層面上是否繫結了pet物件,若沒有則將uri template中的值按對應的名稱繫結到pet物件的各屬性上。
自己建立註解,和使用註解
首先自己建立註解的類 mytest retention value retentionpolicy.runtime 在執行的時候擦除 retention表示執行時候的範圍,一共有3種 target value 這個表明是要用到哪個上 public inte ce mytest 自己在建立乙個,然後使...
spring 註解使用
一 配置web.xml 1 問題 spring專案中有多個配置檔案mvc.xml dao.xml 2 解決 在web.xml中 contextconfiglocation web inf xml 表示xml檔案路徑 xml表示字尾名為xml的任意檔案 3 可以使用 逗號分隔 二 controller...
註解切面使用
通過自定義spring註解方式,給指定的service業務類新增註解,用於日誌記錄等操作。自定義註解 攔截controller target retention retentionpolicy.runtime documented public inte ce systemcontrollerlog...