0

コントローラークラスでいくつかの質問/回答のペアを偽造し、プレイフレームワークを使用してそれらをビュークラスに送信しようとしています。

質問/回答には次の構造があります。

- Question 1
-- Answer 1.1
-- Answer 1.2

- Question 2
-- Answer 2.1

- Question 3
-- Answer 3.1
-- Answer 3.2
...

Application.java クラスにそれらがあります。現時点では、それらはハードコーディングされていますが、後でデータベースを使用したいと考えています。

次に、index.scala.html (ビュー クラス) があり、そこで質問と回答を繰り返し処理し、構造のように表示しますが、うまくいきません!

しかし、どうすればJavaクラスからscalaクラスに送ることができますか?

これまでのところ、Google Guava Multimap を使用してみました。

// Needed, as the view-class gets a List[String]
static List<String> questionList = new ArrayList<String>();
static List<String> answerList = new ArrayList<String>();

// Create the Multimap, one for questions, one for answers
    Multimap<String, String> questionMap = ArrayListMultimap.create();
    Multimap<String, String> answerMap = ArrayListMultimap.create();

// Question 1, in the format Questiontext (used as key here) / ID, votescore, Userid
    questionMap.put("What happens if I use a break here?", "e77dccbc-fd8d-4641-b9ca-17528e5d56b2");
    questionMap.put("What happens if I use a break here?", "150");
    questionMap.put("What happens if I use a break here?", "345");

    // Answer 1.1 in the format: Answertext / ID / Question-ID (Answer needs to be linked to a question) / votescore / Userid
    answerMap.put("The loop will just fall through!", "b8756ff5-ff8a-4a17-9517-811b91639fdf");
    answerMap.put("The loop will just fall through!", "e77dccbc-fd8d-4641-b9ca-17528e5d56b2");
    answerMap.put("The loop will just fall through!", "46");
    answerMap.put("The loop will just fall through!", "567");

// The Lists get the keySets (contains the questiontext / answertext)
    questionList.addAll(questionMap.keySet());
    answerList.addAll(answerMap.keySet());

// returns the 2 lists to the view-class
public static Result index() {
    return ok(index.render(questionList, answerList));
}

index.scala.html:

// first line, gets the q/a lists as parameters
@(questions: List[String], answers: List[String])

// iterate over the question-list and show the questiontext (works)

@for(index <- 0 until questions.size){
    @questions(index)

// iterate over the answers and show them as well, I know I am iterating wrong somehow here!
@for(index <- 0 until answers.size){
    @answers(index)
}

私は答えを間違って繰り返していることを知っているので、現在示されている構造は次のとおりです。

- Question 1
-- Answer 1.1
-- Answer 1.2
-- Answer 2.1
-- Answer 3.1
-- Answer 3.2

- Question 2
-- Answer 1.1
-- Answer 1.2
-- Answer 2.1
-- Answer 3.1
-- Answer 3.2

- Question 3
-- Answer 1.1
-- Answer 1.2
-- Answer 2.1
-- Answer 3.1
-- Answer 3.2
...

だから私はすべての質問のすべての答えを示しています。これは、回答を対応する質問にリンクする方法がわからないという問題に起因します。質問と回答に ID がありますが、それらを (q/a テキストと一緒に!) ビュー クラスに取得する方法と、ID を反復処理する方法がわかりません。

私のアプローチをより簡単にする別のデータ構造を使用できるでしょうか?

[EDIT1]============================================== =====[編集1]

私は今、アプローチを変更し、2 つのクラス (Question.java と Answer.java) を持つ通常の Map を使用しています。

Question.java (Answer.java も同様):

public class Question {

public String ID;
public String questionText;
public Integer voteScore;
public String userID;

public Question(String inputID, String inputQuestionText, Integer inputVoteScore, String inputUserID){
    this.ID = inputID;
    this.questionText = inputQuestionText;
    this.voteScore = inputVoteScore;
    this.userID = inputUserID;
}
}

私の Application.java (Controller クラス) では、Map を使用しており、いくつかの偽の質問/回答があります。

static Map<Question, List<Answer>> myMap = new HashMap<Question, List<Answer>>();

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");

Question question2 = new Question("fgh", "Why is the sky blue?", 76, "Frank");
Answer answer21 = new Answer("xcv", "fgh", "Frequency filtered sunlight!", 45, "Oliver");
Answer answer22 = new Answer("tzu", "fgh", "Light reflects from the blue sea water!", 3, "Tom");

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

List<Answer> answerList2 = new ArrayList<Answer>();
answerList2.add(answer21);
answerList2.add(answer22);

myMap.put(question1, answerList1);
myMap.put(question2, answerList2);

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

私のindex.scala.htmlでは:

@import model.Question
@import model.Answer

@(myMap: Map[Question, List[Answer]])

@for((key, value) <- myMap){
  @key.questionText - @value <br>
}

出力は次のとおりです。

Why is the sky blue? - 
[ID = xcv questionID = fgh answerText = Frequency filtered sunlight! voteScore = 45 userID = Oliver, ID = tzu questionID = fgh answerText = Light reflects from the blue sea water! voteScore = 3 userID = Tom]

Do Androids dream? - 
[ID = zab questionID = xyz answerText = Only of electric sheep! voteScore = 70 userID = Tibor, ID = qwert questionID = xyz answerText = No, they dont! voteScore = 10 userID = Sarah] 

では、answerText を反復処理してビュークラスに表示するにはどうすればよいでしょうか。リストへのアクセス方法がわかりません。

4

1 に答える 1