1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package com.iamberry.rst.utils;
- import com.iamberry.rst.core.page.PagedResult;
- import org.springframework.web.servlet.ModelAndView;
- import java.lang.reflect.Field;
- import java.util.*;
- public class StitchAttrUtil {
- public final static String noProperty[] = {"serialVersionUID"};
- public static Set<String> propertySet = new HashSet<String>(Arrays.asList(noProperty));
- /**
- * 组装ModelAndView
- * @param object
- * @param modelAndView
- * @param url
- * @param pagedResult
- * @throws IllegalAccessException
- */
- public static void setModelAndView(Object object, ModelAndView modelAndView, String url, PagedResult<?> pagedResult) {
- StringBuilder sb = new StringBuilder(url);
- if(pagedResult.getTotal() != 0) {
- pagedResult.setPages((int) Math.ceil((double)pagedResult.getTotal()/pagedResult.getPageSize()));
- }
- sb.append("?pageSize=" + pagedResult.getPageSize());
- sb.append("&totalNum=" + pagedResult.getTotal() );
- StitchAttrUtil.setUrlByObj(sb,object);
- sb.append("&&pageNO=");
- Map<String, Object> map = StitchAttrUtil.getObjToMap(object);
- modelAndView.addAllObjects(map);
- modelAndView.addObject("page", pagedResult);
- modelAndView.addObject("url", sb.toString());
- return;
- }
- /**
- * 将obj 转为map,
- * 属性值为null的不会 put 到 Map 中
- * 属性名称在 noProperty 数组中的属性不会 put 到 Map 中
- * @param object
- * @return
- * @throws IllegalAccessException
- */
- public static Map<String, Object> getObjToMap(Object object) {
- Map<String, Object> map = new HashMap<>();
- Class<?> clazz = object.getClass();
- for (Field field : clazz.getDeclaredFields()) {
- field.setAccessible(true);
- String fieldName = field.getName();
- Object value = null;
- try {
- value = field.get(object);
- }catch (IllegalAccessException e){
- }
- if (value != null && !propertySet.contains(fieldName)) {
- map.put(fieldName, value);
- }
- }
- return map;
- }
- /**
- * 拼接url
- * @param sb
- * @param object
- * @throws IllegalAccessException
- */
- public static void setUrlByObj(StringBuilder sb,Object object){
- Class<?> clazz = object.getClass();
- for (Field field : clazz.getDeclaredFields()) {
- field.setAccessible(true);
- String fieldName = field.getName();
- Object value = null;
- try {
- value = field.get(object);
- }catch (IllegalAccessException e){
- }
- if (value != null && !propertySet.contains(fieldName)) {
- sb.append("&"+fieldName+ "=" + value.toString());
- }
- }
- }
- }
|