In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to expand Spring MVC data binding, the content is very detailed, interested friends can refer to, hope to be helpful to you.
After three versions of Spring MVC, the function has been improved and improved a lot. In particular, the parameter binding of Annotation has been used since 2.5, which greatly facilitates the development, and 3.0 further improves it. For some special foreground frameworks, the parameters passed to the background are not the parameters in the ordinary request, but the xml format in the request stream, so you can't use the parameter binding method that comes with SpringMVC. At this point, consider whether it can be expanded.
SpringMVC uses AnnotationMethodHandlerAdapter.java by default, and you can modify this class to implement the extension. The key locations are in the following methods:
Otected ModelAndView invokeHandlerMethod (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {ServletHandlerMethodResolver methodResolver= getMethodResolver (handler); Method handlerMethod = methodResolver.resolveHandlerMethod (request); ServletHandlerMethodInvoker methodInvoker=new ServletHandlerMethodInvoker (methodResolver); ServletWebRequest webRequest = new ServletWebRequest (request, response); ExtendedModelMap implicitModel = new BindingAwareModelMap (); Object result=methodInvoker.invokeHandlerMethod (handlerMethod,handler,webRequest, implicitModel); ModelAndView mav=methodInvoker.getModelAndView (handlerMethod,handler.getClass (), result, implicitModel, webRequest) MethodInvoker.updateModelAttributes (handler, (mav. GetModel (): null), implicitModel,webRequest); return mav;}
The blue position is the key point, and ServletHandlerMethodInvoker.java is the inner class, which inherits from the HandlerMethodInvoker.java,invokeHandlerMethod method and needs to be extended. Continue to track this method and find that it is the method of the class HandlerMethodInvoker.java. The key method in this method is resolveHandlerArguments (). The key parts are as follows:
If (RequestParam.class.isInstance (paramAnn)) {RequestParam requestParam = (RequestParam) paramAnn; paramName = requestParam.value (); required = requestParam.required (); defaultValue = parseDefaultValueAttribute (requestParam.defaultValue ()); annotationsFound++;} else if (RequestHeader.class.isInstance (paramAnn)) {RequestHeader requestHeader = (RequestHeader) paramAnn; headerName = requestHeader.value (); required = requestHeader.required (); defaultValue = parseDefaultValueAttribute (requestHeader.defaultValue ()); annotationsFound++;}
To this extension, you need to add your own type, such as RequestParamExt, and add it later, imitating as follows:
Else if (RequestParamExt.class.isInstance (paramAnn)) {RequestParamExtrequestParam = (RequestParamExt) paramAnn; paramName = requestParam.value (); defaultValue = parseDefaultValueAttribute (requestParam.defaultValue ()); miType = requestParam.type (); annotationsFound++;}
In
Else if (paramName! = null) {args [I] = resolveRequestParam (paramName, required, defaultValue, methodParam, webRequest, handler);}
Add extension logic to this method:
If (! RequestParamExt.TYPE_NONE.equals (miType)) {if (null = = platformRequest) {HttpServletRequest request = webRequest.getNativeRequest (HttpServletRequest.class); platformRequest = new PlatformRequest ((HttpServletRequest) request, "utf-8"); platformRequest.receiveData ();} if (RequestParamExt.TYPE_PLATFORMREQUEST.equals (miType)) {args [I] = platformRequest;} else if (RequestParamExt.TYPE_STR.equals (miType)) {args [I] = resolveRequestStrParamExt (platformRequest, methodParam) } else {args [I] = resolveRequestParamExt (miType,platformRequest,paramName, defaultValue, methodParam, webRequest, handler);}}
The two resolveRequest*Ext methods are as follows:
Protected Object resolveRequestStrParamExt (PlatformRequest platformRequest, MethodParameter methodParam) {VariableList inVl = platformRequest.getVariableList (); String paraName = methodParam.getParameterName (); return inVl.getValueAsObject (paraName);} protected Object resolveRequestParamExt (String miType,PlatformRequest platformRequest, String paramName, String defaultValue,MethodParameter methodParam,NativeWebRequest webRequest, Object handler) throws Exception {if (StringUtils.isBlank (paramName)) {paramName = defaultValue;} Class paramType = methodParam.getParameterType (); DatasetList inDl = platformRequest.getDatasetList (); VariableList inVl = platformRequest.getVariableList () If (RequestParamExt.TYPE_DS.equals (miType)) {/ / the key process of binding Dataset ds = inDl.getDataset (paramName); Object vo = paramType.newInstance (); MiPDataBinder dataBinder = new MiPDataBinder (vo, false); dataBinder.bind (inVl); return dataBinder.getTarget ();}}
You also need a definition of annotation: an example is as follows:
Package com.company.springext.web.bind.annotation; 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 RequestParamExt {public static final String TYPE_NONE = "none"; public static final String TYPE_DS = "ds" Public static final String TYPE_VL = "vl"; public static final String TYPE_STR = "string"; String type () default TYPE_NONE; String value () default ""; String defaultValue () default "ds";}
* modify Spring configuration:
At this point, data binding in a custom format is implemented.
For the output of a specific format, if you need to customize, you also need to modify the AnnotationMethodHandlerAdapterExt.java class, the key point is in the getModelAndView () method. At the following location:
} else if (AnnotationUtils.findAnnotation (handlerMethod, ResponseBody.class)! = null) {handleResponseBody (returnValue, webRequest); return null;}
Add your own extension method:
Else if (AnnotationUtils.findAnnotation (handlerMethod, ResponseBodyExt.class)! = null) {ResponseBodyExt bodyMi = AnnotationUtils.findAnnotation (handlerMethod, ResponseBodyExt.class); handleResponseBodyExt (returnValue, webRequest, bodyMi); return null;}
Define the handleResponseBodyExt method:
Private void handleResponseBodyExt (Object returnValue, ServletWebRequest webRequest, ResponseBodyMI bodyMi) throws Exception {HttpServletResponse servletResponse = (HttpServletResponse) webRequest.getNativeResponse (); writeWithExtConverters (returnValue, servletResponse, bodyMi);}
The writeWithExtConverters () method is as follows:
Private void writeWithExtConverters (Object returnValue, HttpServletResponse response, ResponseBodyMI bodyMi) throws Exception {convertToXML (...);}
The mode of use is as follows:
@ RequestMapping (value= "/ getContractList") @ ResponseBodyExt (isCheck=true, resultType= "sql", sqlColumns= "ID,TUREID") public Page getContractList (@ RequestParamExt (value= "ds_search", type = "ds") Contract cp) throws Exception {Page page = method1 (); return page;} this is the end of the extension on how to bind Spring MVC data. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.