今天遇到了個奇怪的問題,使用springmvc從前端日後端傳參數的時候,參數死活傳不進去。前端
後來看了一下springmvc參數封裝的過程,恍然大悟。web
springmvc參數封裝的過程當中有個類是這樣的。spring
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」 因此一直封裝不進去。mvc