如何将问题/答案对传输到 playframework 中的视图-class?
How to transfer questions / answers pairs into a view-class in the playframework?
我试图在我的控制器中伪造一些问题/答案对-class 并通过使用 playframework 将它们发送到视图-class。
我的问题/答案结构如下:
- Question 1
-- Answer 1.1
-- Answer 1.2
- Question 2
-- Answer 2.1
- Question 3
-- Answer 3.1
-- Answer 3.2
...
我的 Application.java class 里有这些。目前它们是硬编码的,稍后我想使用数据库。
然后我有一个 index.scala.html(视图 class),在那里我迭代问题和答案并像在我的结构中一样显示它们 - 这是行不通的!
但是我怎样才能将它们从 java class 发送到 scala class?
到目前为止我已经尝试使用 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
...
所以我在每个问题中都显示了每个答案。这源于我不知道如何 link 相应问题的答案的问题!我在问题和答案中有 ID,但我不知道如何将它们(与 q/a 文本一起!)放入视图 class 以及如何迭代 ID。
也许我可以使用另一种数据结构来简化我的方法?
[EDIT1]===================================== ===============[EDIT1]
我现在改变了我的方法,使用带有 2 个 classes(Question.java 和 Answer.java)的法线贴图:
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(控制器class)中,我使用地图并有一些假问题/答案:
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]
那么我如何遍历 answerTexts 并在我的视图中显示它们-class?我不知道如何访问列表。
只需使用内部循环来迭代答案(因为value变量将保存循环迭代问题的答案):
index.scala.html
@import model.Question
@import model.Answer
@(myMap: Map[Question, List[Answer]])
@for((key, value) <- myMap){
@key.questionText<br>
@for(ans <- value){
<p>Possible answer: @ans.answerText</p>
}
}
我试图在我的控制器中伪造一些问题/答案对-class 并通过使用 playframework 将它们发送到视图-class。
我的问题/答案结构如下:
- Question 1
-- Answer 1.1
-- Answer 1.2
- Question 2
-- Answer 2.1
- Question 3
-- Answer 3.1
-- Answer 3.2
...
我的 Application.java class 里有这些。目前它们是硬编码的,稍后我想使用数据库。
然后我有一个 index.scala.html(视图 class),在那里我迭代问题和答案并像在我的结构中一样显示它们 - 这是行不通的!
但是我怎样才能将它们从 java class 发送到 scala class?
到目前为止我已经尝试使用 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
...
所以我在每个问题中都显示了每个答案。这源于我不知道如何 link 相应问题的答案的问题!我在问题和答案中有 ID,但我不知道如何将它们(与 q/a 文本一起!)放入视图 class 以及如何迭代 ID。
也许我可以使用另一种数据结构来简化我的方法?
[EDIT1]===================================== ===============[EDIT1]
我现在改变了我的方法,使用带有 2 个 classes(Question.java 和 Answer.java)的法线贴图:
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(控制器class)中,我使用地图并有一些假问题/答案:
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]
那么我如何遍历 answerTexts 并在我的视图中显示它们-class?我不知道如何访问列表。
只需使用内部循环来迭代答案(因为value变量将保存循环迭代问题的答案):
index.scala.html
@import model.Question
@import model.Answer
@(myMap: Map[Question, List[Answer]])
@for((key, value) <- myMap){
@key.questionText<br>
@for(ans <- value){
<p>Possible answer: @ans.answerText</p>
}
}