在Java中使用Playframework时的父子表单

在Java中使用Playframework时的父子表单,第1张

在Java中使用Playframework时的父/子表单

此处记录了这一点,特别是在处理重复值方面。当然,文档总是可以改进的。请打开一个引发此问题的问题。而且,这并不表示该框架已过时或被其他人所取代(实际上,Play 2.5即将发布且社区正在增长)。

无论如何,这是一个有关如何完成您描述的父母/子女表格的综合示例。请记住,我不在乎样式的样式。

您的域层次结构:

models/Question.java

package models;public class Question {    public String text;    public String sourceCode;    public String complement;    public Answer answer;}

models/Answer.java

package models;import java.util.List;public class Answer {    public List<Alternative> alternatives;}

models/Alternative.java

package models;public class Alternative {    public boolean correct;    public String statement;}
控制器和路线:

现在,我有以下 *** 作,该 *** 作仅将

Question
对象及其子对象作为JSON返回(因为我们只对如何提交此类数据感兴趣):

package controllers;import models.Question;import play.data.Form;import play.libs.Json;import play.mvc.*;import views.html.*;import static play.data.Form.form;public class Application extends Controller {    public Result index() {        Form<Question> form = form(Question.class);        return ok(index.render(form));    }    public Result post() {        Form<Question> form = form(Question.class).bindFromRequest();        Question question = form.get();        return ok(Json.toJson(question));    }}

请注意

index
*** 作中如何声明a
Form<Question>
并将其作为参数传递给视图。您可以在docs上查看有关如何定义表单的更多信息。让我们看看我们的路线:

GET     /       controllers.Application.index()POST    /save   controllers.Application.post()
表单视图:

最后,我们需要创建将填充并提交数据的表单:

@(questionForm: Form[Question])@main("Welcome to Play") {  @helper.form(action = routes.Application.post()) {    <h2>Question:</h2>    @helper.inputText(questionForm("text"))    @helper.inputText(questionForm("sourceCode"))    @helper.inputText(questionForm("complement"))    <h3>Answers:</h3>    @helper.repeat(questionForm("answer.alternatives"), min = 2) { alternative =>      @helper.checkbox(alternative("correct"))      @helper.inputText(alternative("statement"))    }    <button type="submit">Save</button>  }}

基本上,表单是使用表单助手创建的,该助手将处理表单工作方式的大部分方面(例如按实例显示错误)。特别注意

@helper.repeat
标记:它将创建以下标记(省略不相关的部分):

<h3>Answers:</h3><label>answer.alternatives.0.correct</label><input type="checkbox" name="answer.alternatives[0].correct" value=""/><label>answer.alternatives.0.statement</label><input type="text" name="answer.alternatives[0].statement" value=""/><label>answer.alternatives.1.correct</label><input type="checkbox" name="answer.alternatives[1].correct" value="" /><label>answer.alternatives.1.statement</label><input type="text" name="answer.alternatives[1].statement" value=""/>

请注意,参数是如何命名的以表示顺序以及

alternative
对象的相关不同字段。

提交表格:

最后,填写并提交表单后,您将获得以下JSON:

{  "text": "Question text",  "sourceCode": "Question source",  "complement": "Question complement",  "answer": {    "alternatives": [      {        "correct": true,        "statement": "Statement 1"      },      {        "correct": false,        "statement": "Statement 2"      }    ]  }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5616326.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存