如何添加面包屑到Spring MVC? [英] How to add breadcrumb to Spring MVC?

查看:106
本文介绍了如何添加面包屑到Spring MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将面包屑添加到Spring MVC的所有页面?我想问一个好问题,因为面包屑现在很流行,可能是很多其他人的问题.

How can I add breadcrumb to all pages of Spring MVC? I suppose it would be a good question to ask as breadcrumbs are quite popular now and it might be the question of many others.

我找到了使用dummiesmind.breadcrumb.springmvc的解决方案.注释中,还有一个问题关于stackoverflow,但是除了找到与Ive找到的解决方案相似的解决方案之外,我学不到很多东西.另一个解决方案是使用JavaScript的.

I've found this solution that is using dummiesmind.breadcrumb.springmvc.annotations, there is also a question on stackoverflow but I could not learn much except finding similar solution to the one that Ive found. Another solution is this one that is using JavaScript.

有人能比我提到的有更好的选择吗?

Does anyone have a better option to the ones I mentioned?

@Link(label="Sample Link", family="controllerFamily", parent="");                                                                  
@RequestMapping(value = "sample.do", method=RequestMethod.GET);
public ModelAndView sampleMethod(HttpSession session){...}

推荐答案

您使用什么来显示页面? JSP?胸腺吗?

What are you using to display your pages ? JSP ? Thymeleaf ?

您指向的链接似乎是一个不错的选择,或者至少您可以基于该链接构建适用于您的自定义项.使用注释可使代码保持整洁,但您需要确保将它们添加到各处.

The link you point to seems a good option or at least you can build something custom that works for you based on that. Using annotations keeps your code clean but you need to make sure you add them everywhere.

您可以使用抽象控制器,在其中定义一种方法来为所有页面创建ModelAndView对象.您可以这样添加面包屑:

You could use an abstract controller where you define a method to create a ModelAndView object for all pages. You could add breadcrumbs that way :

protected ModelAndView createModelAndView(String pageName, BreadCrumb breadCrumb) {
    ModelAndView modelAndView = new ModelAndView(pageName);
    modelAndView.addObject("breadCrumb", breadCrumb);
    return modelAndView;
}

当然,您每次实现时都需要调用该方法.

Of course you would need to call that method everytime in your implementation.

此方法的一种用法是将您的请求url作为参数传递,并通过将文本与路径的每个部分相关联(使用当前本地文本将其合并)来解析它以构建面包屑.因此,例如"/admin/user/list"将为您提供主页>>管理>>用户管理>>用户列表.那应该很容易构建.

One usage of this would be to pass your request url as parameter and parse it to build breadcrumbs by associating text to each part of the path (using the current local to tanslate it). So for example "/admin/user/list" would give you Home >> Administration >> User management >> List of users. That should be easy to build.

这篇关于如何添加面包屑到Spring MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆