SpringMVC 肯定pojo值的三步

1,若是隱含模型中有這個key(標了modelAttribute 註解就是註解指定的value,
沒標就是參數類型的首字母小寫)指定的值;若是有將這個值賦值給bindObject
2,若是是sessionAttribute標註的屬性,就從session中拿,拿不到就拋異常
3,若是都不是,就利用反射建立對象java

源碼以下:類名HandlerMethodInvokerweb

private WebDataBinder resolveModelAttribute(String attrName, MethodParameter methodParam,
			ExtendedModelMap implicitModel, NativeWebRequest webRequest, Object handler) throws Exception {

		// Bind request parameter onto object...
		String name = attrName;
		if ("".equals(name)) {
			name = Conventions.getVariableNameForParameter(methodParam);
		}
		Class<?> paramType = methodParam.getParameterType();
		Object bindObject;
		if (implicitModel.containsKey(name)) {
			bindObject = implicitModel.get(name);
		}
		else if (this.methodResolver.isSessionAttribute(name, paramType)) {
			bindObject = this.sessionAttributeStore.retrieveAttribute(webRequest, name);
			if (bindObject == null) {
				raiseSessionRequiredException("Session attribute '" + name + "' required - not found in session");
			}
		}
		else {
			bindObject = BeanUtils.instantiateClass(paramType);
		}
		WebDataBinder binder = createBinder(webRequest, bindObject, name);
		initBinder(handler, name, binder, webRequest);
		return binder;
	}
相關文章
相關標籤/搜索