Play Framework - 值login不是controllers.Application的成员

时间:2016-03-31 23:14:44

标签: java playframework

我正在运行最新的Play Framework 2.5.1,并且我从登录页面收到错误:

错误:

value login is not a member of controllers.Application

我尝试添加@符号,即:

@controllers.Application.login()

build.sbt删除注射器,清理/清理文件并更新" CMD"。我正在使用https://www.playframework.com/documentation/2.1.0/JavaGuide中的示例。

HTML CODE

@(form: Form[Application.Login])
<html>
    <head>
        <title>Zentasks</title>
        <link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">
        <link rel="stylesheet" type="text/css" media="screen" href="@routes.Assets.versioned("stylesheets/login.css")">
    </head>
    <body>
        <header>
            <a href=@routes.Application.index" id="logo"><span>Zen</span>tasks</a>
        </header>

    </body>
</html>

控制器

package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {

    public Result index() {
        return ok(index.render("Your new application is ready."));
    }



    public static Result login() {
        return ok(
            login.render(form(Login.class))
        );
    }

    public static class Login {
        public String email;
        public String password;
    }   

}   

路线

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                           controllers.Application.index()

GET     /login                      controllers.Application.login()

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               @controllers.Assets.versioned(path="/public", file: Asset)

1 个答案:

答案 0 :(得分:9)

从控制器的操作中删除static关键字。播放2.5.1默认使用依赖注入,如果要使用static操作,则需要explicitly configure it。因此,您的登录操作必须如下:

// no static keyword here
public Result login() {
    return ok(login.render(form(Login.class)));
}

更新

顺便说一句,你在这里混合了很多东西。我建议您在开发2.5.x版本时不要遵循2.1.0指南,因为这两个版本之间存在很多差异。事实上,Play 2.1.0来自 2013年2月6日

以下是一些可以解释代码失败原因的参考资料:

  1. Java Routing: Dependency Injection
  2. Dependency injecting controllers
  3. Replaced static controllers with dependency injection