簡體   English   中英

為什么我將映射的類型不匹配作為路由參數?

[英]Why do I get a type mismatch for a map as a routing parameter?

我正在使用playframework 2.3.8,並具有一個視圖類。 在其中有一個按鈕:

<button class="btn btn-default" type="button" onclick="@routes.Application.sendMap(myMap)" method="POST">Send</button>

我想在控制器類(Application.java)中的地圖上添加一個問題/答案對:

public static Result sendMap(Map<Question, List<Answer>> sendMap){
    Question question4 = new Question("ertw", "SendMap Question?", 34, "Tim");
    Answer answer41 = new Answer("werw", "ertw", "SendMap Answer 1!", 12, "Oliver");
    Answer answer42 = new Answer("tzsdfu", "ertw", "SendMap Answer 2!", 1, "Marcus");

    List<Answer> answerList4 = new ArrayList<Answer>();
    answerList4.add(answer41);
    answerList4.add(answer42);

    sendMap.put(question4, answerList4);
    return ok(views.html.frageAntwort.render(sendMap));
}

在我的routes.conf中,我已將路由添加到控制器類,並使用Map作為參數:

POST    /QuestionMap                    controllers.Application.sendMap(Map)

但是現在我得到了錯誤:

type mismatch; found : String required: java.util.Map[model.Question,java.util.List[model.Answer]]

為什么將地圖轉換為字符串?

默認參數類型為String:“對於String類型的參數,參數類型是可選的。” 文件播放 您也可以在這里看看: 如何在Stack Overflow上創建Map-post 您應該創建正確的模板,然后將其通過配置文件中方法中的參數傳遞

我現在幾乎可以完全使用 ……盡管我還不完全了解如何:

我的按鈕在視圖類中,現在沒有任何參數

<a href="@routes.Application.sendMap()"><button class="btn btn-default" type="button">Absenden</button>

我的控制器類現在可以正確地將另一個偽造的問題/答案對放入地圖中,然后將其與更改后的地圖一起返回到index.scala.html:

return ok(views.html.index.render(myMap));

最重要的部分發生在routes.conf中:

GET     /                           controllers.Application.index()
GET     /Fragenliste                controllers.Application.sendMap()
GET     /FrageStellen               controllers.Application.askQuestion()
GET     /Einstellungen              controllers.Application.showSettings()
GET     /Quiz                       controllers.Application.startQuiz()

sendMap()現在沒有參數,並且指向另一個名為/Fragenliste站點。 還有我不完全理解的部分-如果我使用

GET     /                           controllers.Application.sendMap()

它指向Application.java中的index()方法。 在那里,我有另一個名為initialize()的方法:

public static Result index() {
    initialize();
    (...)
}

public static void initialize() {
    Question question1 = new Question("xyz", "Do Androids dream?", 127, "Marcus");
    Answer answer11 = new Answer("zab", "xyz", "Only of electric sheep!", 70, "Tibor");
    Answer answer12 = new Answer("qwert", "xyz", "No, they dont!", 10, "Sarah");

    List<Answer> answerList1 = new ArrayList<Answer>();
    answerList1.add(answer11);
    answerList1.add(answer12);

    myMap.clear();
    myMap.put(question1, answerList1);
}

在此之后,我清除了舊地圖的其余部分后,就可以構建地圖。 因此,我的路線指向了索引,現在沒有添加新問題! 為什么我也不能指向索引,但仍然可以正常工作?

基本上:

mysite.com/ = index ... user presses button for new question, gets transfered to
mysite.com/enterQuestion ... user enters question and hits "submit", gets transfered to
mysite.com/ = index

我目前的解決方案是:

mysite.com/ = index ... user presses button for new question, gets transfered to
mysite.com/enterQuestion ... user enters question and hits "submit", gets transfered to
mysite.com/ANOTHER_SITE != index, but looks and works exactly like the index!

有辦法嗎? 如果是,我的問題將得到徹底解決。

暫無
暫無

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

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