为什么我将地图的类型不匹配作为路由参数?

时间:2015-05-04 14:36:04

标签: java dictionary playframework scala-template

我正在使用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]]

为什么地图会转换为字符串?

2 个答案:

答案 0 :(得分:1)

默认参数类型为String:&#34;对于String类型的参数,参数类型是可选的。&#34; Documentation Play。你也可以看一下:How to create Map - post on Stack Overflow。您应该创建正确的模板,然后将其传递给配置文件

中方法中的参数

答案 1 :(得分:0)

我已经几乎完全现在正在工作......尽管我还不完全明白:

我在视图类中的按钮,现在没有任何参数

<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!

有没有办法这样做?如果是,我的问题将完全解决。