SpringMVC中的@requestMapping問題與controller方法的傳回值問題

來源:互聯網
上載者:User

標籤:post   url   表示   mvc   obj   content   char   形參   str   

@requestMapping

1、url映射

  定義controller方法對應的url,進行處理器映射使用

  @RequestMapping("/items")

  public class ItemsController {...}

2、窄化請求映射

3、限制http要求方法

出於安全性考慮,對http的連結進行方法限制。

如果限制請求為post方法,進行get請求,報錯:

但是因為method={}是數組,所以可以把POST與GET方法都寫進去

 

controller方法的傳回值

1、返回ModelAndView

  需要方法結束時,定義ModelAndVIew,將Model 和View分別進行設定:

modelAndView.addObject("item", itemsCustom);

modelAndView.setViewName("editItem");

 

2、返回string

  若controller方法返回string

a.表示返回邏輯視圖名。(所謂邏輯視圖名即:在springMMVC裡配置了首碼和尾碼,所以具體路徑簡化為去掉首碼和尾碼的路徑)

真正視圖(jsp路徑)=首碼+邏輯視圖名+尾碼

 

b、redirect重新導向

 商品修改提交後,重新導向到商品查詢列表。

 redirect重新導向特點:瀏覽器地址欄中的url會變化。修改提交的request資料無法傳到重新導向的地址。因為重新導向後重新進行request(request無法共用)

 

 

c、forward頁面轉寄

通過forward進行頁面轉寄,瀏覽器地址欄url不變,request可以共用。

 

3、返回void

在controller方法形參上可以定義request和response,使用request或response指定響應結果:

a、使用request轉向頁面,如下:

request.getRequestDispatcher("頁面路徑").forward(request, response);

 

b、也可以通過response頁面重新導向:

response.sendRedirect("url")

 

c、也可以通過response指定響應結果,例如響應json資料如下:

response.setCharacterEncoding("utf-8");

response.setContentType("application/json;charset=utf-8");

response.getWriter().write("json串");

 

 

 

SpringMVC中的@requestMapping問題與controller方法的傳回值問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.