项目需求
不同的任务会有不同的配置,根据所选择的任务,展示不同的表单
思路
后端给页面返回 List <String,List> list, object 是各任务所有的属性集合
代码
// 自定义注解
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TaskProperty {
/**
* 属性描述名
*/
String label() default "";
/**
* 默认值
*/
String defaultValue() default "";
/**
* 是否必填
*/
boolean required() default false;
/**
* 输入框文本
*/
String placeHolder() default "";
}
//所有可执行的 枚举
@Getter
public enum TaskConfigEnum {
HNZX(HnUploadConfig.class,"定时"),
;
private Class type;
private String name;
TaskConfigEnum(Class type, String name) {
this.type = type;
this.name = name;
}
}
// 对用task 需要的配置
@Data
public class HnUploadConfig {
@TaskProperty (required = true,label = "ip地址",placeHolder = "127.0.0.1")
private String host;
@TaskProperty(required = true,label = "端口")
private String port;
@TaskProperty (required = true,label = "表达式")
private String cron;
@TaskProperty (required = true,label = "方法名")
private String method;
@TaskProperty (required = true,label = "参数")
private String param;
}
// main method
public Object getTranslatorList() {
TaskConfigEnum[] translators = TaskConfigEnum.values();
List<TaskVo> list = Arrays.stream(translators).map(translator -> {
TaskVovo = new TaskVo();
vo.setName(translator.getName());
Class<?> type = translator.getType();
Field[] fields = type.getDeclaredFields();
List<TaskPropertyVo> propertyList = Arrays.stream(fields).filter(field -> field.isAnnotationPresent(TaskProperty.class)).map(field -> {
TaskPropertyVo property = new TaskPropertyVo();
property.setName(field.getName());
TaskProperty translatorProperty = field.getAnnotation(TaskProperty.class);
if (StringUtils.isNotBlank(translatorProperty.label())) {
property.setLabel(translatorProperty.label());
} else {
property.setLabel(field.getName());
}
property.setRequired(translatorProperty.required());
property.setPlaceHolder(translatorProperty.placeHolder());
return property;
}).collect(Collectors.toList());
vo.setPropertyList(propertyList);
return vo;
}).collect(Collectors.toList());
return list;
}
@Data
public class TaskVo {
//名称
private String name;
//属性集合
private List<TaskPropertyVo> propertyList;
}
@Data
public class TaskPropertyVo {
//属性名称
private String name;
//属性描述
private String label;
//是否必填
private boolean required;
//placeHolder
private String placeHolder;
}