我正在关注 youtube 上的 Java Play Framework 教程 (#13),我卡在了 BookStore 应用程序的索引方法中。自从我得到:“模棱两可的方法调用。index 中的 render(Set,) 和 index$ 中的 render(Set) 都匹配”。

我尝试将 Set 更改为 List,但我得到的消息基本上与 List 相关。

public class BooksController extends Controller { 
 
    public Result index(){ 
        Set<Book> books = Book.allBooks(); 
 
        return ok(index.render(books));  //<--------- the error 
    }         
} 
 
public class Book { 
 
    public Integer id; 
    public String title; 
    public Integer price; 
    public String author; 
 
    public Book(Integer id, String title,Integer price, String author){ 
        this.id = id; 
        this.title = title; 
        this.price = price; 
        this.author = author; 
    } 
 
    public static Set<Book> books; 
 
    static { 
        books = new HashSet<>(); 
        books.add(new Book(1, "Java", 20, "ABC")); 
        books.add(new Book(2,"C++", 30, "XYZ")); 
    } 
 
    public  static Set<Book> allBooks(){ 
        return books; 
    }  
} 

请您参考如下方法:

您收到此错误是因为 views.html.index.render 中的 index 和 BooksController 中的方法 index() 具有相同的名称并且编译器对使用什么方法感到困惑。只需将渲染方法的名称更改为其他名称,例如 booksIndex(),您的问题就会消失。

public class BooksController extends Controller { 
 
    public Result booksIndex(){ 
        Set<Book> books = Book.allBooks(); 
 
        return ok(index.render(books)); 
    }         
} 

附言之后不要忘记更改路由文件


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!