Exemplo n.º 1
0
  protected <T extends Model> List<Property> getPartProperties(T model) {
    List<Property> properties = new ArrayList<Property>();

    Enumeration<?> pars = getRequestParameterNames();
    while (pars.hasMoreElements()) {
      String par = (String) pars.nextElement();
      if (par.startsWith("model.") && !par.equals("model.id")) {
        String prop = par.replace("model.", "");
        if (prop.contains(".")) {
          if (prop.contains(".id")) {
            // 处理两个对象之间的引用,如:model.org.id=1
            String[] attr = prop.replace(".", ",").split(",");
            if (attr.length == 2) {
              Object obj = ReflectionUtils.getFieldValue(model, attr[0]);
              properties.add(new Property(prop, ReflectionUtils.getFieldValue(obj, attr[1])));
            }
          }
        } else {
          properties.add(new Property(prop, ReflectionUtils.getFieldValue(model, prop)));
        }
      }
    }
    assemblyModelForPartUpdate(properties);

    return properties;
  }
Exemplo n.º 2
0
 protected void buildModel(Model model) {
   Enumeration<?> pars = getRequestParameterNames();
   while (pars.hasMoreElements()) {
     String par = (String) pars.nextElement();
     if (par.startsWith("model.")) {
       String fieldName = par.replace("model.", "");
       String value = getRequestParameterValue(par);
       // 这里应该有类型转换和合法性校验
       PropertyEditor propertyEditor = new PropertyEditor(fieldName, Operator.eq, value);
       Class<?> fieldType = ReflectionUtils.getDeclaredField(model, fieldName).getType();
       Object fieldValue;
       if (fieldType != propertyEditor.getPropertyType().getValue()) {
         log.debug(fieldType + "!=" + propertyEditor.getPropertyType().getValue());
         fieldValue = propertyEditor.getProperty().getValue().toString();
       } else {
         fieldValue = propertyEditor.getProperty().getValue();
       }
       ReflectionUtils.setFieldValue(model, fieldName, fieldValue);
     }
   }
 }
Exemplo n.º 3
0
 protected String getDefaultModelName(Class clazz) {
   String modelClassName = ReflectionUtils.getSuperClassGenricType(clazz).getSimpleName();
   return Character.toLowerCase(modelClassName.charAt(0)) + modelClassName.substring(1);
 }