简体   繁体   English

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

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

I am using the playframework 2.3.8 and have a view-class. 我正在使用playframework 2.3.8,并具有一个视图类。 In there a is button: 在其中有一个按钮:

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

I want to append a question / answer pair to the map in my controller class (Application.java): 我想在控制器类(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));
}

In my routes.conf I have added the route to the controller class and use Map as the parameter: 在我的routes.conf中,我已将路由添加到控制器类,并使用Map作为参数:

POST    /QuestionMap                    controllers.Application.sendMap(Map)

But now I get the error: 但是现在我得到了错误:

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

Why does the map get converted into a string? 为什么将地图转换为字符串?

Default parameters type is String: "For parameters of type String, the parameter type is optional." 默认参数类型为String:“对于String类型的参数,参数类型是可选的。” Documentation Play . 文件播放 You can also take a look there: How to create Map - post on Stack Overflow . 您也可以在这里看看: 如何在Stack Overflow上创建Map-post You should create correct template and then pass it through the parameter in your method on the configuration file 您应该创建正确的模板,然后将其通过配置文件中方法中的参数传递

I've got it almost completely working now ... altough I dont completely understand how: 我现在几乎可以完全使用 ……尽管我还不完全了解如何:

My button in the view-class, now without any parameter 我的按钮在视图类中,现在没有任何参数

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

My controller-class now correctly puts another fake question/answer pair into the map and then returns it with the changed map to the index.scala.html: 我的控制器类现在可以正确地将另一个伪造的问题/答案对放入地图中,然后将其与更改后的地图一起返回到index.scala.html:

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

The most important part happens in routes.conf: 最重要的部分发生在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() now has no parameters and points to another site named /Fragenliste . sendMap()现在没有参数,并且指向另一个名为/Fragenliste站点。 And there is the part I dont fully understand - if I use 还有我不完全理解的部分-如果我使用

GET     /                           controllers.Application.sendMap()

it points to the index()-method in Application.java. 它指向Application.java中的index()方法。 In there I have another method called initialize(): 在那里,我有另一个名为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);
}

In this the map gets constructed, after I cleared potential rests of old maps. 在此之后,我清除了旧地图的其余部分后,就可以构建地图。 So my route points to the index and now the new question does not get added! 因此,我的路线指向了索引,现在没有添加新问题! Why cant I point to the index aswell and still have it work? 为什么我也不能指向索引,但仍然可以正常工作?

Basically: 基本上:

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

My solution for the moment is: 我目前的解决方案是:

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!

Is there a way to do so? 有办法吗? If yes, my problem would be fully solved. 如果是,我的问题将得到彻底解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在编译时出现类型不匹配 - Why do I get a type mismatch during compile time 为什么会出现输入不匹配异常? - Why do I get Input Mismatch Exception? 将参数与PowerMockito模拟静态方法一起使用时,为什么参数类型不匹配? - Why do I get an argument type mismatch when I use arguments with PowerMockito mocked static method? 为什么会出现此错误“类型不匹配:无法从序列化转换为T”? - Why do I get this error “Type mismatch: cannot convert from Serializable to T”? 为什么在尝试在Java中反转字符串时会出现类型不匹配的问题? - Why do I get a type mismatch when trying to reverse a string in Java? Map 在分配 Map.class 时是原始类型。 但是分配类型会导致不匹配。 如何参数化 Map.class? - Map is a raw type when assigning Map.class. But assigning a type results in a mismatch. How do I parameterize Map.class? 当我尝试使用具有相同名称和参数类型的两个方法时,为什么会出现编译错误? - Why do I get a compilation error when I try to have two methods with the same name and parameter type? 如何创建泛型子类的实例? 获取错误:“绑定不匹配:类型...不是有界参数的有效替代...” - How do I make an instance of generic subclass? Getting error: “Bound mismatch: The type … is not a valid substitute for the bounded parameter …” 为什么我收到错误“类型不匹配”? - Why i am getting error “Type Mismatch”? 为什么会出现类型不匹配错误? - Why am I getting Type mismatch error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM