SpringMVC3.1引入了HandlerMethodArgumentResolver接口,Spring调用该接口实现Controller的参数装配。HandlerMethodArgumentResolver实现类中会调用DataBinder,Converter等。
常用的该接口实现类有:
ServletModelAttributeMethodProcessor:实体类的组装用它实现。
RequestParamMethodArgumentResolver:基本数据类型如String用它实现。
在我学习过程中,发现对List类型的参数SpringMVC没有提供默认实现。我参照Spring的示例 通过实现 HandlerMethodArgumentResolver接口,实现对List参数的组装。
示例中用到的Model类:
package com.weishubin.springmvc.model;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(name);
sb.append("-");
sb.append(age);
return sb.toString();
}
}
注解类:
package com.weishubin.springmvc.listargument;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ListAttribute {
}
实现了HandlerMethodArgumentResolver接口的参数解析器:
package com.weishubin.springmvc.listargument;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import com.weishubin.springmvc.model.User;
public class ListArgumentResolver implements HandlerMethodArgumentResolver {
public boolean supportsParameter(MethodParameter parameter) {
//仅作用于添加了注解ListAttribute的参数
return parameter.getParameterAnnotation(ListAttribute.class) != null;
}
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
List<User> users = new ArrayList<User>();
String[] names = webRequest.getParameterValues("name");
String[] ages = webRequest.getParameterValues("age");
for (int i = 0; i < names.length; i++) {
User user = new User();
BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(user);
String name = names[i];
String age = ages[i];
beanWrapper.setPropertyValue("name", name);
beanWrapper.setPropertyValue("age", age);
users.add(user);
}
return users;
}
}
Controller类:
package com.weishubin.springmvc.listargument;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.weishubin.springmvc.model.User;
@Controller
public class ListArgumentController {
@ResponseBody
@RequestMapping("/list")
public String argumentResolver(@ListAttribute List<User> list) {
StringBuilder b = new StringBuilder();
for (User u : list) {
b.append(u);
b.append("<br/>");
}
return b.toString();
}
}
配置文件:
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="com.weishubin.springmvc.listargument.ListArgumentResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>