【发布时间】:2022-04-01 16:54:50
【问题描述】:
问题是,我无法在 play framework 2.0 中读取提交表单中的值。该值始终为空,
这是我的代码:
我的模型文件
package model;
public class Paper {
public String query;
}
我的 index.scala.html
@(myform: Form[model.Paper])
@helper.form(action =routes.Application.newPaper()) {
myvalue: @helper.inputText(myform("query")) <br><br>
<br><input type="submit">
}
配置/路由
POST /newkey controllers.Application.newPaper()
GET / controllers.Application.index()
我的 Application.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());
}
当我访问html页面时,在文本输入框中看不到“初始值”。当我填写输入框并单击提交按钮时,打印的日志始终为:query= null
我还使用 chrome devtool 进行了监控。我看到 post http 请求已发送。但最终状态码是 303,而不是 200。
如果我在 newPaper() 中使用 DynamicForm 处理提交的表单,那么我可以按预期读取查询值。但是为什么当前的代码不起作用?
感谢您的帮助!
【问题讨论】:
标签: java playframework playframework-2.0