我有2页,一个用于输入问题,一个用于输入答案。因此,使用2个表单,将其内容(问题或答案)发送到索引页面。我还将新创建的问题/答案放入我的数据库中。
但奇怪的是,我输入的所有内容都会进入问题表。这是由于某个地方的拼写错误还是bindFromRequest无法正常工作?
控制器类 application.java :
static List<Question> questionListAll = new ArrayList<Question>();
static List<Answer> answerListAll = new ArrayList<Answer>();
private static final Form<Question> newQuestionForm = Form.form(Question.class);
private static final Form<Answer> newAnswerForm = Form.form(Answer.class);
// Go to the ask question page
public static Result askQuestion(){
List<Question> questionHelper = new ArrayList<Question>();
for (Question questionItem : Question.find.all()) {
questionHelper.add(questionItem);
}
return ok(views.html.frageAntwort.render(newQuestionForm, questionHelper));
}
// Send the question to the indexpage
public static Result sendQuestion(){
// Create new question-form and fill it with the values from the other page
Form<Question> boundQuestion = newQuestionForm.bindFromRequest();
Question newQuestion = boundQuestion.get();
Question.create(newQuestion);
questionListAll.add(newQuestion);
return ok(views.html.index.render(questionListAll, answerListAll));
}
// Write an answer, goto answerpage
public static Result writeAnswer(){
List<Answer> answerHelper = new ArrayList<Answer>();
for (Answer answerItem : Answer.find.all()) {
answerHelper.add(answerItem);
}
return ok(views.html.antwortGeben.render(newAnswerForm, answerHelper));
}
// Send answer to indexpage
public static Result sendAnswer(){
Form<Answer> boundAnswer = newAnswerForm.bindFromRequest();
Answer newAnswer = boundAnswer.get();
Answer.create(newAnswer);
answerListAll.add(newAnswer);
return ok(views.html.index.render(questionListAll, answerListAll));
}
我的 antwortGeben.scala.html 视图类,您可以在其中输入新答案:
@import models.Question
@import models.Answer
@import helper._
@import helper.twitterBootstrap._
@(answerForm: Form[Answer], answerList: List[Answer])
@main("Antwort geben"){
@helper.form(action = routes.Application.sendAnswer()){
<fieldset>
@helper.inputText(answerForm("answerID"))
@helper.inputText(answerForm("questionID"))
@helper.inputText(answerForm("answerText"))
@helper.inputText(answerForm("voteScore"))
@helper.inputText(answerForm("userID"))
</fieldset>
<input type="submit" class="btn btn-default">
}
}
我的 frageAntwort.scala.html 视图课程,您可以在其中输入新问题:
@import models.Question
@import models.Answer
@import helper._
@import helper.twitterBootstrap._
@(questionForm: Form[Question], questionList: List[Question])
@main("Frage stellen"){
@helper.form(action = routes.Application.sendQuestion()){
<fieldset>
@helper.inputText(questionForm("questionID"))
@helper.inputText(questionForm("questionText"))
@helper.inputText(questionForm("voteScore"))
@helper.inputText(questionForm("userID"))
</fieldset>
<input type="submit" class="btn btn-default">
}
}
我的routes.conf:
# Home page
GET / controllers.Application.index()
#Questions
GET /FrageStellen controllers.Application.askQuestion()
POST / controllers.Application.sendQuestion()
#Answers
GET /AntwortGeben controllers.Application.writeAnswer()
POST / controllers.Application.sendAnswer()
因此,当我转到您可以输入新答案的页面时,我在表单中输入answerID,...当我单击按钮时,每个输入都会进入我的数据库中的问题表。
我已经google了解决方案。我也做了activator clean ... activator compile ... activator run
并在我的Scala IDE(Eclipse)中清理过。
使用play framework 2.3.8和Scala IDE 4.0.0。
为什么每个输入都进入我在DB中的问题表?
答案 0 :(得分:1)
尝试将sendAnswer映射到仅适用于此测试的新网址。变化
POST / controllers.Application.sendAnswer()
类似于:
POST /Antwort controllers.Application.sendAnswer()
在routes
文件中,请求具有优先权。看起来第一个POST /
路线优先,这就是为什么你永远不会进入sendAnswer()
方法