標籤: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方法的傳回值問題