简体   繁体   中英

Package issues between controller and views using Form in PlayFramework

I have a problem using forms PlayFramework 2.3.8. In the controller I use play.data.Form, but the view requested me play.api.data.Form. This causes the IntelliJ IDEA 14.0.3 throw me an error and can not run the application.

Error detail:

Error:Play 2 Compiler: 
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output

Error:(59, 50) Play 2 Compiler: 
C:\Users\Jonathan\Documents\WorkspaceIdea\quicket\app\controllers\UsuarioController.java:59: error: incompatible types: Form<Usuario> cannot be converted to List<Usuario>
     return ok(views.html.usuariosHome.render(formularioForm, Comuna.all()));

Controller

package controllers;

import controllers.seguridad.Autorizacion;
import controllers.seguridad.AutorizacionLogica;
import controllers.seguridad.Rol;
import models.Comuna;
import models.Usuario;
import play.data.DynamicForm;
import play.data.Form;
import play.filters.csrf.RequireCSRFCheck;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UsuarioController extends Controller {

    public static Form<Usuario> formularioForm = Form.form(Usuario.class);

    public static Result indexTest() {

        return ok(views.html.usuarios.render(formularioForm, Comuna.all()));
    }
}

View

@(userForm: Form[Usuario], lstComuna: List[Comuna])
@import helper._
@import java.util._
@main("Usuarios") {

    <div class="container">
        <h1>Usuario</h1>
        @form(routes.UsuarioController.agregarUsuario(),'class -> "form-horizontal") {


            @defining(userForm("id")) { uidField => <input type="hidden" name="@uidField.name" id="@uidField.id" value="@uidField.value"> }
            <div class="panel panel-default">
                <div class="panel-heading">Agregar Usuarios</div>
                <div class="panel-body">
                    <div class="form-group">
                        <label for="rut" class="col-sm-2 control-label">Rut</label>
                        <div class="col-sm-4">
                        @inputText(userForm("rut"), 'id -> "rut", 'name -> "rut",'_showConstraints -> false, 'class ->"form-control")
                        </div>
                        <div class="col-sm-1">
                        @inputText(userForm("dv"), 'id -> "dv", 'name -> "dv",'_showConstraints -> false, 'class ->"form-control")
                        </div>
                    </div>
                    ....
        }
}

Thanks in advance.

UPDATE

Thanks to singhakash I resolved a bug in the code. But anyway, the IDE continues to set me an error on Form packages between the view and the controller. The strange thing is that it lets me run the project now.

调节器视图

As of IntelliJ 14.1.6 (Scala plugin 1.5.4) I'm still trying to figure out the right way to solve this, but I was able to make things a little less annoying by reconfiguring the module sources.

The most direct approach is to mark the views source directory as excluded (Right-click -> "Mark Directory As" -> Excluded), but this has the drawback of not showing any new views in autocomplete until you trigger a SBT compile, since the definitions won't be available.

My preferred approach is to just change the order in which IntelliJ looks for sources so that it finds the Twirl templates first, and then failing that, falls back on its play.api.data version, so that I can get autocomplete but also have the errors disappear after SBT recompiles everything.

To do that, open the Module Settings, select the module, then the "Sources" tab, and remove the app folder from sources, then readd it by selecting the app folder and clicking the "Sources" button (there's not a more direct way to reorder them in the UI):

重新排列的源文件夹

This is, of course, super kludgy, especially since it seems to work out-of-the-box for most people! Likewise, note that due to the way this works, changes to existing view templates will still require a recompile to be reflected in the editor since IntelliJ will prefer the SBT-compiled version.

I'll to keep looking into this to see if I can figure out what's particular about my configuration* (and presumably yours) that causes things to not work as expected, since resolving that would be the proper solution.

*In my case I've imported the two Play 2.3 projects as modules of a common project, which I suspect is at least some part of the problem—it's just not clear why that would be

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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