前面已经说了,spring mvc可以通过@PathVariable可以把url中的值映射到目标方法的参数上,也可以使用@RequestParam进行单个参数的传递,如果一次需要传递多个参数的话,就需要写多个相应的注解,那有没有什么更简单方便的方法呢?答案是有的,spring mvc有自动装箱的功能,可以通过参数的name属性,给后台绑定的对象的对应的属性完成赋值操作,而且还支持级联操作,即支持嵌套。
1、使用POJO(Plain Old Java Object)进行参数传值
下面来看一下具体例子,定义一个User对象,一个Address对象:
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可以进行自动"装箱" ,会按照请求参数的name完成和pojo属性之间的映射绑定,
* 自动为该对象填充属性值,并且支持级联属性
*
* @param user
* @return
*/
@RequestMapping("/testPojo")
public String testPojo(User user){
System.out.println(user);
return "greeting";
}
测试代码如下,有User对象的属性,有级联属性Address:
<form action="/testPojo/testPojo" method="post">
<p>pojo测试,可以直接传递参数绑定到pojo对象上</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进行参数传值
spring mvc支持使用ServletRequest和ServletResponse等作为目标方法的参数。spring mvc支持的类型有以下几种ServletRequest、ServletResponse、HttpSession、Pricipal、Locale、InputStream、OutputStream、Reader、Writer,具体可以参见源码:
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);
}
项目源代码:
https://git.oschina.net/acesdream/spring-mvc