在struts2中创建动态资源的友好url

发布于 2024-07-08 10:28:12 字数 964 浏览 10 评论 0原文

我有一个带有单个页面的 struts2 应用程序,可以显示数据库中存储的多个值之一。 该申请适用于一所拥有多个院系的学校,每个院系都有多个项目。 使用这样的 url 访问部门页面

department.action?id=2

,DepartmentAction 将加载 id = 2 的部门进行显示。 如果用户只是浏览网站,这一切都很好,但如果我想提供报纸上工程部门的链接,就会感到不舒服。 该链接必须为 www.myschooldomain.com/department.action?id=2。 我发现这方面存在很多问题。

首先,它对用户不友好。 其次,它很容易被破坏,因为部门是动态维护的,并且部门的 ID 可能会在没有警告的情况下发生变化,从而导致链接不稳定。

我更喜欢打印这样的网址:www.myschooldomain.com/department/engineering 并以某种方式转到department.action?id=2。

到目前为止我的想法是:创建一个操作,该操作将在最后解析部门名称的 url,然后按名称查找。 也许我可以为每个部门在数据库中添加一个Friendlyurl 字段。

但问题是:struts2中有没有更好的方法来做到这一点?

谢谢。

更新(2009 年 5 月):我只是偶然发现这个问题,并想我会说一下我是如何解决这个问题的。

我在 struts.xml 中创建了一个名为“departments”的新包。 在此包中,只有一个操作映射到 *. 因此它捕获对 mydomain.com/departments/anything.html 的所有请求。

在操作类中,我简单地解析 url 并查找 Departments/ 和 .html 之间的部分,这就是部门的名称,这样我就可以在数据库中查找它。 近 5 个月以来,该功能一直运行良好,并且我已将其应用于网站的其他区域。

I have a struts2 application with a single page that may show one of a number of values stored in a database. The application is for a school with many departments and each department has many programs. The department page is accessed using a url like this

department.action?id=2

and the DepartmentAction will load the Department with id = 2 for display. All this is fine if the user is just browsing around the site but it gets uncomfortable if I want to provide a link to say the Engineering department in the newspapers. The link will have to be www.myschooldomain.com/department.action?id=2. I see a number of problems with this.

First, it is not user friendly. Second, it is prone to be broken because the departments are dynamically maintained and the id for a department could change without warning making the link unstable.

I would prefer to print a url like this: www.myschooldomain.com/department/engineering and have that somehow go to department.action?id=2.

My thoughts so far: create an action that will parse the url for the department name at the end then look it up by name. Maybe I could add a friendlyurl field to the database for each department.

But the question is: Is there a better way to do this in struts2?

Thanks.

Update (May 2009): I just happened to stumble back over this question and thought that I would say what I did to solve it.

I created a new package in the struts.xml called departments. In this package there is only one action mapped to *. So it catches all requests to mydomain.com/departments/anything.html.

In the action class I simply parse the url and look for the part between departments/ and .html and that is the name of the department so I can do a lookup in the database for it. This has been working fine for almost 5 months now and I have implemented it for other areas of the site.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光清浅 2024-07-15 10:28:12

您可以使用 URL 重写过滤器

这避免了对任何额外 servlet 或 Java 代码的需要,但需要 XML 描述符。

You could use the URL Rewrite filter

This avoids the need for any additional servlet or Java code but requires XML descriptors.

素衣风尘叹 2024-07-15 10:28:12

这通常是通过将 servlet 映射到(在您的情况下为“/department”),然后使用 servlet 内的路径信息(例如“/engineering”)以确定 ID。

由于 Struts2 调度程序没有实现此行为,因此编写您自己的 servlet 可能是最简单的方法。 该 Servlet 将配置有效的“友好”名称到不友好的数字标识符的映射。 这可以是一个实际的Map,也可以使用数据库查找器方法来完成。

getPathInfo() 的结果将用于查找 ID,请求将是 转发到department.action。 也处理 null 情况,这意味着用户正在尝试浏览 /departments/ 目录。

This is normally done by mapping a servlet to, in your case '/department', and then using the path information (e.g., '/engineering') within the servlet to determine the ID.

Since the Struts2 dispatcher doesn't implement this behavior, it might be simplest to write your own servlet. This servlet would be configured with a map of valid "friendly" names to the unfriendly numeric identifiers. This could be an actual Map or it could be done with a database finder method.

The result of getPathInfo() would be used to look up the ID, and the request would be forwarded to the department.action. Handle the null case too, which means the user is trying to browse the /departments/ directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文