Play framework 2.5.3 没有相应的数据绑定访问器

does not have a corresponding accessor for data binding Play framework 2.5.3

我想将用户数据保存到数据库中。 玩2.5.3版本

我收到这个错误:

JSR-303 validated property 'first_name' does not have a corresponding accessor for data binding - check your DataBinder's configuration (bean property versus direct field access)]

我的模型class

@Entity
public class UserRegisterModel extends Model
{
    @Id
    @GeneratedValue
    protected Long ID;

    @Constraints.Required
    protected String first_name;
    protected String last_name;
    protected String user_name;

    @Constraints.Required
    protected String password;
    protected String password_confirmation;

    @Constraints.Email
    @Column(unique = true)
    protected String email;
}

控制器class

public Result submitUserRegistrationForm()
{
    play.data.Form<UserRegisterModel> form = play.data.Form.form(UserRegisterModel.class).bindFromRequest();
    UserRegisterModel register = form.bindFromRequest().get();
}

我也想匹配密码和确认密码。我应该在模型或控制器中执行此操作。

能否请您提供一些带有表单验证的示例代码(模型、控制器)?

正如评论中所讨论的那样,您必须按照此处的说明添加播放增强器:

https://www.playframework.com/documentation/2.5.x/PlayEnhancer#Setting-up

此外,增强器仅在以下条件下工作:

The enhancer looks for all fields on Java classes that:

  • are public
  • are non static
  • are non final

For each of those fields, it will generate a getter and a setter if they don’t already exist. If you wish to provide a custom getter or setter for a field, this can be done by just writing it, the Play enhancer will simply skip the generation of the getter or setter if it already exists.

因此,您在这里有两个选择:保留字段 protected 并编写您自己的 getter 和 setter 或制作 public 并让增强器生成其他库所需的 getter 和 setter(例如表单绑定)。