java 动态面包屑生成 - 怎么办?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/161003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic breadcrumb generation - how to do?
提问by kosoant
I'm in the early phases of developing a brand spanking new site with Spring + Tiles. The site needs dynamically generated breadcrumbs.
我正处于使用 Spring + Tiles 开发品牌全新网站的早期阶段。该站点需要动态生成的面包屑。
What I mean by dynamic is that the user may reach a certain site from multiple starting points. If I have views for Customers, Orders and Products, the user could reach a Product directly:
我所说的动态是指用户可能从多个起点到达某个站点。如果我有客户、订单和产品的视图,用户可以直接访问产品:
Products -> Product xyz
or the user could reach a product through a customer's order:
或者用户可以通过客户的订单到达产品:
Customers -> John Doe -> Orders -> Order 123 -> Product xyz
What is the best way to achieve breadcrumbs like these in a java environment? I've previously done this by using a request attribute (a Vector of Url objects) that is filled with the Urls in each action/servlet of my webapp (like in the action List of Products). I'm not happy with this solution as it requires adding code to each controller/action for generating the breadcrumb trail. And in a case like viewing a product of given order of given customer, the if-then-else logic needed to determine the trail is awful.
在 Java 环境中实现此类面包屑的最佳方法是什么?我以前通过使用请求属性(Url 对象的向量)来完成此操作,该属性在我的 web 应用程序的每个操作/servlet 中填充了 Url(例如在产品的操作列表中)。我对这个解决方案不满意,因为它需要向每个控制器/动作添加代码来生成面包屑路径。在查看给定客户的给定订单的产品的情况下,确定路径所需的 if-then-else 逻辑很糟糕。
Are there any libraries that I could use?
有没有我可以使用的库?
回答by Vinko Vrsalovic
Why don't you just use a session variable that stores the trail? Each view would only have to either append itself to the variable or reset the variable in case of the 'root' views. The code to append it and the code to show it would always be the same and could go in a generic library, you just would call it with a flag to either append or reset the value in the case of storing the trail.
为什么不直接使用存储轨迹的会话变量?每个视图只需将自身附加到变量或在“根”视图的情况下重置变量。附加它的代码和显示它的代码总是相同的,可以放在一个通用库中,你只需要用一个标志调用它,在存储轨迹的情况下附加或重置值。
回答by Binil Thomas
Struts2 has a breadcrumbsplugin.
Struts2 有一个面包屑插件。
回答by gtosto
There is a more recent struts2breadcrumb plugin hosted at google codeit is very configurable and should satisfy your needs.

