斯卡拉lift:使用 LiftRules.statelessRewrite.append 外部声明的变量重写 URL

发布于 2024-09-12 03:02:33 字数 859 浏览 3 评论 0原文

我正在寻找一种使用在 LiftRules.statelessRewrite.append 范围之外声明的列表来重写 lift 中的 URL 的解决方案,

LiftRules.statelessRewrite.append {  
    case RewriteRequest(ParsePath("abc" :: Nil, _ , _ , _ ), _ , _ ) =>  
        RewriteResponse("index" :: Nil)
}

以下代码的工作方式与上面的代码相同:

val requestList = "abc" :: Nil

LiftRules.statelessRewrite.append {  
    case RewriteRequest(ParsePath(requestList, _ , _ , _ ), _ , _ ) =>
        RewriteResponse("index" :: Nil)
}

我希望 有人写过如何使用 lift 2.0 获得这样的功能吗?

[编辑]

您能否建议以参数形式访问此列表后缀的最佳方法。我想要得到的是类似于:

LiftRules.statelessRewrite.append {  
  case RewriteRequest(ParsePath(`requestList` ::: List(someId), _ , _ , _ ), _ , _ ) =>  
    RewriteResponse("index" :: Nil, Map("someId" -> someId))
}    

I'm looking for a solution for rewriting URLs in lift using a list declared outside the scope of LiftRules.statelessRewrite.append

LiftRules.statelessRewrite.append {  
    case RewriteRequest(ParsePath("abc" :: Nil, _ , _ , _ ), _ , _ ) =>  
        RewriteResponse("index" :: Nil)
}

I'd like to have the following code working the same as the one above:

val requestList = "abc" :: Nil

LiftRules.statelessRewrite.append {  
    case RewriteRequest(ParsePath(requestList, _ , _ , _ ), _ , _ ) =>
        RewriteResponse("index" :: Nil)
}

Could anyone write how to get such functionality with lift 2.0?

[edit]

Could you also suggest the best way to access this list's suffix as parameter. What I would like to get is similar to:

LiftRules.statelessRewrite.append {  
  case RewriteRequest(ParsePath(`requestList` ::: List(someId), _ , _ , _ ), _ , _ ) =>  
    RewriteResponse("index" :: Nil, Map("someId" -> someId))
}    

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

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

发布评论

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

评论(1

奶气 2024-09-19 03:02:33

case 语句中的任何小写变量都将创建一个具有该名称的新变量,因此 requestList 将被隐藏。试试这个:

val requestList = "abc" :: Nil

LiftRules.statelessRewrite.append {
  case RewriteRequest(ParsePath(list, _ , _ , _ ), _ , _ ) if list == requestList =>
    RewriteResponse("index" :: Nil)
}

另一种方法是使用反引号(Scala ref:“稳定标识符模式”):

LiftRules.statelessRewrite.append {
  case RewriteRequest(ParsePath(`requestList`, _ , _ , _ ), _ , _ ) =>
    RewriteResponse("index" :: Nil)
}    

在您的情况下,第二种形式将是选择的规范形式,但一般来说第一种形式会更强大。

作为第三种选择,您还可以定义 val RequestList = requestList 并与大写版本进行匹配,但我建议不要这样做,除非您有充分的理由创建大写的 RequestList

Any lowercased variable in a case statement will create a new variable with that name, therefore requestList is going to be shadowed. Try this:

val requestList = "abc" :: Nil

LiftRules.statelessRewrite.append {
  case RewriteRequest(ParsePath(list, _ , _ , _ ), _ , _ ) if list == requestList =>
    RewriteResponse("index" :: Nil)
}

Another approach would be to use backticks (Scala ref: ‘stable identifier patterns’):

LiftRules.statelessRewrite.append {
  case RewriteRequest(ParsePath(`requestList`, _ , _ , _ ), _ , _ ) =>
    RewriteResponse("index" :: Nil)
}    

In your case, the second form would be the canonical one to choose, but in general the first form will be more powerful.

As a third alternative, you could also define val RequestList = requestList and match against the uppercased version, though I would advise against this unless you have a good reason for creating a capitalised RequestList.

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