Spring MVC的參數(shù)綁定和返回值問題
參數(shù)綁定過程
在springMVC中,從前端(頁面:jsp…)發(fā)送請求到后端(controller…),會包含一些數(shù)據(jù),數(shù)據(jù)是如何到達Controller,這個過程就是參數(shù)綁定過程
1、默認支持的類型
SpringMVC有支持的默認的參數(shù)類型,在方法上給出默認的參數(shù)類型的聲明就可以直接使用
HttpServletRequest request:通過request對象來獲取請求的信息 HttpServletResponse response:通過response來處理響應信息 HttpSession session:通過session對象來存放緩存信息 Model model:Model是一個接口,modelMap是一個接口實現(xiàn),將model信息填充到request中public String index(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) { //返回的modelAndView //指定返回的頁面
2、基本數(shù)據(jù)類型
基本的數(shù)據(jù)類型也支持綁定,基礎的數(shù)據(jù)類型包括byte、short、int、long 、float、double、char、boolean
JSP頁面:
<html><head> <title>測試基本數(shù)據(jù)類型</title></head><body><!-- 測試基本的數(shù)據(jù)類型綁定:form表單向后端提交數(shù)據(jù) --><form action='/index/basedataType' method='post'> <input type='text' name='username'> <input type='submit' value='提交'></form></body></html>
controller層:
@RequestMapping('/basedataType')public void baseDataType(int username) { System.out.println('基本數(shù)據(jù)類型:'+username);} 注意:表單上Input中的name值和controller的參數(shù)的變量名保持一致,則能完成數(shù)據(jù)綁定,如果不一致呢? 需要@RequestParam注解來完成,JSP頁面不用改變
使用@RequestParam注解就可以解決頁面Input的name值和controller方法形參名不一致的問題
注意:基本的數(shù)據(jù)類型和包裝類型(Integer,Long…)以及String類型參數(shù)綁定都是適用于以上基本類型參數(shù)綁定,基本類型和包裝類型的區(qū)別:基本類型傳遞參數(shù)不能為null或者“”,否則會出現(xiàn)數(shù)據(jù)轉(zhuǎn)化的異常,包裝類型不會出現(xiàn)這種問題,建議使用時使用包裝。
3、自定義類型
自定義類型的類(User)
public class User { private Long id; private String name; private String address;}//getter setter toString
JSP頁面
<%@ page language='java' contentType='text/html; charset=UTF-8' pageEncoding='UTF-8' %> <%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c' %> <%@ taglib uri='http://java.sun.com/jsp/jstl/fmt' prefix='fmt' %> <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'> <html> <head> <title>自定義類型數(shù)據(jù)提交</title> </head> <body> <form action='/user/adduser' method='post'> 用戶id:<input type='text' name='id'><br/> 用戶名:<input type='text' name='name'><br/> 地址:<input type='text' name='address'><br/> <input type='submit' value='提交'> </form> </body> </html>
controller層
@Controller@RequestMapping('/user')public class UserController { @RequestMapping('/index') public String index() { return 'user'; } @RequestMapping('/adduser') //接收自定義類型的數(shù)據(jù) public String addUser(User user){ System.out.println('用戶信息:'+user); return 'success'; }}
注意:頁面輸入框的name屬性值和自定義的pojo實例的屬性名保持一致即可映射成功如果類型存在 不一致時需要自定義一個類型轉(zhuǎn)化器,需要給定一個自定義的類,實現(xiàn)Converter接口,該接口需要執(zhí)行轉(zhuǎn)化的類型,例如將前端的String的數(shù)據(jù)轉(zhuǎn)化為日期類型,即Converter<String,Date>
4、集合類型
集合類型:數(shù)組,List、Map等常用的集合類型都會支持以List集合為例進行介紹
JSP頁面
<%@ page language='java' contentType='text/html; charset=UTF-8' pageEncoding='UTF-8'%><%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c' %><%@ taglib uri='http://java.sun.com/jsp/jstl/fmt' prefix='fmt'%><!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'><html><head> <title>用戶列表</title></head><body><table border='1'> <thead> <tr> <td>用戶id</td> <td>用戶名</td> <td>地址</td> </tr> </thead> <tbody> <c:forEach items='${users}' var='user'> <tr> <td>${user.id}</td> <td>${user.name}</td> <td>${user.address}</td> </tr> </c:forEach> </tbody></table></body></html>
controller層
@RequestMapping('/userlist')//集合類型數(shù)據(jù)的傳遞public ModelAndView userList() { ModelAndView modelAndView = new ModelAndView(); //指定邏輯視圖名 modelAndView.setViewName('userlist'); //偽數(shù)據(jù)集合 ArrayList<User> users = new ArrayList <>(); User user1 = new User(); user1.setId(1L); user1.setName('張三'); user1.setAddress('陜西西安'); users.add(user1); User user2 = new User(); user2.setId(2L); user2.setName('李四'); user2.setAddress('陜西西安'); users.add(user2); User user3 = new User(); user3.setId(3L); user3.setName('王五'); user3.setAddress('陜西西安'); users.add(user3); //填充數(shù)據(jù) modelAndView.addObject('users', users); return modelAndView;}
1、返回ModelAndView類型
返回結構定位ModelAndView時,將model和View分別進行設置
@RequestMapping('/userlist') //集合類型數(shù)據(jù)的傳遞 public ModelAndView userList() { //偽數(shù)據(jù)集合 ArrayList <User> users = new ArrayList <>(); ModelAndView modelAndView = new ModelAndView(); //指定邏輯視圖名 modelAndView.setViewName('userlist'); //返回數(shù)據(jù) modelAndView.addObject('users', users); modelAndView.addObject('class', '超大充電寶'); return modelAndView; }
2、返回String類型 (1)返回邏輯視圖名
返回String,可以表示是邏輯視圖名
真正視圖(jsp路徑)=“前綴”+邏輯視圖名+“后綴”前綴和后綴可以在spring-mvc中設置視圖解析器組件時指定
(2)redirect重定向
redirect特點:
瀏覽器上地址URL會發(fā)生改變, 修改后的request的數(shù)據(jù)無法傳遞到重定向的頁面,即重定向時request數(shù)據(jù)無法共享點擊提交之后,頁面跳轉(zhuǎn)到redirect指定的頁面,URL會發(fā)生改變
(3)forward頁面轉(zhuǎn)發(fā)
forward特點:
瀏覽器的地址URL不變的 request是可以共享的點擊提交之后,頁面跳轉(zhuǎn)到forward指定的頁面,URL不會發(fā)生改變
問題:forward和redirect的區(qū)別?
Forward和Redirect代表了兩種請求轉(zhuǎn)發(fā)方式:直接轉(zhuǎn)發(fā)和間接轉(zhuǎn)發(fā)。
直接轉(zhuǎn)發(fā)方式(Forward),客戶端和瀏覽器只發(fā)出一次請求,Servlet、HTML、JSP或其它信息資源,由第二個信息資源響應該請求,在請求對象request中,保存的對象對于每個信息資源是共享的。 間接轉(zhuǎn)發(fā)方式(Redirect)實際是兩次HTTP請求,服務器端在響應第一次請求的時候,讓瀏覽器再向另外一個URL發(fā)出請求,從而達到轉(zhuǎn)發(fā)的目的。舉個通俗的例子:
直接轉(zhuǎn)發(fā)就相當于:“A找B借錢,B說沒有,B去找C借,借到借不到都會把消息傳遞給A” 間接轉(zhuǎn)發(fā)就相當于:“A找B借錢,B說沒有,讓A去找C借”二者區(qū)別:redirect 重定向:
1、重定向會改變?yōu)g覽器地址欄地址 。
2、重定向不止可以訪問服務器內(nèi)的資源,還可以訪問外部連接 。
3、重定向因為是兩次請求,所以重定向不能使用request來訪問資源和共享數(shù)據(jù),因為request的作用域是一次請求內(nèi)。
forward 請求轉(zhuǎn)發(fā):
1、不會改變?yōu)g覽器地址欄信息 。
2、只能訪問服務器內(nèi)部資源 。
3、是一次請求.所以可以使用request共享數(shù)據(jù)。
2、返回void類型
@RequestMapping('/void') //接收自定義類型的數(shù)據(jù) public void result(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //添加用戶成功跳轉(zhuǎn)到用戶列表 request.getRequestDispatcher('/user/userlist').forward(request,response); response.sendRedirect('/user/userlist'); response.getWriter().write('hello...'); }
(1)使用request頁面轉(zhuǎn)向
request.getRequestDispatcher('/user/userlist').forward(request,response);
(2)通過response頁面重定向
response.sendRedirect('/user/userlist');
(3)通過response指定響應結果
response.getWriter().write('hello...');
到此這篇關于Spring MVC的參數(shù)綁定和返回值的文章就介紹到這了,更多相關Spring MVC參數(shù)綁定和返回值內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. Python importlib動態(tài)導入模塊實現(xiàn)代碼3. android studio 打包自動生成版本號與日期,apk輸入路徑詳解4. 利用promise及參數(shù)解構封裝ajax請求的方法5. 淺談python出錯時traceback的解讀6. 在Android中使用WebSocket實現(xiàn)消息通信的方法詳解7. .NET中l(wèi)ambda表達式合并問題及解決方法8. Nginx+php配置文件及原理解析9. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解10. JSP數(shù)據(jù)交互實現(xiàn)過程解析
