Displaying active navigation based on contextual data

后端 未结 6 1181
礼貌的吻别
礼貌的吻别 2021-02-07 00:36

I have the following separated fragment in a Thymeleaf template.

6条回答
  •  孤城傲影
    2021-02-07 01:18

    You could add a ModelAttribute with the value active in your controllers for each page, e.g. :

    SettingsController.java

    @RequestMapping("/settings")
    public String viewSettings(Model model) {
      // do stuff
      model.addAttribute("classActiveSettings","active");
      return "settings";
    }
    

    OR in a SettingsControllerAdvice.java

    @ControllerAdvice(assignableTypes = SettingsController.class)
    public class SettingsControllerAdvice {
    
        @ModelAttribute("classActiveSettings")
        public String cssActivePage() {
            return "active";
        }
    
    }
    

    Then, in the navigation fragment included in your settings.html :

    
    

    Finally, you can repeat this process for each controller and links in your navbar.

提交回复
热议问题