StitchAttrUtil.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.iamberry.rst.utils;
  2. import com.iamberry.rst.core.page.PagedResult;
  3. import org.springframework.web.servlet.ModelAndView;
  4. import java.lang.reflect.Field;
  5. import java.text.ParsePosition;
  6. import java.text.SimpleDateFormat;
  7. import java.util.*;
  8. public class StitchAttrUtil {
  9. /**
  10. * 组装参数时,会过滤以下参数
  11. * 推荐使用:addNoPro(),添加过滤
  12. */
  13. public final static String timeDate[] = {
  14. "startDate",
  15. "endDate"
  16. };
  17. public static Set<String> timeDateSet = new HashSet<String>(Arrays.asList(timeDate));
  18. public final static String noProperty[] = {
  19. "serialVersionUID",
  20. "awaitingSignclosedProductInfoList",
  21. "signclosedProductInfoList"
  22. };
  23. public static Set<String> propertySet = new HashSet<String>(Arrays.asList(noProperty));
  24. /**
  25. * 组装ModelAndView
  26. * @param object
  27. * @param modelAndView
  28. * @param url
  29. * @param pagedResult
  30. * @throws IllegalAccessException
  31. */
  32. public static void setModelAndView(Object object, ModelAndView modelAndView, String url, PagedResult<?> pagedResult) {
  33. StringBuilder sb = new StringBuilder(url);
  34. if(pagedResult.getTotal() != 0) {
  35. pagedResult.setPages((int) Math.ceil((double)pagedResult.getTotal()/pagedResult.getPageSize()));
  36. }
  37. sb.append("?pageSize=" + pagedResult.getPageSize());
  38. sb.append("&totalNum=" + pagedResult.getTotal() );
  39. StitchAttrUtil.setUrlByObj(sb,object);
  40. sb.append("&&pageNO=");
  41. Map<String, Object> map = StitchAttrUtil.getObjToMap(object);
  42. modelAndView.addAllObjects(map);
  43. modelAndView.addObject("page", pagedResult);
  44. modelAndView.addObject("url", sb.toString());
  45. return;
  46. }
  47. /**
  48. * 将obj 转为map,
  49. * 属性值为null的不会 put 到 Map 中
  50. * 属性名称在 noProperty 数组中的属性不会 put 到 Map 中
  51. * @param object
  52. * @return
  53. * @throws IllegalAccessException
  54. */
  55. public static Map<String, Object> getObjToMap(Object object) {
  56. Map<String, Object> map = new HashMap<>();
  57. Class<?> clazz = object.getClass();
  58. for (Field field : clazz.getDeclaredFields()) {
  59. field.setAccessible(true);
  60. String fieldName = field.getName();
  61. Object value = null;
  62. try {
  63. value = field.get(object);
  64. }catch (IllegalAccessException e){
  65. }
  66. if (value != null && !propertySet.contains(fieldName)) {
  67. map.put(fieldName, value);
  68. }
  69. }
  70. return map;
  71. }
  72. /**
  73. * 拼接url
  74. * @param sb
  75. * @param object
  76. * @throws IllegalAccessException
  77. */
  78. public static void setUrlByObj(StringBuilder sb,Object object){
  79. Class<?> clazz = object.getClass();
  80. for (Field field : clazz.getDeclaredFields()) {
  81. field.setAccessible(true);
  82. String fieldName = field.getName();
  83. Object value = null;
  84. try {
  85. value = field.get(object);
  86. }catch (IllegalAccessException e){
  87. }
  88. if (value != null && !propertySet.contains(fieldName)) {
  89. if(timeDateSet.contains(fieldName)){
  90. //Date currentTime = new Date();
  91. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  92. String dateString = formatter.format(value);
  93. //ParsePosition pos = new ParsePosition(8);
  94. //Date currentTime = formatter.parse(dateString, pos);
  95. sb.append("&"+fieldName+ "=" + dateString);
  96. }else{
  97. sb.append("&"+fieldName+ "=" + value.toString());
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * 添加组装时被过滤的属性名称
  104. * @param pros
  105. */
  106. public static void addNoPro(String...pros){
  107. for(String pro : pros){
  108. if(pro != null && !"".equals(pro))
  109. propertySet.add(pro);
  110. }
  111. }
  112. }