为什么bindFromRequest使用错误的形式? [英] Why is bindFromRequest using the wrong form?

查看:105
本文介绍了为什么bindFromRequest使用错误的形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2页,其中一页用于输入问题,另一页用于输入答案。因此使用了两种形式,它们将内容(问题或答案)发送到索引页面。我也将新创建的问题/答案放入我的数据库中。



但奇怪的是,我输入的所有内容都放入问题表中。这是由于某个地方出现拼写错误,或者bindFromRequest未按预期工作?



控制器类 application.java

  static List< Question> questionListAll = new ArrayList< Question>(); 
static List< Answer> answerListAll = new ArrayList< Answer>();

private static final表单<问题> newQuestionForm = Form.form(Question.class);
private static final Form< Answer> newAnswerForm = Form.form(Answer.class);

//转到提问问题页面
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));
}

//将问题发送到索引页
public static Result sendQuestion(){
//创建新的问题表单并填入另一页
表格<问题> boundQuestion = newQuestionForm.bindFromRequest();
问题newQuestion = boundQuestion.get();
Question.create(newQuestion);
questionListAll.add(newQuestion);
return ok(views.html.index.render(questionListAll,answerListAll));
}

//写一个答案,转到answerpage
public static Result writeAnswer(){
List< Answer> answerHelper = new ArrayList< Answer>();

for(Answer Item:Answer.find.all()){
answerHelper.add(answerItem);
}
return ok(views.html.antwortGeben.render(newAnswerForm,answerHelper));
}

//发送答案给indexpage
public static Result sendAnswer(){
Form< Answer> boundAnswer = newAnswerForm.bindFromRequest();
解答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(userID))
< / fieldset> $ b $ helper.inputText(answerForm(voteScore))
< input type =submitclass =btn btn-default>
}
}

我的 frageAntwort.scala.html strong> view 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 =submitclass =btn btn-default>
}
}

我的routes.conf:

 #主页
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的形式,...当我点击按钮时,每个输入都会进入我的数据库中的问题表。



我已经搜索了一个解决方案。我也做了 activator clean ... activator compile ... activator run ,并在我的Scala IDE(Eclipse)中进行了清理。

使用play framework 2.3.8和Scala IDE 4.0.0。

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

解决方案

尝试将sendAnswer映射到仅用于此测试的新URL。 Change

  POST / controllers.Application.sendAnswer()

类似于:

  POST / Antwort controllers.Application.sendAnswer )

routes 文件中,他们的优先。它看起来像第一个 POST / 路线优先,这就是为什么你永远不会进入 sendAnswer()方法


I have 2 pages, one is for entering questions and one is for entering answers. Therefore 2 forms are used, which send their content (question or answer) to the index page. I also put the newly created question / answer into my database.

But strangely everything that I enter goes into the question table. Is this due to a typo somewhere or is bindFromRequest not working as intended?

Controller class 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));
}

My antwortGeben.scala.html view class, where you can enter a new answer:

@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">
    }
}

My frageAntwort.scala.html view class, where you can enter new questions:

@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">
    }
}

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

So when I go to the page where you can enter a new Answer, I type into the form the answerID, ... and when I click the button, every input goes into the question-table in my DB.

I already have googled for a solution. I also did activator clean ... activator compile ... activator run and cleaned in my Scala IDE (Eclipse).

Using play framework 2.3.8 and Scala IDE 4.0.0.

Why does every input go into my question table in the DB?

解决方案

Try mapping the sendAnswer to a new URL just for this test. Change

POST    /         controllers.Application.sendAnswer()

to something like:

POST    /Antwort  controllers.Application.sendAnswer()

In the routes file the requests have their priority. It looks like the first POST / route takes precedence and that is why you never get to the sendAnswer() method

这篇关于为什么bindFromRequest使用错误的形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆