Java中使用Yaml文件配置的子菜单

时间:2019-06-12 06:27:39

标签: java html css yaml pug

我正在尝试在现有菜单中创建子菜单,该菜单是使用Java代码中的.yaml配置文件进行配置的。

请在此处找到现有的设计实现模型

Title
  Menu 1
  Menu 2
  Menu 3

但是现在,我正在尝试实现以下设计

Title
  Menu 1
  Menu 2
    SubMenu 2.1
    SubMenu 2.2
  Menu 3

请找到我现有的.yaml配置文件。

- !!com.model.Menu
  name: Programs
  code: PRGRM
  action:
  css: icon programs-icon
  childMenus:
  - !!com.model.Menu
    name: Create
    code: PRGRM_CRT
    action: /programs/basic_information
    css: icon icon-create
  - !!com.cat.pscs.model.Menu
    name: Download Template
    code: DWNLD_BSNS_CS_TMPLT
    action: /business-case-templates
    css: icon icon-download

在这里,我正在尝试为name: Create创建子菜单。但是它不起作用。请找到我正在尝试的以下代码。

- !!com.model.Menu
  name: Programs
  code: PRGRM
  action:
  css: icon programs-icon
  childMenus:
  - !!com.model.Menu
    name: Create
    code: PRGRM_CRT
    action: /programs/basic_information
    css: icon icon-create
    visibleToExternal: false
    childMenus:
      - !!com.model.Menu
        name: Create
        code: PRGRM_CRT
        action: /programs/basic_information
        css: icon icon-create
  - !!com.cat.pscs.model.Menu
    name: Download Template
    code: DWNLD_BSNS_CS_TMPLT
    action: /business-case-templates
    css: icon icon-download

请找到我的java pojo类。

@Getter
@Setter
@NoArgsConstructor
public class Menu {
  private String name;
  private String code;
  private String action;
  private String css;
  private String symbol;


  private List<Menu> childMenus;

  private List<Menu> subMenus;

  public boolean hasChildMenus() {
    return childMenus != null && !childMenus.isEmpty();
  }
}

请在下面找到menuRepository

@Repository
public class MenuRepository {
  private static final Logger LOGGER = LoggerFactory.getLogger(MenuRepository.class);
  private static String menuYamlLocation = "classpath:config/menus.yaml";
  private Collection<Menu> menus;

  public Collection<Menu> getMenus() {
    Cloner cloner = new Cloner();
    return cloner.deepClone(menus);
  }

  @PostConstruct
  private void loadMenus() throws IOException {
    LOGGER.info("Loading menus from the file.");
    Yaml yaml = new Yaml();
    InputStream in = null;
    try {
      final DefaultResourceLoader loader = new DefaultResourceLoader();
      final Resource resource = loader.getResource(menuYamlLocation);
      in = resource.getInputStream();
      menus = (Collection<Menu>) yaml.load(in);
    } finally {
      close(in);
    }
  }

  private void close(InputStream in) {
    if (in != null) {
      try {
        in.close();
      } catch (Exception e) {
        // no-op
      }
    }
  }
}

Update-1

如下所示,在menu.yaml文件中添加了我的子菜单代码。

 - !!com.model.Menu
    name: Defaults
    code: SYS_DFLT
    action: /system-defaults
    css: icon icon-system-defaults
 subMenus:
  - !!com.model.Menu
    name: System Defaults
    code: UPLD_PRGRM_USG
    action: /upload-program-usage
    css: icon icon-upload

请找到我的subMenus.jade文件

ul.subMenu(role="menu", aria-labelledby="dLabel")
  for sub_menu in menu.getSubMenus()
    li
      a(href="#!{sub_menu.getAction()}", id="#{menu.getName()}_#{sub_menu.getName()}")
        if sub_menu.getSymbol() != null
          span.hidden-md #{sub_menu.getSymbol()}
        if sub_menu.getCss() != null
          span(class="#{sub_menu.getCss()}")
        | !{sub_menu.getName()}

我已经在我的childMenu.jade文件中添加了子菜单代码(我已经在我的childMenu.jade中添加了subMenu.jade)

ul.dropdown-menu(role="menu", aria-labelledby="dLabel")
  for child_menu in menu.getChildMenus()
    li
      a(href="#!{child_menu.getAction()}", id="#{menu.getName()}_#{child_menu.getName()}")
        if child_menu.getSymbol() != null
          span.hidden-md #{child_menu.getSymbol()}
        if child_menu.getCss() != null
          span(class="#{child_menu.getCss()}")
        | !{child_menu.getName()}
  if menu.getSubMenus() != null
    include _sub_menu    

请找到所附的屏幕截图。子菜单仍然无法正常显示

enter image description here

0 个答案:

没有答案