为什么 bindFromRequest 使用错误的形式?
Why is bindFromRequest using the wrong form?
我有2个页面,一个用于输入问题,一个用于输入答案。因此使用了 2 种形式,将它们的内容(问题或答案)发送到索引页面。我还将新创建的问题/答案放入我的数据库中。
但奇怪的是,我输入的所有内容都进入了问题 table。这是由于某个地方的拼写错误还是 bindFromRequest 没有按预期工作?
控制器classapplication.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视图class,您可以在其中输入新答案:
@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视图class,您可以在其中输入新问题:
@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,...当我单击按钮时,每个输入都会进入问题-table在我的数据库中。
我已经在谷歌上搜索了解决方案。我也在我的 Scala IDE (Eclipse) 中做了 activator clean ... activator compile ... activator run
和清理。
使用 play framework 2.3.8 和 Scala IDE 4.0.0.
为什么每个输入都会进入数据库中我的问题 table?
尝试将 sendAnswer 映射到新的 URL,仅用于此测试。变化
POST / controllers.Application.sendAnswer()
类似于:
POST /Antwort controllers.Application.sendAnswer()
在 routes
文件中,请求具有优先级。看起来第一个 POST /
路线优先,这就是为什么你永远不会进入 sendAnswer()
方法
我有2个页面,一个用于输入问题,一个用于输入答案。因此使用了 2 种形式,将它们的内容(问题或答案)发送到索引页面。我还将新创建的问题/答案放入我的数据库中。
但奇怪的是,我输入的所有内容都进入了问题 table。这是由于某个地方的拼写错误还是 bindFromRequest 没有按预期工作?
控制器classapplication.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视图class,您可以在其中输入新答案:
@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视图class,您可以在其中输入新问题:
@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,...当我单击按钮时,每个输入都会进入问题-table在我的数据库中。
我已经在谷歌上搜索了解决方案。我也在我的 Scala IDE (Eclipse) 中做了 activator clean ... activator compile ... activator run
和清理。
使用 play framework 2.3.8 和 Scala IDE 4.0.0.
为什么每个输入都会进入数据库中我的问题 table?
尝试将 sendAnswer 映射到新的 URL,仅用于此测试。变化
POST / controllers.Application.sendAnswer()
类似于:
POST /Antwort controllers.Application.sendAnswer()
在 routes
文件中,请求具有优先级。看起来第一个 POST /
路线优先,这就是为什么你永远不会进入 sendAnswer()
方法