简体   繁体   中英

Play Framework beginner. Scala.html error in compilation

I am learning the Play framework and I am learning CRUD controllers. I want to make a form to add new posts but I have a compilation error. As I am new to scala I cannot figure out the error.

@(productForm: Form[Product])
@import helper._
@import helper.twitterBootstrap._

@main("Product Form"){
<h1>Product Form</h1>

@helper.form(action = routes.Products.save()){
    <fieldset>
        <legend>Product (@productForm("name").valueOr("New")) </legend>
        @helper.inputText(@productForm("ean"), '_label -> "EAN")
        @helper.inputText(@productForm("name"), '_label -> "Name")
        @helper.textarea(@productForm("description"), '_label -> "Description")
    </fieldset>

    <input type="submit" class="btn btn-primary" value="Save">
    <a class="btn" href="@routes.Products.index()">Cancel</a>
}
}

the error is:

/Users/andrei/Desktop/PlayFramework/app/views/products/details.scala.html:11: illegal start of simple expression
[error]             @helper.inputText(@productForm("ean"), '_label -> "EAN")

another problem:I define in the class a private static final variable, but I get an error. I think it's due to a deprecated library as I am learning the framework from a 2014 book

import play.api.data.Form; 
import play.api.FormFactory;
public class Products extends Controller {

Form<Product> productForm = formFactory.form(Product.class);

the error:

cannot find symbol
symbol:   variable formFactory
location: class controllers.Products

source to documentation where I've found FormFactory: https://www.playframework.com/documentation/2.5.x/JavaForms

Since you started the expression with @ , than you do not need to use it before productForm , so

@helper.inputText(productForm("ean"), '_label -> "EAN")

should do the trick for you.

The Scala template uses @ as the single special character. Every time this character is encountered, it indicates the beginning of a dynamic statement. You are not required to explicitly close the code block - the end of the dynamic statement will be inferred from your code:

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