简体   繁体   English

bindFromRequest() 在 play framework 2.0 中不工作。 表单值始终为 null

[英]bindFromRequest() not working in play framework 2.0. Form values are always null

The problem is that, I can not read values in submitted form in play framework 2.0.问题是,我无法在 play framework 2.0 中读取提交表单中的值。 The value is always null,该值始终为 null,

Here is my code:这是我的代码:

my model file我的 model 文件

 package model;

 public class Paper {
    public String query;
 }

my index.scala.html我的指数.scala.html

@(myform: Form[model.Paper])

@helper.form(action =routes.Application.newPaper()) {
   myvalue:  @helper.inputText(myform("query"))  <br><br>
   <br><input type="submit">
}

conf/routes会议/路线

 POST     /newkey                  controllers.Application.newPaper()
 GET     /                           controllers.Application.index()

my Application.java我的申请.java

  public static Result index() {
    Paper paper = new Paper();
    paper.query = "initial value";
    Form<Paper> paperForm =  Form.form(Paper.class).fill(paper);
    return ok(index.render(paperForm);
  }

  public static Result newPaper() {
    Form<Paper> paperForm = Form.form(Paper.class).bindFromRequest();
    if (!paperForm.hasErrors()) {
        Paper paper = paperForm.get();
        Logger.info("query= " + paper.query); //why I always get "query= null" ???
    }
    return redirect(routes.Application.index()); 
 }

When I visit the html page, I can not see "initial value" in the text inputbox.当我访问 html 页面时,在文本输入框中看不到“初始值”。 When I fill the inputbox and click submit button, The printed log is always: query= null当我填写输入框并单击提交按钮时,打印的日志始终是: query= null

I also monitored using chrome devtool.我还使用 chrome devtool 进行了监控。 I saw post http request was sent.我看到帖子 http 请求已发送。 But the finial status code is 303, instead of 200.但是最终状态代码是 303,而不是 200。

If I use DynamicForm to process the submitted form in newPaper(), then I can read the query value as expected.如果我在 newPaper() 中使用 DynamicForm 处理提交的表单,那么我可以按预期读取查询值。 But Why current code does not work?但是为什么当前代码不起作用?

Thanks for help!感谢帮助!

You need to specify method attribute as well in form tag with value post. 您还需要在带有值过帐的表单标记中指定方法属性。 But helper method doesn't support method attribute. 但是辅助方法不支持方法属性。 So do it this way. 这样做吧。

@(myform: Form[model.Paper])

<form method="POST" action ="@routes.Application.newPaper()"> 
   query:  @helper.inputText(myform("query"))  <br><br>
   <br><input type="submit">
</form>

I have used formFactory instead of Forms . 我使用formFactory而不是Forms

Add .withDirectFieldAccess(true); 添加.withDirectFieldAccess(true); in myApplication.java for bindFormRequest . myApplication.java获取bindFormRequest

So your code should be: 因此,您的代码应为:

Form<Paper> paperForm = Form.form(Paper.class).bindFromRequest().withDirectFieldAccess(true);

Instead of: 代替:

Form<Paper> paperForm = Form.form(Paper.class).bindFromRequest();

By this you can get all the fields. 通过此操作,您可以获取所有字段。 You can find more help on this link - https://www.playframework.com/documentation/2.7.x/JavaForms 您可以在此链接上找到更多帮助-https: //www.playframework.com/documentation/2.7.x/JavaForms

I had encountered this problem even in 2022.我什至在 2022 年就遇到过这个问题。

I am using version 2.8.x, and here are the solutions I had found.我使用的是 2.8.x 版,这是我找到的解决方案。

Case 1.情况1。

If you have written setters in your model, the fields with setters will be filled automatically, and you should not have any problem.如果你的model里面写了setter ,有setter的字段会自动填充,应该没有问题。

Case 2.案例 2。

If you don't have setters in your model, you should use the following line: Form.form(Paper.class).withDirectFieldAccess(true).bindFromRequest(reqeust);如果您的model中没有设置器,则应使用以下行: Form.form(Paper.class).withDirectFieldAccess(true).bindFromRequest(reqeust);

The position where you put withDirectFieldAccess(true) matters in version 2.8.x, and you also have to pass the request object as the argument. position 放在withDirectFieldAccess(true)的位置在 2.8.x 版本中很重要,您还必须将request object 作为参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM