如何在 jsf 中创建用户友好和 seo 友好的 url? [英] How to create user-friendly and seo-friendly urls in jsf?

查看:24
本文介绍了如何在 jsf 中创建用户友好和 seo 友好的 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个带有方法 getTitle ()getContent () 的文章类.

For example, I have class Article with methods getTitle () and getContent ().

我也有带有方法 getAllArticles () 的 ArticlesService.如何创建具有有意义名称的链接列表(由 #{article.title} 组成)?喜欢:

I also have ArticlesService with method getAllArticles (). How to create a list of links with meaningful names (formed with #{article.title})? Like:

http://mysiteaddress.com/article/first-article-title
http://mysiteaddress.com/article/how-to-make-links-in-jsf

...或类似的东西.

我可以使用 <h:commandLink> 创建具有所有必要功能的链接,但我不知道如何为它制作漂亮的 'href':它总是有 href '#'.

I can create links with all necessary functionality with <h:commandLink>, but I don't know how to make nice 'href' for it: it always has href '#'.

我可以使用 创建漂亮的链接,但我不知道如何为其添加必要的功能.

I can create nice links with <h:outputLink> but I don't know how to add necessary functionality to it.

在jsp中,我创建了自己的前端控制器,它从请求中解析url,然后重定向到相应的jsp页面.

In jsp I created my own front-controller, which parsed urls from requests and then performed redirection to correspondent jsp-page.

如何在 JSF 中实现相同的功能?

How to achieve the same functionality in JSF?

推荐答案

如果这是对现有应用程序的改进,那么您基本上需要一个 Filter 检测脏"和友好"的 URL.当它检测到一个脏"的 URL 时,它应该重定向请求到一个友好"的 URL,HttpServletResponse#sendRedirect().当它检测到一个友好"的 URL 时,它应该转发请求到脏"的 URL,RequestDispatcher#forward().在这个相关问题中可以找到一个例子:如何在 Java 中使用 servlet 过滤器来更改传入的 servlet 请求 url?

If this is intended as an improvement of an existing application, then you basically need a Filter which detects "dirty" and "friendly" URLs. When it detects a "dirty" URL, then it should redirect the request to a "friendly" URL by HttpServletResponse#sendRedirect(). When it detects a "friendly" URL, then it should forward the request to the "dirty" URL by RequestDispatcher#forward(). An example can be found in this related question: How to use a servlet filter in Java to change an incoming servlet request url?

此外,您还需要一个自定义的ViewHandler 为 JSF 等生成所需的友好"URL.可以在此处找到示例:Java EE Web 应用程序中的动态目录.

Further, you also need a custom ViewHandler to produce the desired "friendly" URL for JSF <h:form>, <h:link>, etc. An example can be found here: Dynamic Directory in Java EE Web Application.

如果这是一个新的应用程序或一个可以更改的应用程序,您可以考虑使用任何现有的漂亮 URL 库,而不是重新发明轮子:

If this is a new application or an application which is open to changes, you could consider any of the existing pretty URL libraries instead of reinventing the wheel:

  • PrettyFaces,这是一个完整的 URL 重写解决方案.它需要一个额外的 XML 配置文件 pretty-config.xml.如果您想要完全更改网址和/或想要配置从旧网址到新网址的重定向,则此库非常有用.
  • FacesViewsOmniFaces 库,它通过单个 web.xml 上下文参数使现有 URL 无扩展名.它还支持MultiViews",从而可以声明性地将路径参数注入到托管 bean 中.例如./foo/bar/baz 可以指向 /foo.xhtml 并且可以注入值 barbaz通过 @Param(pathIndex).
  • PrettyFaces, which is a complete URL rewrite solution. It requires an additional XML configuration file pretty-config.xml. This library is useful if you want to completely change URLs and/or want to configure redirects from old to new URLs.
  • FacesViews of OmniFaces library, which makes existing URLs just extensionless by a single web.xml context param. It also supports "MultiViews" whereby path parameters can declaratively be injected in managed beans. E.g. /foo/bar/baz can point to /foo.xhtml and the values bar and baz can be injected by @Param(pathIndex).

还有 Mojarra Scales 库,但它是一个旧库,而 PrettyFaces 主要基于它,因此不值得付出努力.

There's also the experimental PrettyUrlPhaseListener of Mojarra Scales library, but it's an old library and PrettyFaces is largely based on it, so it's not worth the effort.

这篇关于如何在 jsf 中创建用户友好和 seo 友好的 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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