springmvc中注解@ModelAttribute的妙用分享

前言

成都創(chuàng)新互聯(lián)公司主營老邊網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app軟件開發(fā),老邊h5小程序定制開發(fā)搭建,老邊網(wǎng)站營銷推廣歡迎老邊等地區(qū)企業(yè)咨詢

本文主要給大家介紹了關(guān)于spring mvc注解@ModelAttribute妙用的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細的介紹吧。

在Spring mvc中,注解@ModelAttribute是一個非常常用的注解,其功能主要在兩方面:

  1. 運用在參數(shù)上,會將客戶端傳遞過來的參數(shù)按名稱注入到指定對象中,并且會將這個對象自動加入ModelMap中,便于View層使用;
  2. 運用在方法上,會在每一個@RequestMapping標(biāo)注的方法前執(zhí)行,如果有返回值,則自動將該返回值加入到ModelMap中;

一般開發(fā)中,第一種用法居多,本次我將使用第二種用法以期節(jié)省controller層的一些代碼:

目前使用spring mvc開發(fā)的controller層方法一般類似于:

 @RequestMapping("/{encodeId}/detail")
 public String detail(ModelMap model, @PathVariable String encodeId) {
 .....
 }

幾乎在每一個@RequestMapping標(biāo)注的方法的參數(shù)中都會有 ModelMap model的參數(shù),既然這是一個大概率事件,為什么不可以像注入request那樣,直接在類的開始使用@Resource進行自動注入呢?

另外一個,就是response,response也不能像request那樣進行自動注入。

類似的可能還有很多,既然這些都是controller層常用的代碼,如果能將其在一個basecontroller層自動注入,然后controller層繼承這個basecontroller,那樣就沒有必要再@RequestMapping標(biāo)注的方法中寫上這些參數(shù),使得參數(shù)個數(shù)減少,清晰。

我的思路正是使用@ModelAttribute注解,編寫一個basecontroller類,預(yù)定義一些項目中controller層常用的對象,如下:

 @Resource
 protected HttpServletRequest request;
 
 protected ModelMap model;
 
 protected HttpServletResponse response;

request不用解釋,可以直接使用@Resource直接注入,response和model的注入方式如下:

/**
 * 設(shè)置response
 * 
 * @param response
 */
 @ModelAttribute
 private final void initResponse(HttpServletResponse response) {
 this.response = response;
 }

 /**
 * 設(shè)置model
 * 
 * @param model
 */
 @ModelAttribute
 private final void initModelMap(ModelMap model) {
 this.model = model;
 }

spring在執(zhí)行@RequestMapping前會執(zhí)行上述方法,spring會和平常一樣,每次請求重新生成一個model和response,然后注入到方法的參數(shù)中,這樣就變相在繼承了這個basecontroller的controller中自動注入了response和model,在這個controller層中再也不必每次寫ModelMap和response參數(shù),整體代碼整潔了不少。

我在項目中這樣使用暫無問題,如果哪位高手知道這種做法會有弊端或者有更好的方法,求指正!

修正:

非常感謝eBusinessMan的提醒,確實有可能在spring mvc單例模式下會出現(xiàn)訪問對象不一致的情況,為了防止該問題,而又能保持這種代碼的簡潔性以及確保使用spring mvc性能問題不太嚴重,我決定使用ThreadLocal來處理。

(驗證結(jié)果:request采用spring的自動注入方式是線程安全的,response、model是不安全的,采用ThreadLocal可以解決該問題)

request對象不再使用注解自動注入(也可以繼續(xù)使用注解方式注入),而使用同response和model初始化的方式,取消request、response、model三個類變量,具體如下:

private static final ThreadLocal<HttpServletRequest> requestContainer = new ThreadLocal<HttpServletRequest>();

  private static final ThreadLocal<HttpServletResponse> responseContainer = new ThreadLocal<HttpServletResponse>();

  private static final ThreadLocal<ModelMap> modelContainer = new ThreadLocal<ModelMap>();

 /**
 * 初始化response
 * 
 * @param response
 */
 @ModelAttribute
 private final void initResponse(HttpServletResponse response) {
 responseContainer.set(response);
 }

 /**
 * 獲取當(dāng)前線程的response對象
 * 
 * @return
 */
 protected final HttpServletResponse getResponse() {
 return responseContainer.get();
 }

 /**
 * 初始化request
 * 
 * @param request
 */
 @ModelAttribute
 private final void initRequest(HttpServletRequest request) {
 requestContainer.set(request);
 }

 /**
 * 獲取當(dāng)前線程的request對象
 * 
 * @return
 */
 protected final HttpServletRequest getRequest() {
 return requestContainer.get();
 }

 /**
 * 設(shè)置model
 * 
 * @param model
 */
 @ModelAttribute
 private final void initModelMap(ModelMap model) {
 modelContainer.set(model);
 }

 /**
 * 獲取當(dāng)前線程的modelMap對象
 * 
 * @return
 */
 protected final ModelMap getModelMap() {
 return modelContainer.get();
 }

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。

當(dāng)前名稱:springmvc中注解@ModelAttribute的妙用分享
文章網(wǎng)址:http://www.muchs.cn/article10/ghjjdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣網(wǎng)站導(dǎo)航、App開發(fā)、用戶體驗定制開發(fā)、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)