SpringMVC中的参数组装:HandlerMethodArgumentResolver

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>




评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值