简体   繁体   English

如何提取路径的一部分并将其作为参数传递给Java中Play框架中的所有操作?

[英]How can I extract part of the path and pass it as parameter to all actions in Play Framework in java?

Our paths are something like: 我们的路径类似于:

  • www.example.org/first/firstaction www.example.org/first/firstaction
  • www.example.org/second/firstaction www.example.org/second/firstaction
  • www.example.org/first/secondaction www.example.org/first/secondaction
  • www.example.org/second/secondaction www.example.org/second/secondaction

We need to get the first part (first, second) of the path for all actions, manipulate it and pass it to the action. 我们需要获取所有动作的路径的第一部分(第一,第二部分),对其进行操作并将其传递给动作。 I dont want to make the manipulation in every action, so I'm looking for a better way. 我不想在每个动作中都进行操作,因此我正在寻找一种更好的方法。

Filters might be good, but Filters wrap the action after the action has been looked up by the router. 过滤器可能不错,但是在路由器查看了操作之后,过滤器会包装该操作。 So they are too late for me. 所以他们对我来说太迟了。 Global.onRouteRequest might be better, but I don't understand how to pass parameters to the action neither how to pass to standard routing afterwards. Global.onRouteRequest可能会更好,但是我不明白如何将参数传递给操作,也无法随后传递给标准路由。

What would be the best way? 最好的方法是什么?

我切换到动作合成:使用动作来操纵路径前缀,并将每个ctx.args.put()将其传递给每个注释所链接的每个动作。

Try using a split, the '/' as the split: 尝试使用拆分,以“ /”作为拆分:

String[] parts = url.split('/');
String first = parts[1]; // first
String second = parts[2]; // firstaction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM