Java Playframework:处理表单提交

Java Playframework:处理表单提交,java,model-view-controller,playframework,Java,Model View Controller,Playframework,我正在写剧本2.2表格申请。我使用这个函数创建一个表单 public static Result editIndicator() { Indicators addIndObj = new Indicators(); addIndObj.aDt = (new Date()).toString(); addIndObj.aUid = Long.parseLong(play.mvc.Controller.session("userid"));

我正在写剧本2.2表格申请。我使用这个函数创建一个表单

public static Result editIndicator() {
        Indicators addIndObj = new Indicators();
        addIndObj.aDt = (new Date()).toString();
        addIndObj.aUid = Long.parseLong(play.mvc.Controller.session("userid"));
        addIndObj.dTag = "N";

        // List of all Countries
        ALLCommon table_listobj = Indicators.ddl1();

        Form<Indicators> newIndicatorForm = form(Indicators.class).fill(
                addIndObj);

        return (ok(addindicator.render(newIndicatorForm, table_listobj)));

    }
因此,我在之前设置值,然后将其绑定到表单。我没有在表单中使用所有这些
@必需的
值(只是频率部分),当我尝试获取填充表单时,会出现表单错误

Form(of=class models.Indicators, 
data={}, value=None,
errors={time=[ValidationError(time,error.required,[])], 
aDt=[ValidationError(aDt,error.required,[])], 
dTag=[ValidationError(dTag,error.required,[])], 
aUid=[ValidationError(aUid,error.required,[])], 
indFrequency=[ValidationError(indFrequency,error.required,[])]})
即使我不使用这些值,也需要在表单中设置这些值吗?或者我错过了什么。。 感谢您的帮助。。提前感谢……)

找到了答案

1) 您必须使用模型所需的所有参数,即
@required
,如果您不必在表单中使用它的话。只需将其放入
标记中即可

2) 在提交按钮上,我们称之为
@forms(routes.Application.index)
,但是括号应该封装完整的代码,而不仅仅是
提交按钮。因此

最佳实践是
@forms(routes.Application.index){您在此处的完整代码}

如果您不需要一直验证所有字段,则可以使用组

例如:

// interfaces to create groups
public interface Step1 {}
public interface Step2 {}
因此,您需要将这些组添加到字段中:

@Required(groups = {Step1.class})
@Column(name = "A_DT")
public String aDt;

@Required(groups = {Step1.class})
@Column(name = "A_UID")
public Long aUid;

@Required(groups = {Step1.class})
@Column(name = "D_TAG")
public String dTag;

@Column(name = "TIME")
@Required(groups = {Step2.class})
@Formats.DateTime(pattern = "HH:mm:ss")
public Date time;

@Required(groups = {Step2.class})
@Column(name = "INDFREQUENCY")
public String indFrequency;
然后:

//这仅验证包含Step1 group的字段(本例中为aDt、aUid、dTag)
表单newIndicatorForm=表单(Indicators.class,Step1.class)
.填充(addIndObj);
//这仅验证包含Step2 group的字段(本例中为time、indFrequency)
表单newIndicatorForm=表单(Indicators.class,Step2.class)
.填充(addIndObj);
//这将验证两个组的字段(本例中的所有字段)
表单newIndicatorForm=表单(Indicators.class,Step1.class,Step2.class)
.填充(addIndObj);
@Required(groups = {Step1.class})
@Column(name = "A_DT")
public String aDt;

@Required(groups = {Step1.class})
@Column(name = "A_UID")
public Long aUid;

@Required(groups = {Step1.class})
@Column(name = "D_TAG")
public String dTag;

@Column(name = "TIME")
@Required(groups = {Step2.class})
@Formats.DateTime(pattern = "HH:mm:ss")
public Date time;

@Required(groups = {Step2.class})
@Column(name = "INDFREQUENCY")
public String indFrequency;
// this only validates the fields that have Step1 group (aDt, aUid, dTag in this example)
Form<Indicators> newIndicatorForm = form(Indicators.class, Step1.class)
        .fill(addIndObj);

// this only validates the fields that have Step2 group (time, indFrequency in this example)
Form<Indicators> newIndicatorForm = form(Indicators.class, Step2.class)
        .fill(addIndObj);

// this validates the fields of both groups (all the fields in this example)
Form<Indicators> newIndicatorForm = form(Indicators.class, Step1.class, Step2.class)
        .fill(addIndObj);