gpt4 book ai didi

java - 为什么bindFromRequest 使用了错误的形式?

转载 作者:行者123 更新时间:2023-11-30 08:12:34 24 4
gpt4 key购买 nike

我有 2 页,一页用于输入问题,一页用于输入答案。因此,使用了 2 个表单,将其内容(问题或答案)发送到索引页。我还将新创建的问题/答案放入我的数据库中。

但奇怪的是,我输入的所有内容都会进入问题表。这是由于某处的拼写错误还是bindFromRequest未按预期工作?

Controller 类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 View 类,您可以在其中输入新答案:

@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 View 类,您可以在其中输入新问题:

@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()

因此,当我进入可以输入新答案的页面时,我会在表单中输入答案ID,...当我单击按钮时,每个输入都会进入我的数据库中的问题表。

我已经用谷歌搜索了解决方案。我还在我的 Scala IDE (Eclipse) 中执行了 activator clean ... activator compile ... activator run 并进行了清理。

使用play框架2.3.8和Scala IDE 4.0.0。

为什么每个输入都会进入数据库中的问题表?

最佳答案

尝试将 sendAnswer 映射到新的 URL 以进行此测试。改变

POST    /         controllers.Application.sendAnswer()

类似:

POST    /Antwort  controllers.Application.sendAnswer()

routes 文件中,请求有其优先级。看起来第一个 POST/ 路由优先,这就是为什么你永远不会到达 sendAnswer() 方法

关于java - 为什么bindFromRequest 使用了错误的形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30170304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com