您好,登錄后才能下訂單哦!
前面已經(jīng)說(shuō)了,spring mvc可以通過(guò)@PathVariable可以把url中的值映射到目標(biāo)方法的參數(shù)上,也可以使用@RequestParam進(jìn)行單個(gè)參數(shù)的傳遞,如果一次需要傳遞多個(gè)參數(shù)的話,就需要寫多個(gè)相應(yīng)的注解,那有沒(méi)有什么更簡(jiǎn)單方便的方法呢?答案是有的,spring mvc有自動(dòng)裝箱的功能,可以通過(guò)參數(shù)的name屬性,給后臺(tái)綁定的對(duì)象的對(duì)應(yīng)的屬性完成賦值操作,而且還支持級(jí)聯(lián)操作,即支持嵌套。
1、使用POJO(Plain Old Java Object)進(jìn)行參數(shù)傳值
下面來(lái)看一下具體例子,定義一個(gè)User對(duì)象,一個(gè)Address對(duì)象:
public class User { private String userName; private String password; private String email; private int age; private Address address; //省略get和set方法以及toString方法 …… }
public class Address { private String province; private String city; //省略get和set方法以及toString方法 }
Controller的定義如下:
/** * spring mvc可以進(jìn)行自動(dòng)"裝箱" ,會(huì)按照請(qǐng)求參數(shù)的name完成和pojo屬性之間的映射綁定, * 自動(dòng)為該對(duì)象填充屬性值,并且支持級(jí)聯(lián)屬性 * * @param user * @return */ @RequestMapping("/testPojo") public String testPojo(User user){ System.out.println(user); return "greeting"; }
測(cè)試代碼如下,有User對(duì)象的屬性,有級(jí)聯(lián)屬性Address:
<form action="/testPojo/testPojo" method="post"> <p>pojo測(cè)試,可以直接傳遞參數(shù)綁定到pojo對(duì)象上</p> userName:<input type="text" name="userName"/> <br/> password:<input type="text" name="password"/> <br/> email:<input type="text" name="email"/> <br/> age:<input type="text" name="age"/> <br/> province:<input type="text" name="address.province"/> <br/> city:<input type="text" name="address.city"/> <br/> <input type="submit" value="submit" /> </form>
2、使用Servlet原生API進(jìn)行參數(shù)傳值
spring mvc支持使用ServletRequest和ServletResponse等作為目標(biāo)方法的參數(shù)。spring mvc支持的類型有以下幾種ServletRequest、ServletResponse、HttpSession、Pricipal、Locale、InputStream、OutputStream、Reader、Writer,具體可以參見(jiàn)源碼:
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
@Override protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest) throws Exception { HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); if (ServletRequest.class.isAssignableFrom(parameterType) || MultipartRequest.class.isAssignableFrom(parameterType)) { Object nativeRequest = webRequest.getNativeRequest(parameterType); if (nativeRequest == null) { throw new IllegalStateException( "Current request is not of type [" + parameterType.getName() + "]: " + request); } return nativeRequest; } else if (ServletResponse.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; Object nativeResponse = webRequest.getNativeResponse(parameterType); if (nativeResponse == null) { throw new IllegalStateException( "Current response is not of type [" + parameterType.getName() + "]: " + response); } return nativeResponse; } else if (HttpSession.class.isAssignableFrom(parameterType)) { return request.getSession(); } else if (Principal.class.isAssignableFrom(parameterType)) { return request.getUserPrincipal(); } else if (Locale.class == parameterType) { return RequestContextUtils.getLocale(request); } else if (InputStream.class.isAssignableFrom(parameterType)) { return request.getInputStream(); } else if (Reader.class.isAssignableFrom(parameterType)) { return request.getReader(); } else if (OutputStream.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; return response.getOutputStream(); } else if (Writer.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; return response.getWriter(); } return super.resolveStandardArgument(parameterType, webRequest); }
項(xiàng)目源代碼:
https://git.oschina.net/acesdream/spring-mvc
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。