我是否需要在 PrettyFaces 中为此创建一个自定义正则表达式模式
当我有以下映射(示例)时,是否需要创建自定义正则表达式模式来匹配 URL:
<url-mapping id="approvedQuestions">
<pattern>/questions/approved/#{viewOption}/</pattern>
<view-id>/approved.xhtml</view-id>
</url-mapping>
当用户不以“/”结尾 URL 时,viewoption-部分也应该匹配? 如果根本不添加 viewOption 部分,是否可以提供某种默认值?
如果 viewOption 是一个枚举,是否可以将参数小写?现在我必须写入大写才能使其工作。
Do I need to make a custom regex pattern to match URLs when I have the following mapping (example):
<url-mapping id="approvedQuestions">
<pattern>/questions/approved/#{viewOption}/</pattern>
<view-id>/approved.xhtml</view-id>
</url-mapping>
where the viewoption-portion should also match when the user does NOT end the URL with '/'?
And is it possible to supply some kind of default value if the don't add the viewOption portion at all?
And if I the viewOption is a enum, is it possible to lowercase the parameter? Now I have to write uppercase in to make it work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用自定义正则表达式来执行此类操作,但我建议使用 url 重写规则来附加尾部斜杠(如果缺少斜杠)。您应该选择一个 URL(末尾有或没有“/”),否则您实际上是在使用两个不同的地址提供相同的资源,并且您将受到搜索引擎和其他爬虫的惩罚。
为此,我将使用如下重写规则:
这将导致服务器检测 URL 末尾是否缺少“/”,并将请求重定向到正确的位置,并带有“ /' 在最后。
为了解决您的枚举问题,这有点复杂。我们通常不建议将值直接绑定到枚举中。在这种情况下,您实际上并没有绑定到枚举(我猜测),而是实际上将文字字符串 URL 值绑定到请求范围的 EL 上下文中。然后,该值将被提取到应用程序中的其他位置,也就是转换为 ENUM 的位置。
在 PrettyFaces 4 发布之前,我建议将值绑定到字符串位置,然后使用操作方法自己加载正确的值,如下所示:
如果您想尝试更高级的 URL 重写工具,也可以从OCPsoft,您可以使用“重写”(http://ocpsoft.com/rewrite/),这是一个基于Java的URL重写工具,但与 JSF 的集成程度不高。
PrettyFaces 4 将基于重写作为核心,此时,您当前使用的所有功能也将能够执行类似这样的操作,如果我没有记错的话,这就是您想要的:
您需要创建您自己的变压器,因为它们尚未在库中定义,但这是总体思路。它比 PrettyFaces 当前的功能强大得多,但不提供相同的 JSF 导航集成,并且配置起来有点棘手。
我希望这有帮助,
~林肯
You can use a custom regex to do this type of, but I recommend using a url-rewrite rule to append a trailing slash if one is missing. You should pick one URL (with or without the '/' at the end) otherwise you are actually serving up the same resources with two distinct addresses, and you will be punished by search engines and other crawlers.
To do this, I would use a rewrite rule such as the following:
This will cause the server to detect when a '/' is missing from the end of the URL, and it will redirect the request to the proper location, with a '/' at the end.
In order to address your enum issue, this is a bit more complicated. We don't typically recommend binding values directly into enumerations. In this case, you are not actually binding into an enum (I'm guessing,) but are actually binding the literal string URL value into the request scoped EL context. This value is then being extracted somewhere else in your application, and that is where the conversion into an ENUM is taking place.
Until PrettyFaces 4 comes out, I recommend instead binding the value into a String location, then using an action method to do the loading of the correct value yourself, like so:
If you want to try a more advanced URL-rewriting tool, also from OCPsoft, you can use "Rewrite" (http://ocpsoft.com/rewrite/), which is a Java-based URL-rewriting tool, but does not have as much integration with JSF.
PrettyFaces 4 will be based on rewrite as a core, at which point, all of the features you currently use will also be available with the ability to do something more like this, which is what you want if I am not mistaken:
You would need to create your own transformers because they haven't been defined in the library yet, but that's the general idea. It's much more powerful than what's currently possible with PrettyFaces, but does not provide the same JSF navigation integration, and is a little trickier to configure.
I hope this helps,
~Lincoln