【问题标题】:Liferay friendly url with optional param带有可选参数的 Liferay 友好 URL
【发布时间】:2020-01-30 20:39:11
【问题描述】:

我的 URL 有一些参数,所以我使用友好的 URL 来解决 Liferays 默认生成的丑陋 URL。

这几个参数有时是空的,这意味着它不需要,但其他时候给我一些我用于查询的 ID。

示例参数不是null:

https://myliferay.com/example-portlet/-/example-portlet/search/idTable-7
//it works

例如null参数:

https://myliferay.com/example-portlet/-/example-portlet/search/idTable-null
//it works

使用null 参数它可以工作,但我不喜欢在我的网址上看到null

我想要""

https://myliferay.com/example-portlet/-/example-portlet/search/idTable-
//Doesnt work

但它不起作用,就像当参数为空时,URL 与友好的 URL 模式不匹配。

   <route>
       <pattern>/search/idTable-{idTable}</pattern>
       <generated-parameter name="idTable">{idTable}</generated-parameter>
       <implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
       <!--more implicit params-->
   </route>

如何指定参数的可选性?

【问题讨论】:

  • 不确定你从哪里得到null参数:如果你自己生成它,你能把它生成为"",例如空字符串?还是将其转换为这种表示形式?
  • @OlafKock 已经有一个空字符串 "",但空参数与友好 URL 模式不匹配。使用null 可以。我只是做了相反的事情,我将我的 "" 更改为 null 以使其正常工作,但正如我所说,更倾向于不要在 URL 中显示 null

标签: liferay liferay-6 friendly-url


【解决方案1】:

就像所有友好 url 的变量都使用正则表达式一样。您可以像这样更改/覆盖此正则表达式:{idTable:\d}

<route>
    <pattern>/search/idTable-{idTable:.*}</pattern>
    <generated-parameter name="idTable">{idTable}</generated-parameter>
    <implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
    <!--more implicit params-->
</route>

我使用.* 作为 0 个或更多字符的正则表达式,但我不知道它以后是否会给我带来问题。如果有人知道为什么使用该正则表达式不是一个好主意,请发表评论。

使用\d* 升级正则表达式以仅搜索数字或空:{idTable:\d*}

信息:Making URLs friendlier 7.0

【讨论】:

    猜你喜欢
    • 2015-01-06
    • 2015-12-11
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 2015-01-01
    • 2012-11-16
    • 2016-01-13
    相关资源
    最近更新 更多