簡體   English   中英

Playframework-子包裝的控制器和布線問題

[英]Playframework - subpackaged controllers and routing issues

系統詳情:

  • Arch Linux:4.11.7-1-ARCH x64
  • Oracle Java JDK:1.8.0_131
  • IntelliJ Scala插件:2017.1.19(最新)
  • SBT:0.13.13(最新)
  • IntelliJ IDEA Ultimate 2017.1.4版本
  • 播放框架:2.6.1

介紹

注意:我是新玩的框架

我正在使用提供的“ play-starter”項目,並以此創建了一個路由到各個頁面的控制器。 通過不使用任何子包來工作。

目錄結構: {root}/app/controllers/MyController

MyController.index()與路由路徑一起顯示:

GET     /                        controllers.MyController.index

這按預期顯示,我創建的POST方法也按預期運行。


問題:

我嘗試創建子包來啟動我的項目,但是在任何時候都收到錯誤消息(詳細信息如下)。

瀏覽路由文檔沒有解決方案(對我來說)。 我搜索了類似的問題,發現這里是一個非常相似的問題,但是答案不能解決我的問題。

一個博客似乎解決了這個問題,但我沒有成功。

錯誤輸出:

/usr/lib/jvm/java-8-jdk/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:37417,suspend=y,server=n -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled -Dfile.encoding=UTF-8 -classpath /home/cybex/.IntelliJIdea2017.1/config/plugins/Scala/launcher/sbt-launch.jar:/usr/share/intellij-idea-ultimate-edition/lib/idea_rt.jar xsbt.boot.Boot run
Connected to the target VM, address: '127.0.0.1:37417', transport: 'socket'
[info] Loading global plugins from /home/cybex/.sbt/0.13/plugins
[info] Loading project definition from /home/cybex/Documents/play-project/eatalot/project
[info] Set current project to EatAloT (in build file:/home/cybex/Documents/University/Year%205/WRR301/eatalot/eatalot/)

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:8080

(Server started, use Enter to stop and go back to the console...)

[info] Compiling 35 Scala sources and 13 Java sources to /home/cybex/Documents/play-project/eatalot/target/scala-2.12/classes...
[error] /home/cybex/Documents/play-project/eatalot/conf/routes:8: type AccountController is not a member of package controllers.User.routes
[error] GET        /user                 controllers.User.routes.AccountController.index()
[error] /home/cybex/Documents/play-project/eatalot/conf/routes:8: type AccountController is not a member of package controllers.User.routes
[error] GET        /user                 controllers.User.routes.AccountController.index()
[error] /home/cybex/Documents/play-project/eatalot/conf/routes:8: type AccountController is not a member of package controllers.User.routes
[error] GET        /user                 controllers.User.routes.AccountController.index()
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/index.scala.html:28: object AccountController is not a member of package controllers.User.routes
[error]         <form action="@controllers.User.routes.AccountController.login()" method="get">
[error]                                                ^
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/index.scala.html:32: object AccountController is not a member of package controllers.User.routes
[error]         <form action="@controllers.User.routes.AccountController.register()" method="get">
[error]                                                ^
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/login.scala.html:17: object AccountController is not a member of package controllers.User.routes
[error]         <form action="@controllers.User.routes.AccountController.doLogin()" method="post">
[error]                                                ^
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/register.scala.html:16: object AccountController is not a member of package controllers.User.routes
[error]         <form method="post" action="@controllers.User.routes.AccountController.doRegister()">
[error]                                                              ^
[error] 7 errors found
[error] (compile:compileIncremental) Compilation failed
[info] Compiling 35 Scala sources and 13 Java sources to /home/cybex/Documents/play-project/eatalot/target/scala-2.12/classes...
[error] /home/cybex/Documents/play-project/eatalot/conf/routes:8: type AccountController is not a member of package controllers.User.routes
[error] GET        /user                 controllers.User.routes.AccountController.index()
[error] /home/cybex/Documents/play-project/eatalot/conf/routes:8: type AccountController is not a member of package controllers.User.routes
[error] GET        /user                 controllers.User.routes.AccountController.index()
[error] /home/cybex/Documents/play-project/eatalot/conf/routes:8: type AccountController is not a member of package controllers.User.routes
[error] GET        /user                 controllers.User.routes.AccountController.index()
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/index.scala.html:28: object AccountController is not a member of package controllers.User.routes
[error]         <form action="@controllers.User.routes.AccountController.login()" method="get">
[error]                                                ^
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/index.scala.html:32: object AccountController is not a member of package controllers.User.routes
[error]         <form action="@controllers.User.routes.AccountController.register()" method="get">
[error]                                                ^
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/login.scala.html:17: object AccountController is not a member of package controllers.User.routes
[error]         <form action="@controllers.User.routes.AccountController.doLogin()" method="post">
[error]                                                ^
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/register.scala.html:16: object AccountController is not a member of package controllers.User.routes
[error]         <form method="post" action="@controllers.User.routes.AccountController.doRegister()">
[error]                                                              ^
[error] 7 errors found
[error] (compile:compileIncremental) Compilation failed
[error] application - 

! @74j57k5c0 - Internal server error, for (GET) [/] ->

play.sbt.PlayExceptions$CompilationException: Compilation error[type AccountController is not a member of package controllers.User.routes]
    at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27)
    at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27)
    at scala.Option.map(Option.scala:145)
    at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:49)
    at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:44)
    at scala.Option.map(Option.scala:145)
    at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:44)
    at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:40)
    at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17)
    at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17)
[info] Compiling 35 Scala sources and 13 Java sources to /home/cybex/Documents/play-project/eatalot/target/scala-2.12/classes...
[error] /home/cybex/Documents/play-project/eatalot/conf/routes:8: type AccountController is not a member of package controllers.User.routes
[error] GET        /user                 controllers.User.routes.AccountController.index()
[error] /home/cybex/Documents/play-project/eatalot/conf/routes:8: type AccountController is not a member of package controllers.User.routes
[error] GET        /user                 controllers.User.routes.AccountController.index()
[error] /home/cybex/Documents/play-project/eatalot/conf/routes:8: type AccountController is not a member of package controllers.User.routes
[error] GET        /user                 controllers.User.routes.AccountController.index()
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/index.scala.html:28: object AccountController is not a member of package controllers.User.routes
[error]         <form action="@controllers.User.routes.AccountController.login()" method="get">
[error]                                                ^
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/index.scala.html:32: object AccountController is not a member of package controllers.User.routes
[error]         <form action="@controllers.User.routes.AccountController.register()" method="get">
[error]                                                ^
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/login.scala.html:17: object AccountController is not a member of package controllers.User.routes
[error]         <form action="@controllers.User.routes.AccountController.doLogin()" method="post">
[error]                                                ^
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/register.scala.html:16: object AccountController is not a member of package controllers.User.routes
[error]         <form method="post" action="@controllers.User.routes.AccountController.doRegister()">
[error]                                                              ^
[error] 7 errors found
[error] (compile:compileIncremental) Compilation failed
[info] Compiling 35 Scala sources and 13 Java sources to /home/cybex/Documents/play-project/eatalot/target/scala-2.12/classes...
[error] /home/cybex/Documents/play-project/eatalot/conf/routes:8: type AccountController is not a member of package controllers.User.routes
[error] GET        /user                 controllers.User.routes.AccountController.index()
[error] /home/cybex/Documents/play-project/eatalot/conf/routes:8: type AccountController is not a member of package controllers.User.routes
[error] GET        /user                 controllers.User.routes.AccountController.index()
[error] /home/cybex/Documents/play-project/eatalot/conf/routes:8: type AccountController is not a member of package controllers.User.routes
[error] GET        /user                 controllers.User.routes.AccountController.index()
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/index.scala.html:28: object AccountController is not a member of package controllers.User.routes
[error]         <form action="@controllers.User.routes.AccountController.login()" method="get">
[error]                                                ^
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/index.scala.html:32: object AccountController is not a member of package controllers.User.routes
[error]         <form action="@controllers.User.routes.AccountController.register()" method="get">
[error]                                                ^
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/login.scala.html:17: object AccountController is not a member of package controllers.User.routes
[error]         <form action="@controllers.User.routes.AccountController.doLogin()" method="post">
[error]                                                ^
[error] /home/cybex/Documents/play-project/eatalot/app/views/User/Account/register.scala.html:16: object AccountController is not a member of package controllers.User.routes
[error]         <form method="post" action="@controllers.User.routes.AccountController.doRegister()">
[error]                                                              ^
[error] 7 errors found
[error] (compile:compileIncremental) Compilation failed

問題詳細信息:

我創建了一個子包User 在這個子包中,我有一個控制器AccountController

AccountController有幾個函數,出於這個問題的目的,我將使用doLogin()

目錄結構: {root}/app/controllers/User/AccountController

建議的解決方案:

所有解決方案都指向相同的代碼(這會導致各種錯誤)

每個程序包都有自己的路由文件。 就我而言,我應該使用controllers.User.routes后跟路由文件的控制器名稱和功能

例如

POST       /user/login        controllers.User.routes.AccountController.doLogin()

我的目錄布局是controllers/User/AccountController並用於在視圖中顯示此視圖( 在此處找到

<form action="@controllers.User.routes.AccountController.doLogin()" method="post">

但是這不起作用。


額外信息:

{root} / app / controllers / User / AccountController

package controllers.User;

import models.User;
import play.Logger;
import play.data.DynamicForm;
import play.data.FormFactory;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.User.Account.*;

import javax.inject.Inject;
import java.util.Random;
import java.util.Set;

public class AccountController extends Controller{

    @Inject
    FormFactory formFactory;

    public Result index() {
        //...
    }

    public Result login(){
        //...
    }

    public Result doLogin(){
        //...
    }

    public Result register(){
        //...
    }

    public Result doRegister(){
        //...
    }

}

{root} / conf / routes

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

# An example controller showing a sample home page
GET     /                        controllers.HomeController.index

GET        /user                 controllers.User.routes.AccountController.index()
GET        /user/register        controllers.User.routes.AccountController.register()
GET        /user/login           controllers.User.routes.AccountController.login()

##disable this an you will recieve a 403 forbidden error, the CRSF filter is used for cross site scripting prevention, use for authentication
#+ nocsrf
POST       /user/register        controllers.User.routes.AccountController.doRegister()
#+ nocsrf
POST       /user/login           controllers.User.routes.AccountController.doLogin()

# An example controller showing how to use dependency injection
GET     /count                   controllers.CountController.count
# An example controller showing how to write asynchronous code
GET     /message                 controllers.AsyncController.message

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

{root} /app/views/User/Account/index.scala.html

@(users: Set[User])

<html>
    <header>
        <title>All users in list</title>
    </header>
    <body>
        <h1>All users in list</h1>
        <br>
        <table>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Surname</th>
                <th>Cellphone</th>
            </tr>
             @for(user <- users) {
            <tr>
            <td>@user.id</td>
            <td>@user.name</td>
            <td>@user.surname</td>
            <td>@user.cellNumber</td>
            </tr>
            }
        </table>
        <br>
        <br>
        <form action="@controllers.User.routes.AccountController.login()" method="get">
            <input type="submit" value="Login">
        </form>
        <br>
        <form action="@controllers.User.routes.AccountController.register()" method="get">
            <input type="submit" value="Register">
        </form>
    </body>
</html>

{root} /app/views/User/Account/login.scala.html

@(message: String)

<html>
    <header>
        <title>Login Form</title>
    </header>
    <body>
        <h1>Login Form</h1>
        <br/>
        <label>Email:</label> <input type="email" name="edtEmail"/>
        <br/>
        <br/>
        <label>Password:</label> <input type="password" name="edtPassword"/>
        <br/>
        <label>@message</label>
        <br/>
        <form action="@controllers.User.routes.AccountController.doLogin()" method="post">
            <input type="submit" value="Login">
        </form>
    </body>
</html>

似乎是一個錯誤,因為重做了我以前做過的所有“測試”以使其正常運行,事實證明已解決了該問題。

Play 2.6.1項目的正式解決方案,具有子包並需要路由至該子包:

子軟件包名稱: User軟件包控制器: AccountController


項目應用目錄結構:

app/
      controllers/ (provided)
      filters/ (provided)
      models/ 
      services/ (provided)
      views/  (provided)

控制器目錄

controllers/
          User/ (sub package)
                AccountController.java
          HomeController.java (provided)

帳戶控制器包含以下方法:

  • 指數()
  • 寄存器()
  • doRegister()

視圖目錄

views/
          User/
                Account/
                     index.scala.html
                     register.scala.html
          index.scala.html (provided)

注意:嘗試保持相同的目錄結構(請參閱User/Account/{controller function name}

路由文件輸入conf/routes

GET        /user                 controllers.User.AccountController.index
GET        /user/register        controllers.User.AccountController.register()

#disable this (below) and you will receive a 403 forbidden error, the CRSF filter is used for cross site scripting prevention, use for authentication. 
#You can apply this autentication: see https://stackoverflow.com/questions/45017920/post-method-rendering-403-forbidden-page-instead-of-executing-post-method-code

#+ nocsrf
POST       /user/register        controllers.User.AccountController.doRegister()

希望這會有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM