springmvc 源码初探01

今天遇到了个奇怪的问题,使用springmvc从前端往后端传参数的时候,参数死活传不进去。

后来看了一下springmvc参数封装的过程,恍然大悟。

springmvc参数封装的过程中有个类是这样的。

org.springframework.web.bind.WebDataBinder

他有个方法

/**
	 * Check the given property values for field defaults,
	 * i.e. for fields that start with the field default prefix.
	 * <p>The existence of a field defaults indicates that the specified
	 * value should be used if the field is otherwise not present.
	 * @param mpvs the property values to be bound (can be modified)
	 * @see #getFieldDefaultPrefix
	 */
	protected void checkFieldDefaults(MutablePropertyValues mpvs) {
		if (getFieldDefaultPrefix() != null) {
			String fieldDefaultPrefix = getFieldDefaultPrefix();//关键1
			PropertyValue[] pvArray = mpvs.getPropertyValues();
			for (PropertyValue pv : pvArray) {
				if (pv.getName().startsWith(fieldDefaultPrefix)) {//关键2
					String field = pv.getName().substring(fieldDefaultPrefix.length());
					if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
						mpvs.add(field, pv.getValue());
					}
					mpvs.removePropertyValue(pv);//关键3
				}
			}
		}
	}

这个方法,会找属性是下划线开头的并将他移除。我的属性名正好是“_id” 所以一直封装不进去。

猜你喜欢

转载自my.oschina.net/u/1178126/blog/1787584