Привет, я новичок в PlayFramework, и я делаю учебник, который нашел в Интернете при создании книжного магазина.Я получаю сообщение об ошибке компиляции, в котором говорится, что у класса есть местоположение, не совпадающее с его содержимым. Я включил свой код ниже.
Book.java (класс модели)
package Models;
import java.util.HashSet;
import java.util.Set;
public class Book {
public Integer id;
public String title;
public double price;
public String author;
private static Set<Book> books;
public Book(){
}
public Book(Integer id, String title, double price, String author) {
this.id = id;
this.title = title;
this.price = price;
this.author = author;
}
static {
books=new HashSet<>();
books.add(new Book(1,"C++",20,"Mehani"));
books.add(new Book(2,"Java",30,"Anju"));
}
public static Set<Book> allBooks(){
return books;
}
public static Book fineById(Integer id){
for (Book book:books){
if (id.equals(book.id)){
return book;
}
}
return null;
}
public static void add(Book book){
books.add(book);
}
public static boolean remove(Book book){
return books.remove(book);
}
}
BookController
package controllers;
import Models.Book;
import play.data.Form;
import play.filters.cors.CORSFilter;
import play.filters.csrf.CSRFFilter;
import play.filters.headers.SecurityHeadersFilter;
import play.mvc.Controller;
import play.mvc.Result;
import javax.inject.Inject;
import play.data.FormFactory;
import views.html.Books.*;
import java.util.Set;
public class BookController extends Controller {
@Inject
FormFactory formFactory;
@Inject
CSRFFilter csrfFilter;
@Inject
CORSFilter corsFilter;
@Inject
SecurityHeadersFilter secHeaders;
//for all books
public Result index(){
Set<Book> books= Book.allBooks();
return ok(index.render(books));
}
//to add a book
public Result create(){
Form<Book> bookForm=formFactory.form(Book.class);
return ok(create.render(bookForm));
}
//to save a book
public Result save(){
Form<Book> bookForm= formFactory.form(Book.class).bindFromRequest();
Book book = bookForm.get();
Book.add(book);
return redirect(routes.BookController.index());
}
public Result edit(Integer id){
return TODO;
}
public Result update(){
return TODO;
}
public Result destroy(Integer id){
return TODO;
}
public Result show(Integer id){
return TODO;
}
}
маршруты
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# An example controller showing a sample home page
GET / controllers.HomeController.index
GET /books controllers.BookController.index()
GET /books/create controllers.BookController.create()
GET /books/:id controllers.BookController.show(id: Integer)
GET /books/edit/:id controllers.BookController.edit(id: Integer)
+nocsrf
POST /books/edit controllers.BookController.update()
+nocsrf
POST /books/create controllers.BookController.save()
GET /books/destroy/:id controllers.BookController.destroy(id: Integer)
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
Это мое представление index.scala.html.(Переменные выделены красным и говорят, что не может найти символ)
@import Models.Book
@(books : Set[Book])
<html>
<head>
<title>All books</title>
</head>
<body>
<h1>All Books</h1>
@for(book <- books){
<a href="#">@book.title</a>
<p>Price: @book.price</p>
<p>Author: @book.author</p>
}
</body>
</html>
Это ошибка компиляции, которую я получаю.
[info] Compiling 1 Scala source and 1 Java source to J:\myfirstapp\target\scala-2.12\classes ...
[error] error while loading Book, class file 'J:\myfirstapp\target\scala-2.12\classes\models\Book.class' has location not matching its contents: contains class Book
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] application -
! @7a4gfkgn9 - Internal server error, for (GET) [/] ->
play.sbt.PlayExceptions$CompilationException: Compilation error[error while loading Book, class file 'J:\myfirstapp\target\scala-2.12\classes\models\Book.class' has location not matching its contents: contains class Book]
at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:34)
at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:34)
at scala.Option.map(Option.scala:146)
at play.sbt.run.PlayReload$.$anonfun$taskFailureHandler$1(PlayReload.scala:33)
at scala.Option.map(Option.scala:146)
at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:28)
at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:24)
at play.sbt.run.PlayReload$.$anonfun$compile$3(PlayReload.scala:51)
at scala.util.Either$LeftProjection.map(Either.scala:573)
at play.sbt.run.PlayReload$.compile(PlayReload.scala:51)
Я получил это после попытки реализовать save ().
Структура проекта
myfirstapp
.g8
.gradle
.idea
app
controllers
Modles
Book
views
Books
home
index.scala.html
main.scala.html
conf
gradle
logs
project
public
target
test
build.gradle
build.sbt
gradlew
gradlew.bat
myfirstapp.iml
Как исправить эту ошибку компиляции?