简体   繁体   中英

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. 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):

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:

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." Documentation Play . You can also take a look there: How to create Map - post on Stack Overflow . 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:

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

The most important part happens in 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 . 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. In there I have another method called 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM