如何在jsf中创建用户友好和seo友好的URL? [英] How to create user-friendly and seo-friendly urls in jsf?
问题描述
例如,我有一个带有方法getTitle ()
和getContent ()
的Article类.
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 '#'.
我可以使用<h:outputLink>
创建漂亮的链接,但是我不知道如何向其中添加必要的功能.
I can create nice links with <h:outputLink>
but I don't know how to add necessary functionality to it.
在jsp中,我创建了自己的前端控制器,该控制器解析请求中的url,然后执行重定向到相应的jsp-page.
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?
推荐答案
如果这是对现有应用程序的改进,则基本上需要如何在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?
此外,您还需要自定义 <h:form>
,<h:link>
等生成所需的友好" URL.可以在此处找到示例:
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
.如果您要完全更改URL和/或配置从旧URL到新URL的重定向,则此库很有用. - FacesViews "nofollow noreferrer> OmniFaces 库,该库使现有的URL只能通过单个
web.xml
上下文参数进行扩展.它还支持"MultiViews",从而可以声明性地将路径参数注入到托管bean中.例如./foo/bar/baz
可以指向/foo.xhtml
,值bar
和baz
可以由@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 valuesbar
andbaz
can be injected by@Param(pathIndex)
.
还有
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屋!