springmvc元件開發

2021-10-25 09:54:35 字數 2451 閱讀 4753

現在流行的restful風格,往往會將引數寫入到請求路徑中去,有些需要傳遞json資料,比如查詢分頁,使用者資訊比較多的時候,需要將查詢引數組裝成乙個json資料集。

接收普通請求引數:

("/commonparams.do"

)public

void

commonparams

(@requestparam

("account_name"

) string name,string money)

通過引數名稱和http請求引數的名稱保持一致來獲取引數,如果不一致就沒法獲取到,這樣的方式允許引數為空。不一致時可以通過,@requestparam(「account_name」)來對映;

通過實體類pojo獲取對應引數時,也要和http請求引數名稱保持一致。@requestbody (

"/success/.do"

)public modelandview index

(@pathvariable

("id"

) long id)

@pathvariable獲取url中傳遞的引數值

("/insertrole"

)public string addrole

(model model,

@requestbody role role)

通過modelandview實現重定向

("/addrole2"

)public modelandview addrole2

(modelandview mv,

@requestbody role role) (

"/addrole3"

)public string addrole2

(redirectattributes ra,

@requestbody role role)

需求:有些時候我們需要將資料暫存到http的request物件或者session物件中,或者從他們中獲取資料。

springmvc提供了三個註解來給與支援:

jsp頁面

<

%//設定請求屬性

request.

setattribute

("id"

,1l)

;//**給控制器

request.

getrequestdispatcher

(./attribute/requestattribute.do)

.forward

(request,response);%

>

controller層

("/requestattribute.do"

)public modelandview addrole2

(@requestattribute

("id"

) long id)

jsp頁面

<

%//設定session屬性

session.

setattribute

("id"

,1l)

;//**給控制器

response.

sendredirect

(./attribute/sessionattribute.do)

;%>

controller層

("/requestattribute.do"

)public modelandview addrole2

(@sessionattribute

("id"

) long id)

spring要求處理器的***都要實現介面org.springframework.web.servlet.handlerinterceptor介面,handlerinterceptor介面有三個方法:

<

!--自定義了多個spingmvc***--

>

"/hello/findaccountbyid.do"

/>

="com.example.interceptor.hellointerceptor"

/>

<

/mvc:interceptor>

"/hello/findaccountbyid.do"

/>

="com.example.interceptor.hellointerceptor1"

/>

<

/mvc:interceptor>

"/hello/findaccountbyid.do"

/>

="com.example.interceptor.hellointerceptor2"

/>

<

/mvc:interceptor>

<

/mvc:interceptors>

SpringMVC元件解析

前端控制器 dispatcherservlet 使用者請求到達前端控制器,它就相當於 mvc 模式中的 c,dispatcherservlet 是整個流程控制的中心,由 它呼叫其它元件處理使用者的請求,dispatcherservlet 的存在降低了元件之間的耦合性。對映方式,例如 配置檔案方式,實...

SpringMVC元件解析

springmvc元件解析 1.前端控制器 dispatcherservlet 通過 handleradapter 對處理器進行執行,這是介面卡模式的應用,通過擴充套件介面卡可以對更多型別的處理器進行執行。4.處理器 handler 它就是我們開發中要編寫的具體業務控制器。由 dispatchers...

學習筆記 springMVC元件

1 前端控制器dispatcherservlet 不需要程式設計師開發 作用 接收請求,響應結果,相當於 器,處理器。有了dispatcherservlet減少了其它元件之間的耦合度。作用 根據請求的url查詢handler 3 處理器介面卡handleradapter 不需要程式設計師開發 作用 ...