【问题标题】:Play Framework Action not found:未找到播放框架操作:
【发布时间】:2014-12-14 17:09:32
【问题描述】:

我正在自学 Play 框架,并从 Manning 购买了“Play for Java”。我按照他们的指示自从这本书出版以来已经有一些更新(自然)。我收到以下错误:

未找到请求“GET /products”的操作

包控制器;

import play.mvc.Controller;
import play.mvc.Result;

public class Products extends Controller {

  public static Result list(){
    return TODO;
  }

  public static Result newProduct(){
    return TODO;
  }

  public static Result details(String ean){
    return TODO;
  }

  public static Result save(){
    return TODO;
  }

}

GET / controllers.Application.index()

GET /assets/*file controllers.Assets.at(path="/public", file)

GET /products/controllers.Products.list()

GET /products/new controllers.Products.newProduct()

GET /products/:ean controllers.Products.details(ean: String)

POST /products/controllers.Products.save()

我为此使用了 HTML 标签,我希望这是在此处发布的正确方式。

【问题讨论】:

    标签: playframework


    【解决方案1】:

    我上次使用 Play 框架时,需要单独处理带有斜杠的路径,因为该框架的路由引擎会这样做。

    因此,您要么在 URL 中添加斜杠,要么在 routes 文件中更改为以下行:

    GET /products/ controllers.Products.list()
    

    到:

    GET /products controllers.Products.list()
    

    解决方法

    我使用以下代码从 URL 中删除尾部斜杠:

    将此行添加到您的 routes 文件中:

    # Reroute URL with a trailing slash
    GET    /*path/    controllers.Reroute.trailingSlash(path: String)
    

    这是对应的类:

    public class Reroute extends Controller {
        public static Result trailingSlash(String path) {
            /**
             * Moved_Permantly is an HTTP code that indicates a moved ressource. The browser will
             * cache the new address and redirect automatically if the user enters the old URL again.
             */
            return movedPermanently("/" + path);
        }
    }
    

    如果我没记错的话,包含path 变量的路径不带斜杠。如果您使用此代码,请检查指定的每条路线是否都没有尾部斜杠,否则该路线将不再有效。

    【讨论】:

      猜你喜欢
      • 2011-11-09
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多