打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Spring Controller 如何对不同对象中相同的属性名注入不同的值


Spring MVC开发当中,有时候需要对不同对象中相同的属性名注入不同的值。通过下面自定义注解代码可解决问题。

 

新建注解类

  1. package com.cdt.uums.spring.resolver;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.ElementType;  
  5. import java.lang.annotation.Retention;  
  6. import java.lang.annotation.RetentionPolicy;  
  7. import java.lang.annotation.Target;  
  8.   
  9.   
  10. @Target(ElementType.PARAMETER)  
  11. @Retention(RetentionPolicy.RUNTIME)  
  12. @Documented  
  13. public @interface RequestBean {  
  14.     String value() default "_def_param_name";  
  15. }  


自定义注解类

 

  1. package com.cdt.uums.spring.resolver;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5. import java.util.HashMap;  
  6. import java.util.Iterator;  
  7.   
  8. import org.springframework.beans.BeanWrapper;  
  9. import org.springframework.beans.BeanWrapperImpl;  
  10. import org.springframework.beans.propertyeditors.CustomDateEditor;  
  11. import org.springframework.core.MethodParameter;  
  12. import org.springframework.web.bind.support.WebArgumentResolver;  
  13. import org.springframework.web.context.request.NativeWebRequest;  
  14.   
  15. public class BeanArgumentResolver implements WebArgumentResolver {  
  16.   
  17.     @SuppressWarnings("rawtypes")  
  18.     public Object resolveArgument(MethodParameter param, NativeWebRequest request) throws Exception {  
  19.         RequestBean requestBean = param.getParameterAnnotation(RequestBean.class);  
  20.           
  21.         if (requestBean != null) {  
  22.             String _param = requestBean.value();  
  23.             if (_param.equals("_def_param_name")) {  
  24.                 _param = param.getParameterName();  
  25.             }  
  26.             Class clazz = param.getParameterType();  
  27.             Object object = clazz.newInstance();  
  28.             HashMap<String, String[]> paramsMap = new HashMap<String, String[]>();  
  29.             Iterator<String> itor = request.getParameterNames();  
  30.             while (itor.hasNext()) {  
  31.                 String webParam = (String) itor.next();  
  32.                 String[] webValue = request.getParameterValues(webParam);  
  33.                 if (webParam.startsWith(_param)) {  
  34.                     paramsMap.put(webParam, webValue);  
  35.                 }  
  36.             }  
  37.             BeanWrapper obj = new BeanWrapperImpl(object);  
  38.             //obj.findCustomEditor(paramClass, paramString)  
  39.             obj.registerCustomEditor(Date.classnullnew CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));  
  40.             //WebDataBinder.  
  41.             System.out.println(obj.findCustomEditor(Date.classnull).getAsText());  
  42.               
  43.             for (String propName : paramsMap.keySet()) {  
  44.                 String[] propVals = paramsMap.get(propName);  
  45.                 String[] props = propName.split("\\.");  
  46.                 if (props.length == 2) {  
  47.                     obj.setPropertyValue(props[1], propVals);  
  48.                 } else if (props.length == 3) {  
  49.                     Object tmpObj = obj.getPropertyValue(props[1]);  
  50.                     if (tmpObj == null)  
  51.                         obj.setPropertyValue(props[1], obj.getPropertyType(props[1]).newInstance());  
  52.                     obj.setPropertyValue(props[1] + "." + props[2], propVals);  
  53.                 }  
  54.   
  55.             }  
  56.             /* 
  57.              * Field[] fields = clazz.getDeclaredFields(); for(Field 
  58.              * field:fields){ obj.setPropertyValue(field.getName(), 
  59.              * paramsMap.get(_param +"."+field.getName())); } 
  60.              *  
  61.              * for(String porpName:paramsMap.keySet()){ String[] params = 
  62.              * paramsMap.get(porpName); if (null != params) { Object 
  63.              * field=obj.getPropertyValue(porpName.replaceFirst(_param+".", 
  64.              * "")); Class 
  65.              * clz=obj.getPropertyType(porpName.replaceFirst(_param+".", "")); 
  66.              * System.out.println(porpName.replaceFirst(_param+".", 
  67.              * "")+":"+field+" "+clz); if(field==null){ 
  68.              * //field=obj.getPropertyType(porpName.replaceFirst(_param+".", 
  69.              * "")).newInstance(); } 
  70.              * obj.setPropertyValue(porpName.replaceFirst(_param+".", ""), 
  71.              * params); } } 
  72.              */  
  73.             /* 
  74.              * Method[] methods = clazz.getMethods(); for (Method m : methods) { 
  75.              * Class[] parClazzs = m.getParameterTypes(); String methodName = 
  76.              * m.getName(); if (parClazzs.length == 1 && 
  77.              * methodName.startsWith("set")) { 
  78.              *  
  79.              * String[] params = paramsMap.get(_param + 
  80.              * methodName.toLowerCase().replace("set", ".")); 
  81.              *  
  82.              * if (parClazzs[0].equals(String.class)) { if (null != params) { 
  83.              * m.invoke(object, new Object[] { params[0] }); } } else if 
  84.              * (parClazzs[0].equals(String[].class)) { if (null != params) { 
  85.              * m.invoke(object, new Object[] { params }); } } } 
  86.              *  
  87.              * } 
  88.              */  
  89.             return object;  
  90.         } else {  
  91.             return WebArgumentResolver.UNRESOLVED;  
  92.         }  
  93.     }  
  94. }  


 

JSP页面

    <input name="committeeInfo.committeeName" id="committeeInfo.committeeName" value=""/>

Spring 控制器中可通过@RequestBean("committeeInfo") CommitteeInfo committeeInfo的方式接收值.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JSON json
ToString()、Convert.ToString()、(string)、as str...
object obj和object obj = null有实际的区别吗?
List转DataTable(反射)
Notify
java对象转为string的几种常用方法剖析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服