简体   繁体   English

在Play中调用静态方法! 框架控制器不起作用

[英]Calling static methods in Play! framework controller doesn't work

I have a Play! 我有戏! framework with two actions which contain redundant code. 具有两个包含冗余代码的操作的框架。 So I factored this code into a private static method, but It doesn't work anymore then. 因此,我将此代码分解为一个private static方法,但此后不再起作用。

  public static void show(long itemId, String listId) {
    render(getItem(itemId, listId));
  }

  private static Item getItem(long itemId, String listId) {
    // otherwise duplicate code ...
    return item;
  }

If I inline the code contained in getItem into the show action everything is fine: 如果我将getItem包含的代码内联到show action中,一切都很好:

  // this works
  public static void show(long itemId, String listId) {
    Item item = // duplicate code ...
    render(item);
  }

Why can I not call other static methods within a Play! 为什么不能在Play中调用其他静态方法! controller? 控制器?

Solution

Thanks to 'Codemwnci' I've implemented the following solution: 感谢“ Codemwnci”,我实现了以下解决方案:

  public static void show(long itemId, String listId) {
    renderArgs.put("item", getItem(itemId, listId));
    render();
  }

I prefer renderArgs because it makes the intention more clear than a local variable. 我更喜欢renderArgs因为它使意图比局部变量更清晰。

When you pass a local variable into the render method, the name of the local variable is used when passed through to the Groovy view. 当您将局部变量传递给render方法时,传递给Groovy视图时将使用局部变量的名称。 In your example, you are not passing a local variable, therefore Play does not know what name to give the item you have specified. 在您的示例中,您没有传递局部变量,因此Play不知道给您指定的项目赋予什么名称。

You have a couple of options. 您有两种选择。 You can do either 你可以做

  1. Set the return from getItem to a local variable (item), and pass item into the view 将getItem的返回值设置为局部变量(item),然后将item传递到视图中
  2. Set the return from getItem into the renderArgs map, and specify your own name. 将getItem的返回值设置到renderArgs映射中,并指定您自己的名称。

Option 1 is probably the most sensible. 选项1可能是最明智的。

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

相关问题 播放框架:使用字符串参数重定向到控制器方法不起作用 - Play Framework: Redirect to controller method with string argument doesn't work 播放框架2-静态方法 - play framework 2 - static methods 每个 Play 框架 Web 请求是否都使用新的依赖注入控制器实例处理,那么静态控制器方法呢? - Is each Play framework web request handled with a new dependency injected controller instance, but then what about static controller methods? 播放框架JavaScript路线+非静态方法 - Play framework javascript routes + non static methods Update() 方法在带有 h2 数据库的 Play Framework 中不起作用 - Update() method doesn't work in Play Framework with h2 database 身份验证播放框架重定向到页面不起作用 - authentication play framework redirection to pages doesn't work Play Framework 2.1.2国际化不适用于芬兰字符 - Play Framework 2.1.2 internationalization doesn't work with Finnish characters 播放框架:提交按钮似乎不起作用 - Play framework: Submit button doesn't seem to work 在Spring上使用@Before方面对@Controller方法不起作用 - Using @Before aspect on @Controller methods with Spring doesn't work 在Controller方法中抛出异常Play框架? - Throwing exceptions in Controller methods Play framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM